From 191b1108460529dde2e6b077bc27d1dd180f4fff Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 15 Feb 2024 14:09:32 +0100 Subject: [PATCH] got subcommands working --- command_tag.go | 72 ++++++++++++++++++++++++++------------------------ main.go | 2 +- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/command_tag.go b/command_tag.go index eaeb6b4..f58c9f5 100644 --- a/command_tag.go +++ b/command_tag.go @@ -8,46 +8,50 @@ import ( var tag_command Command = Command{ Definition: discordgo.ApplicationCommand{ - Name: "get", - Description: "A command to get messages saved to the bot.", + Name: "tag", + Description: "A command to show and edit saved presaved messages.", Options: []*discordgo.ApplicationCommandOption{ { - Type: discordgo.ApplicationCommandOptionString, - Name: "tag", - Description: "Your predefined tag for the saved message", - Required: true, - Autocomplete: true, + Name: "get", + Description: "A command to get messages saved to the bot.", + Type: discordgo.ApplicationCommandOptionSubCommand, + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "tag", + Description: "Your predefined tag for the saved message", + Required: true, + Autocomplete: true, + }, + }, }, }, - }, -} + }} func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { - if i.Type == discordgo.InteractionApplicationCommandAutocomplete { - commandUseCount++ - choices := generateDynamicChoices(commandUseCount) - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionApplicationCommandAutocompleteResult, - Data: &discordgo.InteractionResponseData{ - Choices: choices, - }, - }) - } - if i.Type == discordgo.InteractionApplicationCommand { - if len(i.ApplicationCommandData().Options) > 0 { - // Loop through the options and handle them - for _, option := range i.ApplicationCommandData().Options { - switch option.Name { - case "tag": - value := option.Value.(string) - response := fmt.Sprintf("You provided the tag: %s", value) - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: response, - }, - }) - } + switch i.ApplicationCommandData().Options[0].Name { + case "get": + if i.Type == discordgo.InteractionApplicationCommandAutocomplete { + commandUseCount++ + choices := generateDynamicChoices(commandUseCount) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Data: &discordgo.InteractionResponseData{ + Choices: choices, + }, + }) + } + if i.Type == discordgo.InteractionApplicationCommand { + option := i.ApplicationCommandData().Options[0].Options[0] + if option.Name == "tag" { + value := option.Value.(string) + response := fmt.Sprintf("You provided the tag: %s", value) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: response, + }, + }) } } } diff --git a/main.go b/main.go index d78f316..68accd8 100644 --- a/main.go +++ b/main.go @@ -73,7 +73,7 @@ func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoi var commandUseCount int func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { - if i.ApplicationCommandData().Name == "get" { + if i.ApplicationCommandData().Name == "tag" { tag_command.Interaction(s, i) } }