From 5bd66a4e6e4b1056f2bb0d35e8565f2474985023 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Fri, 23 Feb 2024 12:42:45 +0100 Subject: [PATCH] started adding form functionality --- cmd_form.go | 144 ++++++++++++++++++++++++++++++++----------- cmd_sticky.go | 2 +- cmd_tag.go | 2 +- manage_data.go | 14 +++++ register_commands.go | 14 ++++- 5 files changed, 135 insertions(+), 41 deletions(-) diff --git a/cmd_form.go b/cmd_form.go index 3dc8202..48206e7 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -21,7 +21,7 @@ var form_command Command = Command{ }, { Type: discordgo.ApplicationCommandOptionSubCommand, - Name: "create", + Name: "custom", Description: "Create a new custom form right inside Discord", Options: []*discordgo.ApplicationCommandOption{ { @@ -44,6 +44,19 @@ var form_command Command = Command{ }, }, }, + { + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: "add", + Description: "Adds existing forms to this channel", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "type", + Description: "Which type of form you want to add", + Autocomplete: true, + }, + }, + }, }, }, Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { @@ -79,57 +92,85 @@ var form_command Command = Command{ }, }, }) - case "create": + case "custom": s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: "Placeholder", + Content: "Feature not available yet use `/form add` instead", Flags: discordgo.MessageFlagsEphemeral, }, }) + case "add": + var optionName string = i.ApplicationCommandData().Options[0].Options[0].Name + var optionValue string = i.ApplicationCommandData().Options[0].Options[0].StringValue() + if optionName == "" { // handling optional empty + optionName = "General" + } + s.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{ + Embed: &discordgo.MessageEmbed{ + Color: hexToDecimal(color["primary"]), + Title: optionName, + Description: "Press the bottom button to open a form popup.", + }, + Components: []discordgo.MessageComponent{ + discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.Button{ + CustomID: "form:" + optionValue, + Label: "Submit", + Emoji: discordgo.ComponentEmoji{ + Name: "📥", + }, + }, + }, + }, + }, + }) } }, - ComponentIDs: []string{"form_demo"}, + ComponentIDs: getFormIDs(), ComponentInteract: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseModal, - Data: &discordgo.InteractionResponseData{ - CustomID: "form_demo_modal" + i.Interaction.Member.User.ID, - Title: "Demo form", - Components: []discordgo.MessageComponent{ - discordgo.ActionsRow{ - Components: []discordgo.MessageComponent{ - discordgo.TextInput{ - CustomID: "demo_short", - Label: "This is a simple textline", - Style: discordgo.TextInputShort, - Placeholder: "...and it is required!", - Value: "", - Required: true, - MaxLength: 20, - MinLength: 0, + if i.Interaction.MessageComponentData().CustomID == "form_demo" { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseModal, + Data: &discordgo.InteractionResponseData{ + CustomID: "form_demo" + i.Interaction.Member.User.ID, + Title: "Demo form", + Components: []discordgo.MessageComponent{ + discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.TextInput{ + CustomID: "demo_short", + Label: "This is a simple textline", + Style: discordgo.TextInputShort, + Placeholder: "...and it is required!", + Value: "", + Required: true, + MaxLength: 20, + MinLength: 0, + }, }, }, - }, - discordgo.ActionsRow{ - Components: []discordgo.MessageComponent{ - discordgo.TextInput{ - CustomID: "demo_paragraph", - Label: "This is a paragraph", - Style: discordgo.TextInputParagraph, - Placeholder: "...and it is not required!", - Value: "We already have some input here", - Required: false, - MaxLength: 2000, - MinLength: 0, + discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.TextInput{ + CustomID: "demo_paragraph", + Label: "This is a paragraph", + Style: discordgo.TextInputParagraph, + Placeholder: "...and it is not required!", + Value: "We already have some input here", + Required: false, + MaxLength: 2000, + MinLength: 0, + }, }, }, }, }, - }, - }) + }) + } }, - ModalID: "form_demo_modal", + ModalIDs: getFormIDs(), ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -139,4 +180,35 @@ var form_command Command = Command{ }, }) }, + Autocomplete: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + choices := []*discordgo.ApplicationCommandOptionChoice{ + { + Name: "Feedback", + Value: "template_feedback", + }, + { + Name: "Support Ticket", + Value: "template_ticket", + }, + { + Name: "Submit URL", + Value: "template_url", + }, + { + Name: "General", + Value: "template_general", + }, + } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Data: &discordgo.InteractionResponseData{ + Choices: choices, + }, + }) + }, +} + +func getFormIDs() []string { + //needs databank support + return []string{"form_demo"} } diff --git a/cmd_sticky.go b/cmd_sticky.go index 804dc77..0aadb58 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -71,7 +71,7 @@ var sticky_command Command = Command{ } } }, - ModalID: "sticky_modal", + ModalIDs: []string{"sticky_modal"}, ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value message, err := s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{ diff --git a/cmd_tag.go b/cmd_tag.go index ccfc6c0..d41dc47 100644 --- a/cmd_tag.go +++ b/cmd_tag.go @@ -97,7 +97,7 @@ var tag_command Command = Command{ }) } }, - ModalID: "tag_add_modal", + ModalIDs: []string{"tag_add_modal"}, ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { tagName := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value tagContent := i.ModalSubmitData().Components[1].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value diff --git a/manage_data.go b/manage_data.go index fc882de..b60be1f 100644 --- a/manage_data.go +++ b/manage_data.go @@ -21,6 +21,20 @@ func initTables() { guild_id TEXT NOT NULL, PRIMARY KEY (channel_id, guild_id) ); + CREATE TABLE IF NOT EXISTS forms ( + form_id TEXT NOT NULL, + title TEXT NOT NULL, + json TEXT NOT NULL, + guild_id TEXT NOT NULL, + PRIMARY KEY (form_id, guild_id) + ); + CREATE TABLE IF NOT EXISTS formbuttons ( + form_id TEXT NOT NULL, + message_id TEXT NOT NULL, + channel_id TEXT NOT NULL, + guild_id TEXT NOT NULL, + PRIMARY KEY (form_id, guild_id, channel_id) + ); ` _, err := db.Exec(createTableQuery) diff --git a/register_commands.go b/register_commands.go index 751ea7a..27cc30f 100644 --- a/register_commands.go +++ b/register_commands.go @@ -16,7 +16,7 @@ type Command struct { ComponentIDs []string Autocomplete func(s *discordgo.Session, i *discordgo.InteractionCreate) ModalSubmit func(s *discordgo.Session, i *discordgo.InteractionCreate) - ModalID string + ModalIDs []string } var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command, form_command} @@ -60,8 +60,16 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { command.Autocomplete(s, i) } case discordgo.InteractionModalSubmit: - if command.ModalSubmit != nil && strings.HasPrefix(i.ModalSubmitData().CustomID, command.ModalID) { - command.ModalSubmit(s, i) + var hasID bool = false + if command.ModalSubmit != nil { + for _, modalID := range command.ModalIDs { + if strings.HasPrefix(i.ModalSubmitData().CustomID, modalID) { + hasID = true + } + } + if hasID { + command.ModalSubmit(s, i) + } } case discordgo.InteractionMessageComponent: if command.ComponentInteract != nil && slices.Contains(command.ComponentIDs, i.MessageComponentData().CustomID) {