feat(bot,commands): error handling of commands
This commit is contained in:
@@ -9,12 +9,11 @@ import (
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
token string
|
||||
db guilddb.GuildDB
|
||||
translator translator.Translator
|
||||
session *discordgo.Session
|
||||
logger *slog.Logger
|
||||
registeredCommands []*discordgo.ApplicationCommand
|
||||
token string
|
||||
db guilddb.GuildDB
|
||||
translator translator.Translator
|
||||
session *discordgo.Session
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewBot(token string, db guilddb.GuildDB, translator translator.Translator, logger *slog.Logger) (*Bot, error) {
|
||||
@@ -24,12 +23,11 @@ func NewBot(token string, db guilddb.GuildDB, translator translator.Translator,
|
||||
}
|
||||
|
||||
return &Bot{
|
||||
token: token,
|
||||
db: db,
|
||||
translator: translator,
|
||||
session: discord,
|
||||
logger: logger,
|
||||
registeredCommands: make([]*discordgo.ApplicationCommand, 0),
|
||||
token: token,
|
||||
db: db,
|
||||
translator: translator,
|
||||
session: discord,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,15 @@ package bot
|
||||
|
||||
import (
|
||||
"dislate/internals/discord/bot/commands"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
dgo "github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) registerCommands() error {
|
||||
cs := []commands.Command{
|
||||
commands.NewTest(b.translator),
|
||||
commands.NewManageChannel(b.db),
|
||||
}
|
||||
|
||||
rcs := make([]*dgo.ApplicationCommand, len(cs))
|
||||
@@ -20,8 +22,29 @@ func (b *Bot) registerCommands() error {
|
||||
return err
|
||||
}
|
||||
|
||||
handlers[cmd.Name] = v.Handle
|
||||
handlers[cmd.Name] = func(s *dgo.Session, ic *dgo.InteractionCreate) {
|
||||
err := v.Handle(s, ic)
|
||||
if err != nil {
|
||||
_ = s.InteractionRespond(ic.Interaction, &dgo.InteractionResponse{
|
||||
Type: dgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||
Data: &dgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Error while trying to handle command: %s", err.Error()),
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
b.logger.Error("Failed to handle command",
|
||||
slog.String("name", cmd.Name),
|
||||
slog.String("id", cmd.ID),
|
||||
slog.String("err", err.Error()),
|
||||
)
|
||||
}
|
||||
}
|
||||
rcs[i] = cmd
|
||||
|
||||
b.logger.Info("Registered command",
|
||||
slog.String("name", cmd.Name),
|
||||
slog.String("id", cmd.ID),
|
||||
)
|
||||
}
|
||||
|
||||
b.session.AddHandler(func(s *dgo.Session, i *dgo.InteractionCreate) {
|
||||
@@ -34,11 +57,20 @@ func (b *Bot) registerCommands() error {
|
||||
}
|
||||
|
||||
func (b *Bot) removeCommands() error {
|
||||
for _, v := range b.registeredCommands {
|
||||
cmds, err := b.session.ApplicationCommands(b.session.State.Application.ID, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, v := range cmds {
|
||||
err := b.session.ApplicationCommandDelete(b.session.State.User.ID, "", v.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.logger.Info("Removed command",
|
||||
slog.String("name", v.Name),
|
||||
slog.String("id", v.ID),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,37 +1,49 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"dislate/internals/translator"
|
||||
"dislate/internals/translator/lang"
|
||||
"dislate/internals/guilddb"
|
||||
|
||||
dgo "github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type Command interface {
|
||||
Info() *dgo.ApplicationCommand
|
||||
Handle(s *dgo.Session, i *dgo.InteractionCreate)
|
||||
Handle(s *dgo.Session, i *dgo.InteractionCreate) error
|
||||
}
|
||||
|
||||
type Test struct {
|
||||
translator translator.Translator
|
||||
type ManageChannel struct {
|
||||
db guilddb.GuildDB
|
||||
}
|
||||
|
||||
func NewTest(t translator.Translator) Test {
|
||||
return Test{t}
|
||||
func NewManageChannel(db guilddb.GuildDB) ManageChannel {
|
||||
return ManageChannel{db}
|
||||
}
|
||||
func (c Test) Info() *dgo.ApplicationCommand {
|
||||
func (c ManageChannel) Info() *dgo.ApplicationCommand {
|
||||
return &dgo.ApplicationCommand{
|
||||
Name: "test-command",
|
||||
Description: "This is a test command",
|
||||
Name: "channel",
|
||||
Description: "Manages a channel options",
|
||||
Options: []*dgo.ApplicationCommandOption{{
|
||||
Type: dgo.ApplicationCommandOptionChannel,
|
||||
Name: "channel",
|
||||
Description: "The channel to manage",
|
||||
ChannelTypes: []dgo.ChannelType{
|
||||
dgo.ChannelTypeGuildText,
|
||||
},
|
||||
}},
|
||||
}
|
||||
}
|
||||
func (c Test) Handle(s *dgo.Session, i *dgo.InteractionCreate) {
|
||||
txt, _ := c.translator.Translate(lang.EN, lang.PT, "Hello world!")
|
||||
|
||||
_ = s.InteractionRespond(i.Interaction, &dgo.InteractionResponse{
|
||||
func (c ManageChannel) Handle(s *dgo.Session, i *dgo.InteractionCreate) error {
|
||||
err := s.InteractionRespond(i.Interaction, &dgo.InteractionResponse{
|
||||
Type: dgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &dgo.InteractionResponseData{
|
||||
Content: txt,
|
||||
Content: "Hello world!",
|
||||
Flags: dgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user