From 5a2e182453deed16459a21474670153bc7e645d0 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 15 Feb 2024 14:31:22 +0100 Subject: [PATCH] added short command for get but needs some restructuring --- command_tag.go | 64 ++++++++++++++++++++++++++++++++------------------ main.go | 6 ++++- manage_data.go | 2 -- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/command_tag.go b/command_tag.go index f58c9f5..3ac8204 100644 --- a/command_tag.go +++ b/command_tag.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "github.com/bwmarrin/discordgo" ) @@ -28,31 +26,51 @@ var tag_command Command = Command{ }, }} -func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { - switch i.ApplicationCommandData().Options[0].Name { - case "get": - if i.Type == discordgo.InteractionApplicationCommandAutocomplete { - commandUseCount++ - choices := generateDynamicChoices(commandUseCount) +var short_get_tag_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "g", + Description: "A short command to get presaved messages.", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "tag", + Description: "Your predefined tag for the saved message", + Required: true, + Autocomplete: true, + }, + }, + }} + +func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { + 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 option.Name == "tag" { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Choices: choices, + Content: option.Value.(string), }, }) } - 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, - }, - }) - } - } } } + +func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { + switch i.ApplicationCommandData().Options[0].Name { + case "get": + GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0]) + } +} + +func (short_get_tag_command Command) tInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) { + GetTagCommand(s, i, i.ApplicationCommandData().Options[0]) +} diff --git a/main.go b/main.go index 68accd8..c382d5f 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,7 @@ func main() { func ready(s *discordgo.Session, event *discordgo.Ready) { commands := []*discordgo.ApplicationCommand{ &tag_command.Definition, + &short_get_tag_command.Definition, } for _, guild := range event.Guilds { @@ -73,8 +74,11 @@ func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoi var commandUseCount int func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { - if i.ApplicationCommandData().Name == "tag" { + switch i.ApplicationCommandData().Name { + case "tag": tag_command.Interaction(s, i) + case "g": + short_get_tag_command.tInteraction(s, i) } } diff --git a/manage_data.go b/manage_data.go index 148da71..d3964c8 100644 --- a/manage_data.go +++ b/manage_data.go @@ -62,8 +62,6 @@ func debugTags() { } addTag(tags, "new_command", "a new command description") - removeTag(tags, "test_command") - modifyTag(tags, "another_test_command", "updated command description") err = writeTags("data.json", tags) if err != nil {