From 3c524e2f27b8eaff0ee750c5dc1e53b6f21c370b Mon Sep 17 00:00:00 2001 From: vaporvee Date: Mon, 26 Feb 2024 13:22:49 +0100 Subject: [PATCH] fixed form button IDs not getting read --- cmd_form.go | 14 +++++++++++--- main.go | 13 ++++++------- manage_data.go | 2 +- register_commands.go | 11 ++++++++--- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cmd_form.go b/cmd_form.go index f3af0fa..c455762 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -3,6 +3,7 @@ package main import ( "bytes" "os" + "strings" "github.com/bwmarrin/discordgo" "github.com/google/uuid" @@ -146,7 +147,7 @@ var form_command Command = Command{ } var exists bool = true - formManageID := uuid.New() + var formManageID uuid.UUID = uuid.New() for exists { formManageID = uuid.New() exists = getFormManageIdExists(i.GuildID, formManageID) @@ -162,7 +163,6 @@ var form_command Command = Command{ if !(len(options.Options) <= 4) { modsCanComment = options.Options[4].BoolValue() } - message, _ := s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{ Embed: &discordgo.MessageEmbed{ Color: hexToDecimal(color["primary"]), @@ -194,8 +194,16 @@ var form_command Command = Command{ }) } }, - ComponentIDs: getFormButtonIDs(), ComponentInteract: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if strings.HasPrefix(i.Interaction.MessageComponentData().CustomID, "form:") { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: i.Interaction.MessageComponentData().CustomID, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } if i.Interaction.MessageComponentData().CustomID == "form_demo" { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseModal, diff --git a/main.go b/main.go index 89efa14..3762847 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,12 @@ func main() { godotenv.Load() var err error - + connStr := "postgresql://" + os.Getenv("DB_USER") + ":" + url.QueryEscape(os.Getenv("DB_PASSWORD")) + "@" + os.Getenv("DB_SERVER") + ":" + string(os.Getenv("DB_PORT")) + "/" + os.Getenv("DB_NAME") + "?sslmode=disable&application_name=Discord Bot" + db, err = sql.Open("postgres", connStr) + if err != nil { + log.Fatal(err) + } + initTables() discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN")) if err != nil { fmt.Println("error creating Discord session,", err) @@ -42,12 +47,6 @@ func main() { fmt.Println("error opening connection,", err) return } - connStr := "postgresql://" + os.Getenv("DB_USER") + ":" + url.QueryEscape(os.Getenv("DB_PASSWORD")) + "@" + os.Getenv("DB_SERVER") + ":" + string(os.Getenv("DB_PORT")) + "/" + os.Getenv("DB_NAME") + "?sslmode=disable&application_name=" + discord.State.User.Username - db, err = sql.Open("postgres", connStr) - if err != nil { - log.Fatal(err) - } - initTables() fmt.Printf("\nBot is now running as \"%s\"!", discord.State.User.Username) sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) diff --git a/manage_data.go b/manage_data.go index be36499..5a1d8a2 100644 --- a/manage_data.go +++ b/manage_data.go @@ -179,7 +179,7 @@ func removeSticky(guildID string, channelID string) { func getFormManageIdExists(guildID string, id uuid.UUID) bool { var exists bool - err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM form_buttons WHERE guild_id = $1 AND form_manage_id = $2)", guildID, id).Scan(&exists) + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM form_buttons WHERE form_manage_id = $1)", id).Scan(&exists) if err != nil { log.Println(err) } diff --git a/register_commands.go b/register_commands.go index 27cc30f..6463868 100644 --- a/register_commands.go +++ b/register_commands.go @@ -19,7 +19,7 @@ type Command struct { ModalIDs []string } -var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command, form_command} +var commands []Command = []Command{form_command, tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command} func ready(s *discordgo.Session, event *discordgo.Ready) { fmt.Print("\nStarting up... (May take longer when Discord rate limits the bot)") @@ -72,8 +72,13 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { } } case discordgo.InteractionMessageComponent: - if command.ComponentInteract != nil && slices.Contains(command.ComponentIDs, i.MessageComponentData().CustomID) { - command.ComponentInteract(s, i) + if command.ComponentInteract != nil { + if command.Definition.Name == "form" { + command.ComponentIDs = getFormButtonIDs() + } // FIXME: Makes it dynamic i don't know why it isn't otherwise + if slices.Contains(command.ComponentIDs, i.MessageComponentData().CustomID) { + command.ComponentInteract(s, i) + } } } }