From 1fc565a3ecd5075dbfb51c660b49a15e0c2764d5 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 15 Feb 2024 16:54:26 +0100 Subject: [PATCH] basic functionality works --- command_tag.go | 17 +++++++++++--- main.go | 13 ----------- manage_data.go | 61 +++++++++++++++++++++++++------------------------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/command_tag.go b/command_tag.go index 07fb258..58469d0 100644 --- a/command_tag.go +++ b/command_tag.go @@ -63,8 +63,7 @@ var short_get_tag_command Command = Command{ func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { if i.Type == discordgo.InteractionApplicationCommandAutocomplete { - commandUseCount++ - choices := generateDynamicChoices(commandUseCount) + choices := generateDynamicChoices() s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionApplicationCommandAutocompleteResult, Data: &discordgo.InteractionResponseData{ @@ -84,6 +83,18 @@ func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option } } +func generateDynamicChoices() []*discordgo.ApplicationCommandOptionChoice { + choices := []*discordgo.ApplicationCommandOptionChoice{} + keys := tags.getTagKeys() + for i := 0; i <= len(keys)-1; i++ { + choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ + Name: keys[i], + Value: tags.Tags[keys[i]], + }) + } + return choices +} + func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { switch i.ApplicationCommandData().Options[0].Name { case "get": @@ -95,7 +106,7 @@ func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.Intera Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: "Tag added!", - Flags: 1 << 6, // ephemeral message + Flags: discordgo.MessageFlagsEphemeral, }, }) } diff --git a/main.go b/main.go index c382d5f..78544d8 100644 --- a/main.go +++ b/main.go @@ -60,19 +60,6 @@ func ready(s *discordgo.Session, event *discordgo.Ready) { } } -func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoice { - choices := []*discordgo.ApplicationCommandOptionChoice{} - for i := 1; i <= count; i++ { - choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ - Name: fmt.Sprintf("Option %d", i), - Value: fmt.Sprintf("option_%d", i), - }) - } - return choices -} - -var commandUseCount int - func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { switch i.ApplicationCommandData().Name { case "tag": diff --git a/manage_data.go b/manage_data.go index 588e810..01af455 100644 --- a/manage_data.go +++ b/manage_data.go @@ -13,48 +13,49 @@ type Tags struct { } var tags Tags +var filename string = "data.json" -func readTags(filename string) (*Tags, error) { +func readTags() { bytes, err := os.ReadFile(filename) - if err != nil { - return nil, err - } - err = json.Unmarshal(bytes, &tags) - if err != nil { - return nil, err - } - - return &tags, nil -} - -func writeTags(filename string, tags *Tags) error { - jsonBytes, err := json.MarshalIndent(tags, "", " ") - if err != nil { - return err - } - - err = os.WriteFile(filename, jsonBytes, 0644) - if err != nil { - return err - } - - return nil -} - -func addTag(tags *Tags, tagKey string, tagValue string) { - tags, err := readTags("data.json") if err != nil { log.Fatalf("Failed to read tags: %v", err) } - tags.Tags[tagKey] = tagValue - err = writeTags("data.json", tags) + err = json.Unmarshal(bytes, &tags) + if err != nil { + log.Fatalf("Failed to read tags: %v", err) + } +} + +func writeTags() { + jsonBytes, err := json.MarshalIndent(&tags, "", " ") + if err != nil { + log.Fatalf("Failed to write tags: %v", err) + } + err = os.WriteFile(filename, jsonBytes, 0644) if err != nil { log.Fatalf("Failed to write tags: %v", err) } } +func addTag(tags *Tags, tagKey string, tagValue string) { + readTags() + tags.Tags[tagKey] = tagValue + writeTags() +} + func removeTag(tags *Tags, tagKey string) { + readTags() delete(tags.Tags, tagKey) + writeTags() +} + +func (tags Tags) getTagKeys() []string { + readTags() + keys := make([]string, 0, len(tags.Tags)) + for k := range tags.Tags { + keys = append(keys, k) + } + return keys } func modifyTag(tags *Tags, tagKey string, newTagValue string) {