feat(commands,bot): mock command for testing

This commit is contained in:
Guz
2024-11-18 10:04:01 -03:00
parent 84ade64afd
commit 3dd9e332de
2 changed files with 51 additions and 3 deletions

View File

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

44
bot/commands.go Normal file
View File

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