From 17f6150a3023b2beef6e10215b2869877af1aad1 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Mon, 19 Feb 2024 23:06:22 +0100 Subject: [PATCH] improved command structure a bit more --- command_tag.go | 95 ++++++++++++++++++++++++++++---------------- go.mod | 6 +-- main.go | 2 +- manage_data.go | 5 +-- register_commands.go | 29 ++++++++++---- 5 files changed, 88 insertions(+), 49 deletions(-) diff --git a/command_tag.go b/command_tag.go index 7088866..67a253b 100644 --- a/command_tag.go +++ b/command_tag.go @@ -1,13 +1,13 @@ package main import ( - "fmt" "log" "github.com/bwmarrin/discordgo" "github.com/iancoleman/strcase" ) +var tagAddName string var tag_command Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "tag", @@ -38,12 +38,6 @@ var tag_command Command = Command{ Description: "Your tag for the saved message", Required: true, }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "content", - Description: "Your content for the saved message", - Required: true, - }, }, }, { @@ -67,21 +61,31 @@ var tag_command Command = Command{ case "get": GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0]) case "add": - option := i.ApplicationCommandData().Options[0] - addTag(i.GuildID, strcase.ToSnake(option.Options[0].StringValue()) /*TODO: tag regex*/, option.Options[1].StringValue()) + tagAddName = strcase.ToSnake(i.ApplicationCommandData().Options[0].Options[0].StringValue()) s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, + Type: discordgo.InteractionResponseModal, Data: &discordgo.InteractionResponseData{ - Content: "Tag added!", - Flags: discordgo.MessageFlagsEphemeral, + CustomID: "tag_add_modal" + i.Interaction.Member.User.ID, + Title: "Add a custom tag command", + Components: []discordgo.MessageComponent{ + discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.TextInput{ + CustomID: "tag_add_modal_content", + Label: "Content", + Placeholder: "Content that gets returned when the tag will be run", + Style: discordgo.TextInputParagraph, + Required: true, + MaxLength: 2000, + Value: "", + }, + }, + }, + }, }, }) 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()) - } + removeTag(i.GuildID, i.ApplicationCommandData().Options[0].Options[0].StringValue()) s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ @@ -91,6 +95,31 @@ var tag_command Command = Command{ }) } }, + ModalID: "tag_add_modal", + ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if tagAddName == "" { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Tag name got missing! Please try again...", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } else { + tagContent := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value + addTag(i.GuildID, tagAddName, tagContent) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Tag \"" + tagAddName + "\" added!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } + }, + Autocomplete: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + AutocompleteTag(s, i) + }, } var short_get_tag_command Command = Command{ @@ -110,29 +139,27 @@ var short_get_tag_command Command = Command{ Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { GetTagCommand(s, i, i.ApplicationCommandData().Options[0]) }, + Autocomplete: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + AutocompleteTag(s, i) + }, } func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { - AutocompleteTag(s, i) - if i.Type == discordgo.InteractionApplicationCommand { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: getTagContent(i.GuildID, option.Value.(string)), - }, - }) - } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: getTagContent(i.GuildID, 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: generateTagChoices(i.GuildID), - }, - }) - } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Data: &discordgo.InteractionResponseData{ + Choices: generateTagChoices(i.GuildID), + }, + }) } func generateTagChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice { diff --git a/go.mod b/go.mod index 9c5bc23..1a43f85 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,14 @@ go 1.21.6 require ( github.com/bwmarrin/discordgo v0.27.1 + github.com/google/uuid v1.6.0 + github.com/iancoleman/strcase v0.3.0 github.com/joho/godotenv v1.5.1 + github.com/lib/pq v1.10.9 ) require ( - github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect - github.com/iancoleman/strcase v0.3.0 // indirect - github.com/lib/pq v1.10.9 // indirect golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect ) diff --git a/main.go b/main.go index 4a80ac5..00d78c3 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ func main() { } fmt.Printf("Bot is now running as \"%s\"!", discord.State.User.Username) sc := make(chan os.Signal, 1) - signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc fmt.Println("\nShutting down...") defer removeCommandFromAllGuilds(discord) diff --git a/manage_data.go b/manage_data.go index ecb58dd..815b8ba 100644 --- a/manage_data.go +++ b/manage_data.go @@ -10,7 +10,7 @@ func initTables() { createTableQuery := `CREATE TABLE IF NOT EXISTS tags ( tag_id TEXT NOT NULL, tag_name TEXT NOT NULL, - tag_content TEXT, + tag_content TEXT NOT NULL, guild_id TEXT NOT NULL, PRIMARY KEY (tag_id,guild_id) );` @@ -43,7 +43,6 @@ func addTag(guildID, tagName, tagContent string) bool { return exists } - func removeTag(guildID string, tagID string) { var exists bool err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, tagID).Scan(&exists) @@ -57,7 +56,6 @@ func removeTag(guildID string, tagID string) { } } } - func getTagIDs(guildID string) ([]string, error) { var IDs []string rows, err := db.Query("SELECT tag_id FROM tags WHERE guild_id = $1", guildID) @@ -80,7 +78,6 @@ func getTagIDs(guildID string) ([]string, error) { return IDs, nil } - func getTagName(guildID string, tagID string) string { var tagName string db.QueryRow("SELECT tag_name FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagName) diff --git a/register_commands.go b/register_commands.go index 4df1281..ba699a7 100644 --- a/register_commands.go +++ b/register_commands.go @@ -2,13 +2,17 @@ package main import ( "fmt" + "strings" "github.com/bwmarrin/discordgo" ) type Command struct { - Definition discordgo.ApplicationCommand - Interact func(s *discordgo.Session, i *discordgo.InteractionCreate) + Definition discordgo.ApplicationCommand + Interact func(s *discordgo.Session, i *discordgo.InteractionCreate) + Autocomplete func(s *discordgo.Session, i *discordgo.InteractionCreate) + ModalSubmit func(s *discordgo.Session, i *discordgo.InteractionCreate) + ModalID string } func ready(s *discordgo.Session, event *discordgo.Ready) { @@ -29,11 +33,22 @@ func ready(s *discordgo.Session, event *discordgo.Ready) { } func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { - switch i.ApplicationCommandData().Name { - case "tag": - tag_command.Interact(s, i) - case "g": - short_get_tag_command.Interact(s, i) + var commands []Command = []Command{tag_command, short_get_tag_command} + for _, command := range commands { + switch i.Type { + case discordgo.InteractionApplicationCommand: + if i.ApplicationCommandData().Name == command.Definition.Name { + command.Interact(s, i) + } + case discordgo.InteractionApplicationCommandAutocomplete: + if i.ApplicationCommandData().Name == command.Definition.Name { + command.Autocomplete(s, i) + } + case discordgo.InteractionModalSubmit: + if strings.HasPrefix(i.ModalSubmitData().CustomID, command.ModalID) { + command.ModalSubmit(s, i) + } + } } }