diff --git a/app/component_loader.go b/app/component_loader.go index 5263995..476ef1e 100644 --- a/app/component_loader.go +++ b/app/component_loader.go @@ -62,3 +62,7 @@ func (loader *ComponentLoader) Notify(message string) error { loader.bot.Notify(message) return nil } + +func (loader *ComponentLoader) RegisterCommand(name string, config common.ChatCommandConfiguration, handler func(common.User, map[string]any) string) { + loader.bot.Session.RegisterCommand(name, config, handler) +} diff --git a/common/chat_commands.go b/common/chat_commands.go new file mode 100644 index 0000000..1911396 --- /dev/null +++ b/common/chat_commands.go @@ -0,0 +1,24 @@ +package common + +import "github.com/bwmarrin/discordgo" + +type CommandOptionType uint64 + +const ( + CommandTypeString CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionString) + CommandTypeInt CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionInteger) + CommandTypeBool CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionBoolean) + CommandTypeFloat CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionNumber) +) + +type ChatCommandConfiguration struct { + Description string + EphemeralResponse bool + Options map[string]ChatCommandOption +} + +type ChatCommandOption struct { + Description string + Type CommandOptionType + Required bool +} diff --git a/common/module.go b/common/module.go index a2de761..caed7a8 100644 --- a/common/module.go +++ b/common/module.go @@ -21,5 +21,8 @@ type ModuleManager interface { CreateEvent(event Event) error Notify(message string) error + // Commands + RegisterCommand(string, ChatCommandConfiguration, func(User, map[string]any) string) + RegisterChatSyncModule(ID string, plugin ChatSyncModule) error } diff --git a/discord/command.go b/discord/command.go index 0e0fe4c..e7a6f19 100644 --- a/discord/command.go +++ b/discord/command.go @@ -7,29 +7,8 @@ import ( "github.com/yeslayla/birdbot/common" ) -type CommandConfiguration struct { - Description string - EphemeralResponse bool - Options map[string]CommandOption -} - -type CommandOption struct { - Description string - Type CommandOptionType - Required bool -} - -type CommandOptionType uint64 - -const ( - CommandTypeString CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionString) - CommandTypeInt CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionInteger) - CommandTypeBool CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionBoolean) - CommandTypeFloat CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionNumber) -) - // RegisterCommand creates an new command that can be used to interact with bird bot -func (discord *Discord) RegisterCommand(name string, config CommandConfiguration, handler func(common.User, map[string]any) string) { +func (discord *Discord) RegisterCommand(name string, config common.ChatCommandConfiguration, handler func(common.User, map[string]any) string) { command := &discordgo.ApplicationCommand{ Name: name, Description: config.Description, @@ -60,13 +39,13 @@ func (discord *Discord) RegisterCommand(name string, config CommandConfiguration optionsMap := make(map[string]any, len(cmdOptions)) for _, opt := range cmdOptions { switch config.Options[opt.Name].Type { - case CommandTypeString: + case common.CommandTypeString: optionsMap[opt.Name] = opt.StringValue() - case CommandTypeInt: + case common.CommandTypeInt: optionsMap[opt.Name] = opt.IntValue() - case CommandTypeBool: + case common.CommandTypeBool: optionsMap[opt.Name] = opt.BoolValue() - case CommandTypeFloat: + case common.CommandTypeFloat: optionsMap[opt.Name] = opt.FloatValue() default: optionsMap[opt.Name] = opt.Value diff --git a/main.go b/main.go index e45d14e..98c59dc 100644 --- a/main.go +++ b/main.go @@ -89,7 +89,7 @@ func main() { SuccessMessage: cfg.Feedback.SuccessMessage, FailureMessage: cfg.Feedback.FailureMessage, - }, bot.Session)) + })) } if _, err := os.Stat(PluginsDirectory); !os.IsNotExist(err) { diff --git a/modules/feedback_webhook.go b/modules/feedback_webhook.go index d3445b2..31198e8 100644 --- a/modules/feedback_webhook.go +++ b/modules/feedback_webhook.go @@ -9,11 +9,9 @@ import ( "net/http" "github.com/yeslayla/birdbot/common" - "github.com/yeslayla/birdbot/discord" ) type feedbackWebhookModule struct { - session *discord.Discord webhookURL string payloadType string successMessage string @@ -27,9 +25,8 @@ type FeedbackWebhookConfiguration struct { } // NewFeedbackWebhookComponent creates a new component -func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfiguration, session *discord.Discord) common.Module { +func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfiguration) common.Module { m := &feedbackWebhookModule{ - session: session, webhookURL: webhookURL, payloadType: "default", successMessage: "Feedback recieved!", @@ -50,13 +47,13 @@ func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfig } func (c *feedbackWebhookModule) Initialize(birdbot common.ModuleManager) error { - c.session.RegisterCommand("feedback", discord.CommandConfiguration{ + birdbot.RegisterCommand("feedback", common.ChatCommandConfiguration{ Description: "Sends a feedback message", EphemeralResponse: true, - Options: map[string]discord.CommandOption{ + Options: map[string]common.ChatCommandOption{ "message": { Description: "Content of what you'd like to communicate in your feedback.", - Type: discord.CommandTypeString, + Type: common.CommandTypeString, Required: true, }, },