got subcommands working

This commit is contained in:
2024-02-15 14:09:32 +01:00
parent 4eb48cbfe1
commit 191b110846
2 changed files with 39 additions and 35 deletions

View File

@@ -8,46 +8,50 @@ import (
var tag_command Command = Command{ var tag_command Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "get", Name: "tag",
Description: "A command to get messages saved to the bot.", Description: "A command to show and edit saved presaved messages.",
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionString, Name: "get",
Name: "tag", Description: "A command to get messages saved to the bot.",
Description: "Your predefined tag for the saved message", Type: discordgo.ApplicationCommandOptionSubCommand,
Required: true, Options: []*discordgo.ApplicationCommandOption{
Autocomplete: true, {
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) { func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommandAutocomplete { switch i.ApplicationCommandData().Options[0].Name {
commandUseCount++ case "get":
choices := generateDynamicChoices(commandUseCount) if i.Type == discordgo.InteractionApplicationCommandAutocomplete {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ commandUseCount++
Type: discordgo.InteractionApplicationCommandAutocompleteResult, choices := generateDynamicChoices(commandUseCount)
Data: &discordgo.InteractionResponseData{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Choices: choices, 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 if i.Type == discordgo.InteractionApplicationCommand {
for _, option := range i.ApplicationCommandData().Options { option := i.ApplicationCommandData().Options[0].Options[0]
switch option.Name { if option.Name == "tag" {
case "tag": value := option.Value.(string)
value := option.Value.(string) response := fmt.Sprintf("You provided the tag: %s", value)
response := fmt.Sprintf("You provided the tag: %s", value) s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource,
Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{
Data: &discordgo.InteractionResponseData{ Content: response,
Content: response, },
}, })
})
}
} }
} }
} }

View File

@@ -73,7 +73,7 @@ func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoi
var commandUseCount int var commandUseCount int
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.ApplicationCommandData().Name == "get" { if i.ApplicationCommandData().Name == "tag" {
tag_command.Interaction(s, i) tag_command.Interaction(s, i)
} }
} }