improved command structure a bit more

This commit is contained in:
2024-02-19 23:06:22 +01:00
parent 9001c734ae
commit 17f6150a30
5 changed files with 88 additions and 49 deletions

View File

@@ -1,13 +1,13 @@
package main package main
import ( import (
"fmt"
"log" "log"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
) )
var tagAddName string
var tag_command Command = Command{ var tag_command Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "tag", Name: "tag",
@@ -38,12 +38,6 @@ var tag_command Command = Command{
Description: "Your tag for the saved message", Description: "Your tag for the saved message",
Required: true, 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": case "get":
GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0]) GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0])
case "add": case "add":
option := i.ApplicationCommandData().Options[0] tagAddName = strcase.ToSnake(i.ApplicationCommandData().Options[0].Options[0].StringValue())
addTag(i.GuildID, strcase.ToSnake(option.Options[0].StringValue()) /*TODO: tag regex*/, option.Options[1].StringValue())
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: "Tag added!", CustomID: "tag_add_modal" + i.Interaction.Member.User.ID,
Flags: discordgo.MessageFlagsEphemeral, 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": 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{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ 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{ 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) { Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
GetTagCommand(s, i, i.ApplicationCommandData().Options[0]) 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) { 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{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: getTagContent(i.GuildID, option.Value.(string)), Content: getTagContent(i.GuildID, option.Value.(string)),
}, },
}) })
}
} }
func AutocompleteTag(s *discordgo.Session, i *discordgo.InteractionCreate) { func AutocompleteTag(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommandAutocomplete {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionApplicationCommandAutocompleteResult, Type: discordgo.InteractionApplicationCommandAutocompleteResult,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Choices: generateTagChoices(i.GuildID), Choices: generateTagChoices(i.GuildID),
}, },
}) })
}
} }
func generateTagChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice { func generateTagChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice {

6
go.mod
View File

@@ -4,14 +4,14 @@ go 1.21.6
require ( require (
github.com/bwmarrin/discordgo v0.27.1 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/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
) )
require ( require (
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // 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/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
) )

View File

@@ -49,7 +49,7 @@ func main() {
} }
fmt.Printf("Bot is now running as \"%s\"!", discord.State.User.Username) fmt.Printf("Bot is now running as \"%s\"!", discord.State.User.Username)
sc := make(chan os.Signal, 1) 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 <-sc
fmt.Println("\nShutting down...") fmt.Println("\nShutting down...")
defer removeCommandFromAllGuilds(discord) defer removeCommandFromAllGuilds(discord)

View File

@@ -10,7 +10,7 @@ func initTables() {
createTableQuery := `CREATE TABLE IF NOT EXISTS tags ( createTableQuery := `CREATE TABLE IF NOT EXISTS tags (
tag_id TEXT NOT NULL, tag_id TEXT NOT NULL,
tag_name TEXT NOT NULL, tag_name TEXT NOT NULL,
tag_content TEXT, tag_content TEXT NOT NULL,
guild_id TEXT NOT NULL, guild_id TEXT NOT NULL,
PRIMARY KEY (tag_id,guild_id) PRIMARY KEY (tag_id,guild_id)
);` );`
@@ -43,7 +43,6 @@ func addTag(guildID, tagName, tagContent string) bool {
return exists return exists
} }
func removeTag(guildID string, tagID string) { func removeTag(guildID string, tagID string) {
var exists bool var exists bool
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, tagID).Scan(&exists) 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) { func getTagIDs(guildID string) ([]string, error) {
var IDs []string var IDs []string
rows, err := db.Query("SELECT tag_id FROM tags WHERE guild_id = $1", guildID) 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 return IDs, nil
} }
func getTagName(guildID string, tagID string) string { func getTagName(guildID string, tagID string) string {
var tagName string var tagName string
db.QueryRow("SELECT tag_name FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagName) db.QueryRow("SELECT tag_name FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagName)

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@@ -9,6 +10,9 @@ import (
type Command struct { type Command struct {
Definition discordgo.ApplicationCommand Definition discordgo.ApplicationCommand
Interact func(s *discordgo.Session, i *discordgo.InteractionCreate) 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) { 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) { func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.ApplicationCommandData().Name { var commands []Command = []Command{tag_command, short_get_tag_command}
case "tag": for _, command := range commands {
tag_command.Interact(s, i) switch i.Type {
case "g": case discordgo.InteractionApplicationCommand:
short_get_tag_command.Interact(s, i) 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)
}
}
} }
} }