feat(commands): send command data alongside interaction data

This commit is contained in:
Guz
2024-11-19 10:55:24 -03:00
parent e5f7c26a6c
commit 507a9127ad
3 changed files with 19 additions and 13 deletions

View File

@@ -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),

View File

@@ -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

View File

@@ -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{