added remove command
This commit is contained in:
@@ -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,17 +79,8 @@ 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)
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Choices: choices,
|
||||
},
|
||||
})
|
||||
}
|
||||
AutocompleteTag(s, i)
|
||||
if i.Type == discordgo.InteractionApplicationCommand {
|
||||
if option.Name == "tag" {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
@@ -82,6 +88,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),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
2
main.go
2
main.go
@@ -15,6 +15,8 @@ import (
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
//TODO: make the codebase less garbage
|
||||
|
||||
type Command struct {
|
||||
Definition discordgo.ApplicationCommand
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user