started adding plugin system

This commit is contained in:
2024-04-14 20:03:57 +02:00
parent 36b8941eac
commit bc4ada155b
22 changed files with 180 additions and 40 deletions

View File

@@ -11,9 +11,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_addemoji Command = Command{
var cmd_addemoji struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "add-emoji",
Description: "Add an external emoji directly to the server.",

View File

@@ -5,9 +5,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_ask = Command{
var cmd_ask = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "ask",
Description: "Ask anything and get a gif as response!",

View File

@@ -5,9 +5,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_autojoinroles Command = Command{
var cmd_autojoinroles struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "autojoinroles",
Description: "Give users a role when they join",

View File

@@ -5,9 +5,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_autopublish Command = Command{
var cmd_autopublish struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "autopublish",
Description: "Toggle automatically publishing every post in a announcement channel",

View File

@@ -5,9 +5,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_blockpolls Command = Command{
var cmd_blockpolls struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "block-polls",
Description: "Block polls from beeing posted in this channel.",

View File

@@ -9,9 +9,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_cat = Command{
var cmd_cat = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "cat",
Description: "Random cat pictures",

View File

@@ -4,9 +4,10 @@ import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_dadjoke = Command{
var cmd_dadjoke = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "dadjoke",
Description: "Gives you a random joke that is as bad as your dad would tell them",

View File

@@ -13,9 +13,10 @@ import (
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_form Command = Command{
var cmd_form struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "form",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
@@ -334,7 +335,7 @@ var cmd_form Command = Command{
},
}
var cmd_ticket_form Command = Command{
var cmd_ticket_form struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "ticket",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),

View File

@@ -9,6 +9,7 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
type UserExtend struct {
@@ -39,7 +40,7 @@ var userFlagsString map[discord.UserFlags]string = map[discord.UserFlags]string{
discord.UserFlagActiveDeveloper: "<:Active_Developer:1224708676611215380>[`Active Developer`](https://support-dev.discord.com/hc/en-us/articles/10113997751447?ref=badge)",
}
var cmd_userinfo Command = Command{
var cmd_userinfo struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "info",
Description: "Gives you information about a user or this bot.",

View File

@@ -9,9 +9,10 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_ping Command = Command{
var cmd_ping struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "ping",
Description: "Returns the ping of the bot",

View File

@@ -7,9 +7,10 @@ import (
"github.com/disgoorg/snowflake/v2"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
var cmd_sticky Command = Command{
var cmd_sticky struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "sticky",
Description: "Stick or unstick messages to the bottom of the current channel",
@@ -55,7 +56,7 @@ var cmd_sticky Command = Command{
},
}
var context_sticky Command = Command{
var context_sticky struct_cmd.Command = struct_cmd.Command{
Definition: discord.MessageCommandCreate{
Name: "Stick to channel",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages),

View File

@@ -5,10 +5,11 @@ import (
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/struct_cmd"
)
// TODO: make user installable tag command using userIDs instead of guildIDs
var cmd_tag Command = Command{
var cmd_tag struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "tag",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),
@@ -82,7 +83,7 @@ var cmd_tag Command = Command{
},
}
var cmd_tag_short Command = Command{
var cmd_tag_short struct_cmd.Command = struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "g",
Description: "A short command to get presaved messages.",
@@ -108,7 +109,7 @@ var cmd_tag_short Command = Command{
},
}
var context_tag Command = Command{
var context_tag struct_cmd.Command = struct_cmd.Command{
Definition: discord.MessageCommandCreate{
Name: "Save as tag",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),

5
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/vaporvee/acecore
go 1.21.6
go 1.22.1
require (
github.com/disgoorg/disgo v0.18.2
@@ -15,9 +15,10 @@ require (
require (
github.com/gorilla/websocket v1.5.1 // indirect
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect
github.com/vaporvee/acecore/struct_cmd v0.0.0-00010101000000-000000000000
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)
replace github.com/vaporvee/acecore/custom => ./custom
replace github.com/vaporvee/acecore/struct_cmd => ./struct_cmd

6
go.sum
View File

@@ -1,12 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/disgo v0.18.1-0.20240408224120-2676e29d6e86 h1:hSRIjnKWL07TYxvZRxdSBpS78gJNf+JkAuzXY3O1Kos=
github.com/disgoorg/disgo v0.18.1-0.20240408224120-2676e29d6e86/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/disgo v0.18.1 h1:8g3ifRVlbdZ28bX2BAVkb04dvQnnRMwN89KUkclaniw=
github.com/disgoorg/disgo v0.18.1/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/disgo v0.18.2-0.20240412120702-1c2e34f76c43 h1:ADoIkRUNXQPAuafuA3SZZvxPxWUK5rXSgNTmODXXgoI=
github.com/disgoorg/disgo v0.18.2-0.20240412120702-1c2e34f76c43/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/disgo v0.18.2 h1:pZCvaFamfHcnXrj0XA73qtVofP0R8dYEyQfPNgv8dLE=
github.com/disgoorg/disgo v0.18.2/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=

View File

@@ -3,6 +3,9 @@ package main
import (
"fmt"
"os"
"path/filepath"
"plugin"
"runtime"
"slices"
"strings"
@@ -12,21 +15,10 @@ import (
"github.com/disgoorg/snowflake/v2"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/struct_cmd"
)
type Command struct {
Definition discord.ApplicationCommandCreate
Interact func(e *events.ApplicationCommandInteractionCreate)
Autocomplete func(e *events.AutocompleteInteractionCreate)
ComponentInteract func(e *events.ComponentInteractionCreate)
ModalSubmit func(e *events.ModalSubmitInteractionCreate)
ComponentIDs []string
ModalIDs []string
DynamicModalIDs func() []string
DynamicComponentIDs func() []string
}
var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky, cmd_ping, cmd_userinfo, cmd_addemoji, cmd_form, cmd_ask, cmd_cat, cmd_dadjoke, cmd_ticket_form, cmd_blockpolls, cmd_autopublish, cmd_autojoinroles}
var commands []struct_cmd.Command = []struct_cmd.Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky, cmd_ping, cmd_userinfo, cmd_addemoji, cmd_form, cmd_ask, cmd_cat, cmd_dadjoke, cmd_ticket_form, cmd_blockpolls, cmd_autopublish, cmd_autojoinroles}
func ready(e *events.Ready) {
logrus.Info("Starting up...")
@@ -57,9 +49,65 @@ func ready(e *events.Ready) {
logrus.Infof("Added global commands sucessfully!")
}
}
err = loadPlugins("plugins/", e.Client(), &commands)
if err != nil {
logrus.Error(err)
}
logrus.Info("Successfully started the Bot!")
}
func loadPlugins(directory string, client bot.Client, commands *[]struct_cmd.Command) error {
files, err := os.ReadDir(directory)
if err != nil {
return err
}
// Determine the appropriate file extension for dynamic libraries
var ext string
switch runtime.GOOS {
case "windows":
ext = ".dll"
case "linux":
ext = ".so"
case "darwin":
ext = ".dylib"
default:
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
for _, file := range files {
if filepath.Ext(file.Name()) == ext {
p, err := plugin.Open(filepath.Join(directory, file.Name()))
if err != nil {
return err
}
symPlugin, err := p.Lookup("Plugin")
if err != nil {
logrus.Errorf("Error looking up symbol 'Plugin' in %s: %v", file.Name(), err)
continue
}
pluginPtr, ok := symPlugin.(**struct_cmd.Plugin)
if !ok {
logrus.Errorf("Plugin does not match expected type")
continue
}
plugin := *pluginPtr
err = plugin.Register(client, commands)
if err != nil {
logrus.Errorf("Error registering plugin commands: %v", err)
continue
}
}
}
return nil
}
func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) {
for _, command := range commands {
if command.Interact != nil && e.Data.CommandName() == command.Definition.CommandName() {

BIN
plugins/testplugin.so Normal file

Binary file not shown.

15
struct_cmd/go.mod Normal file
View File

@@ -0,0 +1,15 @@
module struct_cmd
go 1.22.1
require github.com/disgoorg/disgo v0.18.2
require (
github.com/disgoorg/json v1.1.0 // indirect
github.com/disgoorg/snowflake/v2 v2.0.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)

24
struct_cmd/go.sum Normal file
View File

@@ -0,0 +1,24 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/disgo v0.18.2 h1:pZCvaFamfHcnXrj0XA73qtVofP0R8dYEyQfPNgv8dLE=
github.com/disgoorg/disgo v0.18.2/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad h1:qIQkSlF5vAUHxEmTbaqt1hkJ/t6skqEGYiMag343ucI=
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

23
struct_cmd/main.go Normal file
View File

@@ -0,0 +1,23 @@
package struct_cmd
import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
)
type Command struct {
Definition discord.ApplicationCommandCreate
Interact func(e *events.ApplicationCommandInteractionCreate)
Autocomplete func(e *events.AutocompleteInteractionCreate)
ComponentInteract func(e *events.ComponentInteractionCreate)
ModalSubmit func(e *events.ModalSubmitInteractionCreate)
ComponentIDs []string
ModalIDs []string
DynamicModalIDs func() []string
DynamicComponentIDs func() []string
}
type Plugin struct {
Register func(client bot.Client, commands *[]Command) error
}

23
testplugin/main.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/vaporvee/acecore/struct_cmd"
)
var Plugin = &struct_cmd.Plugin{
Register: func(client bot.Client, commands *[]struct_cmd.Command) error {
*commands = append(*commands, struct_cmd.Command{
Definition: discord.SlashCommandCreate{
Name: "TESTPLUGIN",
Description: "TESTPLUGIN",
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("TEST").SetEphemeral(true).Build())
},
})
return nil
},
}

View File

@@ -43,7 +43,7 @@
services.
</li>
<li>
Command usage: We store data about some commands that you use within
struct_cmd.Command usage: We store data about some commands that you use within
acecore in order to make the commands usable and functioning.
</li>
<li>

View File

@@ -41,10 +41,10 @@ func HostRoutes(botID string) {
http.Redirect(w, r, "https://discord.com/oauth2/authorize?client_id="+botID, http.StatusMovedPermanently)
})
http.HandleFunc("/privacy", func(w http.ResponseWriter, r *http.Request) {
handleHTML(w, privacyHTML, "./html/privacy.html")
handleHTML(w, privacyHTML, "html/privacy.html")
})
http.HandleFunc("/tos", func(w http.ResponseWriter, r *http.Request) {
handleHTML(w, tosHTML, "./html/tos.html")
handleHTML(w, tosHTML, "html/tos.html")
})
server := &http.Server{