From 507a9127ad0fc67f96be120cf80bce587d4e1fa8 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Tue, 19 Nov 2024 10:55:24 -0300 Subject: [PATCH] feat(commands): send command data alongside interaction data --- bot/commands.go | 10 ++++++---- commands/commands.go | 10 +++++++--- commands/interaction.go | 12 ++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/bot/commands.go b/bot/commands.go index de2a808..581d79b 100644 --- a/bot/commands.go +++ b/bot/commands.go @@ -22,9 +22,11 @@ func (c *mockCommand) Info() *discordgo.ApplicationCommand { } } -func (c *mockCommand) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) error { - data := i.ApplicationCommandData() - +func (c *mockCommand) Handle( + s *discordgo.Session, + ic *discordgo.InteractionCreate, + data discordgo.ApplicationCommandInteractionData, +) error { if len(data.Options) == 0 { return errors.New("Option \"message\" is not defined") } @@ -34,7 +36,7 @@ func (c *mockCommand) Handle(s *discordgo.Session, i *discordgo.InteractionCreat return errors.New("Failed to convert \"message\" into string") } - return s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + return s.InteractionRespond(ic.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: fmt.Sprintf("Hello user! Your text is %q", text), diff --git a/commands/commands.go b/commands/commands.go index 9cb54e5..e5e767a 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -10,13 +10,17 @@ import ( type Command interface { Info() *discordgo.ApplicationCommand - Handle(s *discordgo.Session, ic *discordgo.InteractionCreate) error + Handle( + s *discordgo.Session, + ic *discordgo.InteractionCreate, + data discordgo.ApplicationCommandInteractionData, + ) error } type ( - interactionHandler = func(s *discordgo.Session, ic *discordgo.InteractionCreate) error commandName = string commandId = string + commandHandlerFunc = func(s *discordgo.Session, ic *discordgo.InteractionCreate, data discordgo.ApplicationCommandInteractionData) error ) type CommandsHandler struct { @@ -56,7 +60,7 @@ func (h *CommandsHandler) UpdateCommands( return err } - commandInteractionHandlers := make(map[commandName]interactionHandler, len(commandsMap)) + commandInteractionHandlers := make(map[commandName]commandHandlerFunc, len(commandsMap)) for _, cmd := range commandsMap { var err error diff --git a/commands/interaction.go b/commands/interaction.go index a192136..a2205c0 100644 --- a/commands/interaction.go +++ b/commands/interaction.go @@ -8,7 +8,7 @@ import ( ) func (h *CommandsHandler) handleInteraction( - cmdsFunc map[CommandName]CommandFunc, + cmdsHandlers map[commandName]commandHandlerFunc, s *discordgo.Session, ic *discordgo.InteractionCreate, ) { @@ -21,7 +21,7 @@ func (h *CommandsHandler) handleInteraction( switch ic.Type { case discordgo.InteractionApplicationCommand: - h.handleCommand(cmdsFunc, s, ic) + h.handleCommandInteraction(cmdsHandlers, s, ic) case discordgo.InteractionMessageComponent: // TODO! default: @@ -34,8 +34,8 @@ func (h *CommandsHandler) handleInteraction( } } -func (h *CommandsHandler) handleCommand( - cmdsFunc map[CommandName]CommandFunc, +func (h *CommandsHandler) handleCommandInteraction( + cmdsHandlers map[commandName]commandHandlerFunc, s *discordgo.Session, ic *discordgo.InteractionCreate, ) { @@ -55,10 +55,10 @@ func (h *CommandsHandler) handleCommand( slog.String("interaction_guild_id", ic.GuildID), ) - if hf, ok := cmdsFunc[data.Name]; ok { + if hf, ok := cmdsHandlers[data.Name]; ok { log.Debug("Handling application command.") - if err := hf(s, ic); err != nil { + if err := hf(s, ic, data); err != nil { log.Error("Failed to run command, error returned.", slog.String("error", err.Error())) _, err = s.ChannelMessageSendComplex(ic.ChannelID, &discordgo.MessageSend{