From 2ee70560dc1002403e6ef3a90dcac276078c1a6d Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Mon, 18 Nov 2024 17:16:37 -0300 Subject: [PATCH] refactor(commands,interactions): move interactions handling to dedicated function and file --- commands/commands.go | 50 +++-------------------------- commands/interaction.go | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 commands/interaction.go diff --git a/commands/commands.go b/commands/commands.go index b3bdba5..bb9b11d 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -10,11 +10,11 @@ import ( type Command interface { Info() *discordgo.ApplicationCommand - Handle(s *discordgo.Session, i *discordgo.InteractionCreate) error + Handle(s *discordgo.Session, ic *discordgo.InteractionCreate) error } type ( - CommandFunc = func(s *discordgo.Session, i *discordgo.InteractionCreate) error + CommandFunc = func(s *discordgo.Session, ic *discordgo.InteractionCreate) error CommandName = string CommandId = string ) @@ -95,50 +95,8 @@ func (h *CommandsHandler) UpdateCommands( handleFuncs[appCmd.Name] = cmd.Handle } - h.session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { - var userID string - if i.User != nil { - userID = i.User.ID - } else { - userID = i.Member.User.ID - } - - if i.Type == discordgo.InteractionApplicationCommand { - data := i.ApplicationCommandData() - - if hf, ok := handleFuncs[data.Name]; ok { - h.logger.Debug("Handling application command.", - slog.String("command_data_id", data.ID), - slog.String("command_data_name", data.Name), - slog.String("interaction_user_id", userID), - slog.String("interaction_guild_id", i.GuildID), - ) - if err := hf(s, i); err != nil { - h.logger.Error("Failed to run command, error returned.", - slog.String("command_data_id", data.ID), - slog.String("command_data_name", data.Name), - slog.String("interaction_user_id", userID), - slog.String("interaction_guild_id", i.GuildID), - slog.String("error", err.Error()), - ) - } - - } else { - h.logger.Error("Application command interaction created without having a handler.", - slog.String("command_data_id", data.ID), - slog.String("command_data_name", data.Name), - slog.String("interaction_user_id", userID), - slog.String("interaction_guild_id", i.GuildID), - ) - } - } else { - h.logger.Error("Application interaction created without being a command.", - slog.String("interaction_id", i.ID), - slog.String("interaction_type", i.Type.String()), - slog.String("interaction_user_id", userID), - slog.String("interaction_guild_id", i.GuildID), - ) - } + h.session.AddHandler(func(s *discordgo.Session, ic *discordgo.InteractionCreate) { + h.handleInteraction(handleFuncs, s, ic) }) return nil diff --git a/commands/interaction.go b/commands/interaction.go new file mode 100644 index 0000000..22c78dc --- /dev/null +++ b/commands/interaction.go @@ -0,0 +1,71 @@ +package commands + +import ( + "log/slog" + + "github.com/bwmarrin/discordgo" +) + +func (h *CommandsHandler) handleInteraction( + cmdsFunc map[CommandName]CommandFunc, + s *discordgo.Session, + ic *discordgo.InteractionCreate, +) { + var userID string + if ic.User != nil { + userID = ic.User.ID + } else { + userID = ic.Member.User.ID + } + + switch ic.Type { + case discordgo.InteractionApplicationCommand: + h.handleCommand(cmdsFunc, s, ic) + case discordgo.InteractionMessageComponent: + // TODO! + default: + h.logger.Error("Application interaction is not a supported type!", + slog.String("interaction_id", ic.ID), + slog.String("interaction_type", ic.Type.String()), + slog.String("interaction_user_id", userID), + slog.String("interaction_guild_id", ic.GuildID), + ) + } +} + +func (h *CommandsHandler) handleCommand( + cmdsFunc map[CommandName]CommandFunc, + s *discordgo.Session, + ic *discordgo.InteractionCreate, +) { + var userID string + if ic.User != nil { + userID = ic.User.ID + } else { + userID = ic.Member.User.ID + } + + data := ic.ApplicationCommandData() + + log := h.logger.With( + slog.String("command_data_id", data.ID), + slog.String("command_data_name", data.Name), + slog.String("interaction_user_id", userID), + slog.String("interaction_guild_id", ic.GuildID), + ) + + if hf, ok := cmdsFunc[data.Name]; ok { + log.Debug("Handling application command.") + + if err := hf(s, ic); err != nil { + log.Error("Failed to run command, error returned.", + slog.String("error", err.Error()), + ) + } else { + log.Debug("Command ran successfully.") + } + + } else { + log.Error("Application command interaction created without having a handler.") + } +}