diff --git a/cmd_addemoji.go b/cmd_addemoji.go index 1733a7c..3d682ad 100644 --- a/cmd_addemoji.go +++ b/cmd_addemoji.go @@ -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.", diff --git a/cmd_ask.go b/cmd_ask.go index 794db2c..620fff9 100644 --- a/cmd_ask.go +++ b/cmd_ask.go @@ -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!", diff --git a/cmd_autojoinroles.go b/cmd_autojoinroles.go index 8c1ce09..edee59b 100644 --- a/cmd_autojoinroles.go +++ b/cmd_autojoinroles.go @@ -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", diff --git a/cmd_autopublish.go b/cmd_autopublish.go index 0c6b408..c768916 100644 --- a/cmd_autopublish.go +++ b/cmd_autopublish.go @@ -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", diff --git a/cmd_blockpolls.go b/cmd_blockpolls.go index 32e1c68..58cf352 100644 --- a/cmd_blockpolls.go +++ b/cmd_blockpolls.go @@ -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.", diff --git a/cmd_cat.go b/cmd_cat.go index 1cc36b8..a178c13 100644 --- a/cmd_cat.go +++ b/cmd_cat.go @@ -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", diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go index c0b8dd8..7f9f446 100644 --- a/cmd_dadjoke.go +++ b/cmd_dadjoke.go @@ -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", diff --git a/cmd_form.go b/cmd_form.go index d802993..7b49ab2 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -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), diff --git a/cmd_info.go b/cmd_info.go index fdcd667..f29dd99 100644 --- a/cmd_info.go +++ b/cmd_info.go @@ -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.", diff --git a/cmd_ping.go b/cmd_ping.go index 15e50f6..0fcbbe7 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -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", diff --git a/cmd_sticky.go b/cmd_sticky.go index ab8ef7b..b6c10e0 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -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), diff --git a/cmd_tag.go b/cmd_tag.go index c33c598..c3447af 100644 --- a/cmd_tag.go +++ b/cmd_tag.go @@ -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), diff --git a/go.mod b/go.mod index 92fe977..19943b8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 77fc0a2..a0ec24e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/handlers.go b/handlers.go index d0f9f9c..5e2d4cc 100644 --- a/handlers.go +++ b/handlers.go @@ -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() { diff --git a/plugins/testplugin.so b/plugins/testplugin.so new file mode 100644 index 0000000..90a30c0 Binary files /dev/null and b/plugins/testplugin.so differ diff --git a/struct_cmd/go.mod b/struct_cmd/go.mod new file mode 100644 index 0000000..5b621ee --- /dev/null +++ b/struct_cmd/go.mod @@ -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 +) diff --git a/struct_cmd/go.sum b/struct_cmd/go.sum new file mode 100644 index 0000000..27287ae --- /dev/null +++ b/struct_cmd/go.sum @@ -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= diff --git a/struct_cmd/main.go b/struct_cmd/main.go new file mode 100644 index 0000000..2185f01 --- /dev/null +++ b/struct_cmd/main.go @@ -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 +} diff --git a/testplugin/main.go b/testplugin/main.go new file mode 100644 index 0000000..8ef59ca --- /dev/null +++ b/testplugin/main.go @@ -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 + }, +} diff --git a/web/html/privacy.html b/web/html/privacy.html index 5930b75..d2387e4 100644 --- a/web/html/privacy.html +++ b/web/html/privacy.html @@ -43,7 +43,7 @@ services.