added remove command

This commit is contained in:
2024-02-18 21:14:51 +01:00
parent 3748527a54
commit 8fb88677c3
3 changed files with 49 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"github.com/bwmarrin/discordgo" "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,17 +79,8 @@ var short_get_tag_command Command = Command{
}} }}
func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) {
if i.Type == discordgo.InteractionApplicationCommandAutocomplete { AutocompleteTag(s, i)
choices := generateDynamicChoices(i.GuildID)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
Data: &discordgo.InteractionResponseData{
Choices: choices,
},
})
}
if i.Type == discordgo.InteractionApplicationCommand { if i.Type == discordgo.InteractionApplicationCommand {
if option.Name == "tag" {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
@@ -83,6 +89,16 @@ func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option
}) })
} }
} }
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),
},
})
}
} }
func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice { func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice {
@@ -101,13 +117,14 @@ func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptio
} }
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
Name: key, Name: key,
Value: tagContent, Value: tagContent, //TODO: use IDs instead as PK
}) })
} }
return choices return choices
} }
// Yeeeahh the codebase sucks rn
func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.ApplicationCommandData().Options[0].Name { switch i.ApplicationCommandData().Options[0].Name {
case "get": case "get":
@@ -122,6 +139,19 @@ func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.Intera
Flags: discordgo.MessageFlagsEphemeral, 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,
},
})
} }
} }

View File

@@ -15,6 +15,8 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
//TODO: make the codebase less garbage
type Command struct { type Command struct {
Definition discordgo.ApplicationCommand Definition discordgo.ApplicationCommand
} }

View File

@@ -25,14 +25,14 @@ func addTag(guildID, tagName, tagContent string) bool {
return exists return exists
} }
func removeTag(guildID, tagName string) { func removeTag(guildID, tagContent string) {
var exists bool 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 { if err != nil {
log.Println(err) log.Println(err)
} }
if exists { 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 { if err != nil {
log.Println(err) log.Println(err)
} }