diff --git a/command_tag.go b/command_tag.go new file mode 100644 index 0000000..eaeb6b4 --- /dev/null +++ b/command_tag.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + + "github.com/bwmarrin/discordgo" +) + +var tag_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "get", + Description: "A command to get messages saved to the bot.", + 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, + }, + }) + } + } + } + } +} diff --git a/main.go b/main.go index 6df6adc..d78f316 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,13 @@ import ( "github.com/joho/godotenv" ) +type Command struct { + Definition discordgo.ApplicationCommand +} + func main() { godotenv.Load() + debugTags() discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN")) if err != nil { fmt.Println("error creating Discord session,", err) @@ -40,19 +45,7 @@ func main() { func ready(s *discordgo.Session, event *discordgo.Ready) { commands := []*discordgo.ApplicationCommand{ - { - Name: "get", - Description: "A command to get messages saved to the bot.", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "tag", - Description: "Your predefined tag for the saved message", - Required: true, - Autocomplete: true, - }, - }, - }, + &tag_command.Definition, } for _, guild := range event.Guilds { @@ -80,39 +73,8 @@ func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoi var commandUseCount int func interactionCreate(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 i.ApplicationCommandData().Name == "get" { - // Check if the command has options - 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, - }, - }) - } - } - } - } + if i.ApplicationCommandData().Name == "get" { + tag_command.Interaction(s, i) } } diff --git a/manage_data.go b/manage_data.go new file mode 100644 index 0000000..a408836 --- /dev/null +++ b/manage_data.go @@ -0,0 +1,70 @@ +package main + +import ( + "encoding/json" + "log" + "os" +) + +type Tags struct { + Tags map[string]string `json:"tags"` +} + +func readTags(filename string) (*Tags, error) { + bytes, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + + var tags Tags + 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.Tags[tagKey] = tagValue +} + +func removeTag(tags *Tags, tagKey string) { + delete(tags.Tags, tagKey) +} + +func modifyTag(tags *Tags, tagKey string, newTagValue string) { + if _, exists := tags.Tags[tagKey]; exists { + tags.Tags[tagKey] = newTagValue + } +} + +func debugTags() { + tags, err := readTags("tags.json") + if err != nil { + log.Fatalf("Failed to read tags: %v", err) + } + + addTag(tags, "new_command", "a new command description") + removeTag(tags, "test_command") + modifyTag(tags, "another_test_command", "updated command description") + + err = writeTags("tags.json", tags) + if err != nil { + log.Fatalf("Failed to write tags: %v", err) + } +} diff --git a/tags.json b/tags.json new file mode 100644 index 0000000..c890961 --- /dev/null +++ b/tags.json @@ -0,0 +1,8 @@ +{ + "tags": { + "another_another_test_command": "updated command description", + "another_test_command": "updated command description", + "new_command": "a new command description", + "new_tag": "A newly added tag." + } +} \ No newline at end of file