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,
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"}
}

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) {
text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
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) {
tagName := i.ModalSubmitData().Components[0].(*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,
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)

View File

@@ -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) {