From 8fb88677c36616f632f84d112aeb5c6d70c5236e Mon Sep 17 00:00:00 2001 From: vaporvee Date: Sun, 18 Feb 2024 21:14:51 +0100 Subject: [PATCH] added remove command --- command_tag.go | 58 ++++++++++++++++++++++++++++++++++++++------------ main.go | 2 ++ manage_data.go | 6 +++--- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/command_tag.go b/command_tag.go index b0e8054..80d4cb7 100644 --- a/command_tag.go +++ b/command_tag.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "github.com/bwmarrin/discordgo" @@ -45,6 +46,20 @@ var tag_command Command = Command{ }, }, }, + { + Name: "remove", + Description: "A command to remove messages saved to the bot.", + Type: discordgo.ApplicationCommandOptionSubCommand, + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "tag", + Description: "The tag you want to remove", + Required: true, + Autocomplete: true, + }, + }, + }, }, }} @@ -64,24 +79,25 @@ var short_get_tag_command Command = Command{ }} func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { - if i.Type == discordgo.InteractionApplicationCommandAutocomplete { - choices := generateDynamicChoices(i.GuildID) + AutocompleteTag(s, i) + if i.Type == discordgo.InteractionApplicationCommand { 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 { - if option.Name == "tag" { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: option.Value.(string), - }, - }) - } +} + +func AutocompleteTag(s *discordgo.Session, i *discordgo.InteractionCreate) { + if i.Type == discordgo.InteractionApplicationCommandAutocomplete { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Data: &discordgo.InteractionResponseData{ + Choices: generateDynamicChoices(i.GuildID), + }, + }) } } @@ -101,13 +117,14 @@ func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptio } choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ Name: key, - Value: tagContent, + Value: tagContent, //TODO: use IDs instead as PK }) } return choices } +// Yeeeahh the codebase sucks rn func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { switch i.ApplicationCommandData().Options[0].Name { case "get": @@ -122,6 +139,19 @@ func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.Intera Flags: discordgo.MessageFlagsEphemeral, }, }) + case "remove": + AutocompleteTag(s, i) + if i.Type == discordgo.InteractionApplicationCommand { + fmt.Println("Trying to remove " + i.ApplicationCommandData().Options[0].Options[0].StringValue()) // so now it returns the content so wee reeeeaally need to start using UUIDs + removeTag(i.GuildID, i.ApplicationCommandData().Options[0].Options[0].StringValue()) + } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Tag removed!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) } } diff --git a/main.go b/main.go index f5448b3..5fd3a64 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,8 @@ import ( _ "github.com/lib/pq" ) +//TODO: make the codebase less garbage + type Command struct { Definition discordgo.ApplicationCommand } diff --git a/manage_data.go b/manage_data.go index d28ee64..3762aaf 100644 --- a/manage_data.go +++ b/manage_data.go @@ -25,14 +25,14 @@ func addTag(guildID, tagName, tagContent string) bool { return exists } -func removeTag(guildID, tagName string) { +func removeTag(guildID, tagContent string) { var exists bool - err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_name = $2)", guildID, tagName).Scan(&exists) + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_content = $2)", guildID, tagContent).Scan(&exists) // that is so dumb next commit i will make the tag IDs if err != nil { log.Println(err) } if exists { - _, err = db.Exec("DELETE FROM tags WHERE guild_id = $1 AND tag_name = $2", guildID, tagName) + _, err = db.Exec("DELETE FROM tags WHERE guild_id = $1 AND tag_content = $2", guildID, tagContent) if err != nil { log.Println(err) }