From 3dd9e332de03c2712b7b45494d327f6402b22d96 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Mon, 18 Nov 2024 10:04:01 -0300 Subject: [PATCH] feat(commands,bot): mock command for testing --- bot/bot.go | 10 +++++++--- bot/commands.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 bot/commands.go diff --git a/bot/bot.go b/bot/bot.go index 07af1f7..7a97160 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -2,6 +2,7 @@ package bot import ( "database/sql" + "errors" "log/slog" "forge.capytal.company/capytal/dislate/commands" @@ -50,9 +51,12 @@ func (b *Bot) Start() error { ch := commands.NewCommandsHandler(b.logger, b.session) - // TODO: add real commands - if err := ch.UpdateCommands(make(map[string]commands.Command)); err != nil { - return err + COMMANDS := []commands.Command{ + &mockCommand{}, + } + + if err := ch.UpdateCommands(COMMANDS); err != nil { + return errors.Join(errors.New("Failed to update commands"), err) } return nil diff --git a/bot/commands.go b/bot/commands.go new file mode 100644 index 0000000..de2a808 --- /dev/null +++ b/bot/commands.go @@ -0,0 +1,44 @@ +package bot + +import ( + "errors" + "fmt" + + "github.com/bwmarrin/discordgo" +) + +type mockCommand struct{} + +func (c *mockCommand) Info() *discordgo.ApplicationCommand { + return &discordgo.ApplicationCommand{ + Name: "mock", + Type: discordgo.ChatApplicationCommand, + Description: "this is a mock command used for testing", + Options: []*discordgo.ApplicationCommandOption{{ + Name: "message", + Description: "The message to respond", + Type: discordgo.ApplicationCommandOptionString, + }}, + } +} + +func (c *mockCommand) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) error { + data := i.ApplicationCommandData() + + if len(data.Options) == 0 { + return errors.New("Option \"message\" is not defined") + } + + text, ok := data.Options[0].Value.(string) + if !ok { + return errors.New("Failed to convert \"message\" into string") + } + + return s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: fmt.Sprintf("Hello user! Your text is %q", text), + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +}