feat(commands,bot): mock command for testing
This commit is contained in:
10
bot/bot.go
10
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
|
||||
|
||||
44
bot/commands.go
Normal file
44
bot/commands.go
Normal 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,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user