refactor(bot,commands): new command handler
This commit is contained in:
@@ -1,52 +1,30 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"dislate/internals/translator/lang"
|
||||
"dislate/internals/discord/bot/commands"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
dgo "github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type command struct {
|
||||
command *discordgo.ApplicationCommand
|
||||
handler func(*discordgo.Session, *discordgo.InteractionCreate)
|
||||
}
|
||||
|
||||
func (b *Bot) commands() []command {
|
||||
cmds := []command{{
|
||||
&discordgo.ApplicationCommand{
|
||||
Name: "test-command",
|
||||
Description: "This is a test command",
|
||||
},
|
||||
func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
txt, _ := b.translator.Translate(lang.EN, lang.PT, "Hello world!")
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: txt,
|
||||
},
|
||||
})
|
||||
},
|
||||
}}
|
||||
return cmds
|
||||
}
|
||||
|
||||
func (b *Bot) registerCommands() error {
|
||||
cs := b.commands()
|
||||
rcs := make([]*discordgo.ApplicationCommand, len(cs))
|
||||
handlers := make(map[string]func(*discordgo.Session, *discordgo.InteractionCreate), len(cs))
|
||||
cs := []commands.Command{
|
||||
commands.NewTest(b.translator),
|
||||
}
|
||||
|
||||
rcs := make([]*dgo.ApplicationCommand, len(cs))
|
||||
handlers := make(map[string]func(*dgo.Session, *dgo.InteractionCreate), len(cs))
|
||||
|
||||
for i, v := range cs {
|
||||
cmd, err := b.session.ApplicationCommandCreate(b.session.State.User.ID, "", v.command)
|
||||
cmd, err := b.session.ApplicationCommandCreate(b.session.State.User.ID, "", v.Info())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handlers[cmd.Name] = v.handler
|
||||
handlers[cmd.Name] = v.Handle
|
||||
rcs[i] = cmd
|
||||
}
|
||||
|
||||
b.session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
b.session.AddHandler(func(s *dgo.Session, i *dgo.InteractionCreate) {
|
||||
if h, ok := handlers[i.ApplicationCommandData().Name]; ok {
|
||||
h(s, i)
|
||||
}
|
||||
|
||||
37
internals/discord/bot/commands/channels.go
Normal file
37
internals/discord/bot/commands/channels.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"dislate/internals/translator"
|
||||
"dislate/internals/translator/lang"
|
||||
|
||||
dgo "github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type Command interface {
|
||||
Info() *dgo.ApplicationCommand
|
||||
Handle(s *dgo.Session, i *dgo.InteractionCreate)
|
||||
}
|
||||
|
||||
type Test struct {
|
||||
translator translator.Translator
|
||||
}
|
||||
|
||||
func NewTest(t translator.Translator) Test {
|
||||
return Test{t}
|
||||
}
|
||||
func (c Test) Info() *dgo.ApplicationCommand {
|
||||
return &dgo.ApplicationCommand{
|
||||
Name: "test-command",
|
||||
Description: "This is a test command",
|
||||
}
|
||||
}
|
||||
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{
|
||||
Type: dgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &dgo.InteractionResponseData{
|
||||
Content: txt,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -21,5 +21,5 @@ func NewMessageCreate(log *slog.Logger, db guilddb.GuildDB) MessageCreate {
|
||||
return MessageCreate{log, db}
|
||||
}
|
||||
func (h MessageCreate) Serve(s *dgo.Session, e *dgo.MessageCreate) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user