added basic plugin system

This commit is contained in:
2024-04-14 20:56:05 +02:00
parent bc4ada155b
commit 8600bda9bc
5 changed files with 35 additions and 22 deletions

5
.gitignore vendored
View File

@@ -1,5 +1,4 @@
.env
data.json
# Building
acecore
@@ -9,6 +8,8 @@ acecore
logs/
# Web
web/key.pem
web/cert.pem
# Custom plugins
plugins/

View File

@@ -24,6 +24,10 @@ func ready(e *events.Ready) {
logrus.Info("Starting up...")
findAndDeleteUnusedMessages(e.Client())
removeOldCommandFromAllGuilds(e.Client())
err := loadPlugins("plugins/", e)
if err != nil {
logrus.Error(err)
}
var existingCommandNames []string
existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false)
if err != nil {
@@ -49,14 +53,10 @@ 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 {
func loadPlugins(directory string, e *events.Ready) error {
files, err := os.ReadDir(directory)
if err != nil {
return err
@@ -95,12 +95,24 @@ func loadPlugins(directory string, client bot.Client, commands *[]struct_cmd.Com
}
plugin := *pluginPtr
err = plugin.Register(client, commands)
if err != nil {
logrus.Errorf("Error registering plugin commands: %v", err)
if plugin.Name == "" {
logrus.Warn("Plugin is unnamed")
}
if plugin.Commands != nil {
commands = append(commands, plugin.Commands...)
} else {
logrus.Errorf("Plugin %s has no commands set", plugin.Name)
continue
}
if plugin.Register != nil {
err = plugin.Register(e)
if err == nil {
logrus.Infof("Successfully appended plugin %s for registration", plugin.Name)
} else {
logrus.Errorf("Error registering plugin %s commands: %v", plugin.Name, err)
continue
}
}
}
}

Binary file not shown.

View File

@@ -1,7 +1,6 @@
package struct_cmd
import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
)
@@ -19,5 +18,7 @@ type Command struct {
}
type Plugin struct {
Register func(client bot.Client, commands *[]Command) error
Name string
Commands []Command
Register func(e *events.Ready) error
}

View File

@@ -1,23 +1,22 @@
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{
Name: "testplugin",
Commands: []struct_cmd.Command{
{
Definition: discord.SlashCommandCreate{
Name: "TESTPLUGIN",
Description: "TESTPLUGIN",
Name: "testplugincommand",
Description: "Tesing if plugins work",
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("TEST").SetEphemeral(true).Build())
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("Plugins are working!").SetEphemeral(true).Build())
},
},
})
return nil
},
}