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

7
.gitignore vendored
View File

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

View File

@@ -24,6 +24,10 @@ func ready(e *events.Ready) {
logrus.Info("Starting up...") logrus.Info("Starting up...")
findAndDeleteUnusedMessages(e.Client()) findAndDeleteUnusedMessages(e.Client())
removeOldCommandFromAllGuilds(e.Client()) removeOldCommandFromAllGuilds(e.Client())
err := loadPlugins("plugins/", e)
if err != nil {
logrus.Error(err)
}
var existingCommandNames []string var existingCommandNames []string
existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false) existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false)
if err != nil { if err != nil {
@@ -49,14 +53,10 @@ func ready(e *events.Ready) {
logrus.Infof("Added global commands sucessfully!") logrus.Infof("Added global commands sucessfully!")
} }
} }
err = loadPlugins("plugins/", e.Client(), &commands)
if err != nil {
logrus.Error(err)
}
logrus.Info("Successfully started the Bot!") 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) files, err := os.ReadDir(directory)
if err != nil { if err != nil {
return err return err
@@ -95,12 +95,24 @@ func loadPlugins(directory string, client bot.Client, commands *[]struct_cmd.Com
} }
plugin := *pluginPtr plugin := *pluginPtr
if plugin.Name == "" {
err = plugin.Register(client, commands) logrus.Warn("Plugin is unnamed")
if err != nil { }
logrus.Errorf("Error registering plugin commands: %v", err) if plugin.Commands != nil {
commands = append(commands, plugin.Commands...)
} else {
logrus.Errorf("Plugin %s has no commands set", plugin.Name)
continue 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 package struct_cmd
import ( import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/events"
) )
@@ -19,5 +18,7 @@ type Command struct {
} }
type Plugin 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 package main
import ( import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/events"
"github.com/vaporvee/acecore/struct_cmd" "github.com/vaporvee/acecore/struct_cmd"
) )
var Plugin = &struct_cmd.Plugin{ var Plugin = &struct_cmd.Plugin{
Register: func(client bot.Client, commands *[]struct_cmd.Command) error { Name: "testplugin",
*commands = append(*commands, struct_cmd.Command{ Commands: []struct_cmd.Command{
{
Definition: discord.SlashCommandCreate{ Definition: discord.SlashCommandCreate{
Name: "TESTPLUGIN", Name: "testplugincommand",
Description: "TESTPLUGIN", Description: "Tesing if plugins work",
}, },
Interact: func(e *events.ApplicationCommandInteractionCreate) { 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
}, },
} }