From f11d3bc6c8a12d21ad85ad7ab3644d41660d97d6 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 29 Feb 2024 18:30:31 +0100 Subject: [PATCH] continued forms --- cmd_form.go | 59 ++++++++++++++-------------- form_templates/form_demo.json | 2 +- form_templates/template_general.json | 2 +- form_templates/template_ticket.json | 2 +- form_templates/template_url.json | 2 +- manage_data.go | 29 +++++++++----- register_commands.go | 2 +- 7 files changed, 54 insertions(+), 44 deletions(-) diff --git a/cmd_form.go b/cmd_form.go index 940162a..a147b11 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -71,7 +71,7 @@ var form_command Command = Command{ }, { Type: discordgo.ApplicationCommandOptionChannel, - Name: "accept_channel_id", + Name: "accept_channel", Description: "Channel for results that need to be accepted by a moderator before sending it to the result channel", ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText}, }, @@ -92,7 +92,7 @@ var form_command Command = Command{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: "Get the example file edit it (make sure to have a unique \"form_id\") and submit it via `/form create`.\nOr use the demo button to get an idea of how the example would look like.", + Content: "Get the example file edit it (make sure to have a unique \"form_type\") and submit it via `/form create`.\nOr use the demo button to get an idea of how the example would look like.", Flags: discordgo.MessageFlagsEphemeral, Files: []*discordgo.File{ { @@ -130,39 +130,40 @@ var form_command Command = Command{ var modsCanComment bool options := i.ApplicationCommandData().Options[0] - if len(options.Options) <= 1 { - formID = "template_general" - } else { - formID = options.Options[1].StringValue() + for _, opt := range options.Options { + switch opt.Name { + case "type": + formID = options.Options[1].StringValue() + case "title": + overwriteTitle = opt.StringValue() + title = overwriteTitle + case "accept_channel": + acceptChannelID = opt.ChannelValue(s).ID + case "mods_can_comment": + modsCanComment = opt.BoolValue() + } } - switch formID { - case "template_feedback": - title = "Submit Feedback" - case "template_ticket": - title = "Make a new ticket" - case "template_url": - title = "Add your URL" - case "template_general": - title = "Form" + if formID == "" { + formID = "template_general" + } + + formTitles := map[string]string{ + "template_feedback": "Submit Feedback", + "template_ticket": "Make a new ticket", + "template_url": "Add your URL", + "template_general": "Form", + } + if val, ok := formTitles[formID]; ok { + title = val } var exists bool = true var formManageID uuid.UUID = uuid.New() for exists { formManageID = uuid.New() - exists = getFormManageIdExists(i.GuildID, formManageID) + exists = getFormManageIdExists(formManageID) } - if !(len(options.Options) <= 2) { - overwriteTitle = options.Options[2].StringValue() - title = overwriteTitle - } - if !(len(options.Options) <= 3) { - acceptChannelID = options.Options[3].ChannelValue(s).ID - } - if !(len(options.Options) <= 4) { - modsCanComment = options.Options[4].BoolValue() - } message, _ := s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{ Embed: &discordgo.MessageEmbed{ Color: hexToDecimal(color["primary"]), @@ -199,7 +200,7 @@ var form_command Command = Command{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: i.Interaction.MessageComponentData().CustomID, + Content: getFormType(strings.TrimPrefix(i.Interaction.MessageComponentData().CustomID, "form:")), Flags: discordgo.MessageFlagsEphemeral, }, }) @@ -244,7 +245,7 @@ var form_command Command = Command{ }) } }, - ModalIDs: getFormIDs(), + ModalIDs: getFormTypes(), ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -282,7 +283,7 @@ var form_command Command = Command{ }, } -func getFormIDs() []string { +func getFormTypes() []string { //needs custom IDs from databank return []string{"form_demo", "template_ticket", "template_url", "template_general"} } diff --git a/form_templates/form_demo.json b/form_templates/form_demo.json index 048811c..ef6a138 100644 --- a/form_templates/form_demo.json +++ b/form_templates/form_demo.json @@ -1,5 +1,5 @@ { - "form_id": "form_demo", + "form_type": "form_demo", "title": "Demo form", "form": [ { diff --git a/form_templates/template_general.json b/form_templates/template_general.json index 8b29132..624aae4 100644 --- a/form_templates/template_general.json +++ b/form_templates/template_general.json @@ -1,5 +1,5 @@ { - "form_id": "template_general", + "form_type": "template_general", "title": "Form", "form": [ { diff --git a/form_templates/template_ticket.json b/form_templates/template_ticket.json index cf2a898..98cc71d 100644 --- a/form_templates/template_ticket.json +++ b/form_templates/template_ticket.json @@ -1,5 +1,5 @@ { - "form_id": "template_ticket", + "form_type": "template_ticket", "title": "Submit your Ticket", "mods_can_comment": true, "form": [ diff --git a/form_templates/template_url.json b/form_templates/template_url.json index 9ed816c..f572fc9 100644 --- a/form_templates/template_url.json +++ b/form_templates/template_url.json @@ -1,5 +1,5 @@ { - "form_id": "template_url", + "form_type": "template_url", "title": "Submit URL", "form": [ { diff --git a/manage_data.go b/manage_data.go index 5a1d8a2..55ec605 100644 --- a/manage_data.go +++ b/manage_data.go @@ -22,15 +22,15 @@ func initTables() { PRIMARY KEY (channel_id, guild_id) ); CREATE TABLE IF NOT EXISTS custom_forms ( - form_id TEXT NOT NULL, + form_type TEXT NOT NULL, title TEXT NOT NULL, json JSON NOT NULL, guild_id TEXT NOT NULL, - PRIMARY KEY (form_id, guild_id) + PRIMARY KEY (form_type, guild_id) ); - CREATE TABLE IF NOT EXISTS form_buttons ( + CREATE TABLE IF NOT EXISTS form_manage ( form_manage_id TEXT NOT NULL, - form_id TEXT NOT NULL, + form_type TEXT NOT NULL, overwrite_title TEXT, message_id TEXT NOT NULL, channel_id TEXT NOT NULL, @@ -38,7 +38,7 @@ func initTables() { result_channel_id TEXT NOT NULL, accept_channel_id TEXT, mods_can_comment BOOL, - PRIMARY KEY (form_manage_id, form_id) + PRIMARY KEY (form_manage_id, form_type) ); ` @@ -177,17 +177,17 @@ func removeSticky(guildID string, channelID string) { } } -func getFormManageIdExists(guildID string, id uuid.UUID) bool { +func getFormManageIdExists(id uuid.UUID) bool { var exists bool - err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM form_buttons WHERE form_manage_id = $1)", id).Scan(&exists) + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM form_manage WHERE form_manage_id = $1)", id).Scan(&exists) if err != nil { log.Println(err) } return exists } -func addFormButton(guildID string, channelID string, messageID string, formManageID string, formID string, resultChannelID string, overwriteTitle string, acceptChannelID string, modsCanComment bool) { - _, err := db.Exec("INSERT INTO form_buttons (guild_id, form_manage_id, channel_id, message_id, form_id, result_channel_id, overwrite_title, accept_channel_id, mods_can_comment) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", guildID, formManageID, channelID, messageID, formID, resultChannelID, overwriteTitle, acceptChannelID, modsCanComment) +func addFormButton(guildID string, channelID string, messageID string, formManageID string, formType string, resultChannelID string, overwriteTitle string, acceptChannelID string, modsCanComment bool) { + _, err := db.Exec("INSERT INTO form_manage (guild_id, form_manage_id, channel_id, message_id, form_type, result_channel_id, overwrite_title, accept_channel_id, mods_can_comment) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", guildID, formManageID, channelID, messageID, formType, resultChannelID, overwriteTitle, acceptChannelID, modsCanComment) if err != nil { log.Println(err) } @@ -198,7 +198,7 @@ func getFormManageIDs() []string { return nil } var IDs []string - rows, err := db.Query("SELECT form_manage_id FROM form_buttons") + rows, err := db.Query("SELECT form_manage_id FROM form_manage") if err != nil { log.Println(err) return nil @@ -220,3 +220,12 @@ func getFormManageIDs() []string { } return IDs } + +func getFormType(formManageID string) string { + var formType string + err := db.QueryRow("SELECT form_type FROM form_manage WHERE form_manage_id = $1", formManageID).Scan(&formType) + if err != nil { + log.Println(err) + } + return formType +} diff --git a/register_commands.go b/register_commands.go index 6463868..8d17280 100644 --- a/register_commands.go +++ b/register_commands.go @@ -22,7 +22,7 @@ type Command struct { 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)") + fmt.Print("\nStarting up...") removeOldCommandFromAllGuilds(s) var existingCommandNames []string for _, guild := range event.Guilds {