started adding form functionality

This commit is contained in:
2024-02-23 12:42:45 +01:00
parent 1796d59d38
commit 5bd66a4e6e
5 changed files with 135 additions and 41 deletions

View File

@@ -21,7 +21,7 @@ var form_command Command = Command{
}, },
{ {
Type: discordgo.ApplicationCommandOptionSubCommand, Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "create", Name: "custom",
Description: "Create a new custom form right inside Discord", Description: "Create a new custom form right inside Discord",
Options: []*discordgo.ApplicationCommandOption{ 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) { 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{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: "Placeholder", Content: "Feature not available yet use `/form add` instead",
Flags: discordgo.MessageFlagsEphemeral, 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) { ComponentInteract: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ if i.Interaction.MessageComponentData().CustomID == "form_demo" {
Type: discordgo.InteractionResponseModal, s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Data: &discordgo.InteractionResponseData{ Type: discordgo.InteractionResponseModal,
CustomID: "form_demo_modal" + i.Interaction.Member.User.ID, Data: &discordgo.InteractionResponseData{
Title: "Demo form", CustomID: "form_demo" + i.Interaction.Member.User.ID,
Components: []discordgo.MessageComponent{ Title: "Demo form",
discordgo.ActionsRow{ Components: []discordgo.MessageComponent{
Components: []discordgo.MessageComponent{ discordgo.ActionsRow{
discordgo.TextInput{ Components: []discordgo.MessageComponent{
CustomID: "demo_short", discordgo.TextInput{
Label: "This is a simple textline", CustomID: "demo_short",
Style: discordgo.TextInputShort, Label: "This is a simple textline",
Placeholder: "...and it is required!", Style: discordgo.TextInputShort,
Value: "", Placeholder: "...and it is required!",
Required: true, Value: "",
MaxLength: 20, Required: true,
MinLength: 0, MaxLength: 20,
MinLength: 0,
},
}, },
}, },
}, discordgo.ActionsRow{
discordgo.ActionsRow{ Components: []discordgo.MessageComponent{
Components: []discordgo.MessageComponent{ discordgo.TextInput{
discordgo.TextInput{ CustomID: "demo_paragraph",
CustomID: "demo_paragraph", Label: "This is a paragraph",
Label: "This is a paragraph", Style: discordgo.TextInputParagraph,
Style: discordgo.TextInputParagraph, Placeholder: "...and it is not required!",
Placeholder: "...and it is not required!", Value: "We already have some input here",
Value: "We already have some input here", Required: false,
Required: false, MaxLength: 2000,
MaxLength: 2000, MinLength: 0,
MinLength: 0, },
}, },
}, },
}, },
}, },
}, })
}) }
}, },
ModalID: "form_demo_modal", ModalIDs: getFormIDs(),
ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, 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"}
} }

View File

@@ -71,7 +71,7 @@ var sticky_command Command = Command{
} }
} }
}, },
ModalID: "sticky_modal", ModalIDs: []string{"sticky_modal"},
ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
message, err := s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{ message, err := s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{

View File

@@ -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) { ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
tagName := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value tagName := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
tagContent := i.ModalSubmitData().Components[1].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value tagContent := i.ModalSubmitData().Components[1].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value

View File

@@ -21,6 +21,20 @@ func initTables() {
guild_id TEXT NOT NULL, guild_id TEXT NOT NULL,
PRIMARY KEY (channel_id, guild_id) 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) _, err := db.Exec(createTableQuery)

View File

@@ -16,7 +16,7 @@ type Command struct {
ComponentIDs []string ComponentIDs []string
Autocomplete func(s *discordgo.Session, i *discordgo.InteractionCreate) Autocomplete func(s *discordgo.Session, i *discordgo.InteractionCreate)
ModalSubmit 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} 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) command.Autocomplete(s, i)
} }
case discordgo.InteractionModalSubmit: case discordgo.InteractionModalSubmit:
if command.ModalSubmit != nil && strings.HasPrefix(i.ModalSubmitData().CustomID, command.ModalID) { var hasID bool = false
command.ModalSubmit(s, i) 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: case discordgo.InteractionMessageComponent:
if command.ComponentInteract != nil && slices.Contains(command.ComponentIDs, i.MessageComponentData().CustomID) { if command.ComponentInteract != nil && slices.Contains(command.ComponentIDs, i.MessageComponentData().CustomID) {