refactor(commands,interactions): move interactions handling to dedicated function and file
This commit is contained in:
@@ -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
|
||||
|
||||
71
commands/interaction.go
Normal file
71
commands/interaction.go
Normal file
@@ -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.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user