From 112d3ce4dd67700c487f7bdf68874782c738d0e0 Mon Sep 17 00:00:00 2001 From: Arkitu Date: Sun, 18 Jan 2026 12:49:03 +0100 Subject: [PATCH] sub get guess --- main.go | 111 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 40 deletions(-) diff --git a/main.go b/main.go index 33511d5..7707545 100644 --- a/main.go +++ b/main.go @@ -27,41 +27,68 @@ func init() { } } -type Command struct { - desc *dg.ApplicationCommand - handler func(*dg.Session, *dg.InteractionCreate) -} - -var cmds = map[string]Command{ - "ping": { - desc: &dg.ApplicationCommand{ - Name: "ping", - Description: "Replies with pong", - DescriptionLocalizations: &map[dg.Locale]string{ - dg.French: "Répond pong", - }, - }, - handler: func(s *dg.Session, i *dg.InteractionCreate) { - s.InteractionRespond(i.Interaction, &dg.InteractionResponse{ - Type: dg.InteractionResponseChannelMessageWithSource, - Data: &dg.InteractionResponseData{ - Content: ":ping_pong: Pong !", - }, - }) +var descs = map[string]*dg.ApplicationCommand{ + "ping": &dg.ApplicationCommand{ + Name: "ping", + Description: "Replies with pong", + DescriptionLocalizations: &map[dg.Locale]string{ + dg.French: "Répond pong", }, }, - "sub": { - desc: &dg.ApplicationCommand{ - Name: "sub", - Description: "Submit a guess", - DescriptionLocalizations: &map[dg.Locale]string{ - dg.French: "Envoie un essais pour deviner", - }, - Options: []*dg.ApplicationCommandOption{}, + "sub": &dg.ApplicationCommand{ + Name: "sub", + Description: "Submit a guess", + DescriptionLocalizations: &map[dg.Locale]string{ + dg.French: "Envoie un essais pour deviner", }, - handler: func(s *dg.Session, i *dg.InteractionCreate) { + // /!\ Make sure to update handler if other options are added + // Guess options are added in init function + // It's assumed in handler that there aren't other options + Options: []*dg.ApplicationCommandOption{}, + }, +} +func send_error(s *dg.Session, i *dg.Interaction, reason string) { + s.InteractionRespond(i, &dg.InteractionResponse{ + Type: dg.InteractionResponseChannelMessageWithSource, + Data: &dg.InteractionResponseData{ + Embeds: []*dg.MessageEmbed{ + { + Title: "⚠️ Something went wrong", + Description: reason, + Color: 0xFF0000, + }, + }, }, + }) +} + +// Handlers are separated because we need to be able to access descs +var handlers = map[string]func(*dg.Session, *dg.InteractionCreate){ + "ping": func(s *dg.Session, i *dg.InteractionCreate) { + s.InteractionRespond(i.Interaction, &dg.InteractionResponse{ + Type: dg.InteractionResponseChannelMessageWithSource, + Data: &dg.InteractionResponseData{ + Content: ":ping_pong: Pong !", + }, + }) + }, + "sub": func(s *dg.Session, i *dg.InteractionCreate) { + // red := 0 + // green := 0 + for _, opt := range descs["sub"].Options { + val := i.ApplicationCommandData().GetOption(opt.Name) + if val == nil { + send_error(s, i.Interaction, "Missing option") + return + } + if val.Type != dg.ApplicationCommandOptionUser { + send_error(s, i.Interaction, "Invalid option type") + return + } + // guess := val.Value.(string) + + } }, } @@ -86,17 +113,11 @@ func init() { } else { opt.Name = r[0] + "_" + duplicateNames[i-1] } - cmds["sub"].desc.Options = append(cmds["sub"].desc.Options, &opt) + descs["sub"].Options = append(descs["sub"].Options, &opt) } } } -func interactionHandler(s *dg.Session, i *dg.InteractionCreate) { - if cmd, ok := cmds[i.ApplicationCommandData().Name]; ok { - cmd.handler(s, i) - } -} - func main() { s, _ := dg.New("Bot " + stg.Token) s.AddHandler(func(s *dg.Session, r *dg.Ready) { @@ -106,11 +127,21 @@ func main() { if err != nil { log.Panicf("Failed to create session : %s", err) } - s.AddHandler(interactionHandler) + s.AddHandler(func(s *dg.Session, i *dg.InteractionCreate) { + if i.Type != dg.InteractionApplicationCommand { + log.Printf("Warning : Received unknown interaction type (%s)", i.Type.String()) + } + if h, ok := handlers[i.ApplicationCommandData().Name]; ok { + h(s, i) + log.Printf("Reveived command \"%s\"", i.ApplicationCommandData().Name) + } else { + log.Printf("Warning : Received unknown command \"%s\" (id:%s)", i.ApplicationCommandData().Name, i.ApplicationCommandData().ID) + } + }) // Create commands - for _, cmd := range cmds { - if _, err := s.ApplicationCommandCreate(s.State.User.ID, stg.Server, cmd.desc); err != nil { + for _, d := range descs { + if _, err := s.ApplicationCommandCreate(s.State.User.ID, stg.Server, d); err != nil { log.Panicf("Failed to create command : %s", err) } }