From 8597308f7c82c76785deba36ce6748517abcdce8 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 17 Dec 2023 06:05:27 -0600 Subject: [PATCH] Begin switching over to modal-based dynamic fields --- commands.go | 91 ++++++++++++++++++++++++++++++++--------------------- form.go | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 36 deletions(-) create mode 100644 form.go diff --git a/commands.go b/commands.go index a32991d..bfad46b 100644 --- a/commands.go +++ b/commands.go @@ -116,26 +116,9 @@ var RegisterCommandDefinition = &discordgo.ApplicationCommand{ }, { Type: discordgo.ApplicationCommandOptionString, - Name: "make", - Description: "Make of Vehicle (e.g. Honda)", - MaxLength: 15, - Required: true, - // TODO: Add autocomplete - }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "model", - Description: "Model of Vehicle (e.g. Civic)", - MaxLength: 15, - Required: true, - // TODO: Add autocomplete - }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "plate", - Description: "License Plate Number (e.g. 123ABC)", - MaxLength: 8, - Required: true, + Name: "code", + Description: "The guest code, if required", + Required: false, }, }, } @@ -144,30 +127,69 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I switch interaction.Type { case discordgo.InteractionApplicationCommand: - // TODO: Validate license plate - session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{ - { - Footer: &discordgo.MessageEmbedFooter{ - Text: GetFooterText(), + location_id, parse_err := strconv.Atoi(interaction.ApplicationCommandData().Options[0].StringValue()) + if parse_err != nil { + panic(parse_err) + } + + form := GetForm(uint(location_id)) + if form.err != nil { + panic(form.err) + } + + if form.requireGuestCode { + session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Footer: &discordgo.MessageEmbedFooter{ + Text: GetFooterText(), + }, + Description: "This location requires a guest code to register a vehicle.", }, - Description: "testing 123", - Fields: []*discordgo.MessageEmbedField{}, }, }, - AllowedMentions: &discordgo.MessageAllowedMentions{}, + }) + return + } + + registrationFormComponents := FormToComponents(form) + + err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseModal, + Data: &discordgo.InteractionResponseData{ + CustomID: "registration_" + interaction.Interaction.Member.User.ID, + Title: "Vehicle Registration", + Components: registrationFormComponents, }, }) + if err != nil { + panic(err) + } + + // TODO: Validate license plate + // session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + // Type: discordgo.InteractionResponseChannelMessageWithSource, + // Data: &discordgo.InteractionResponseData{ + // Embeds: []*discordgo.MessageEmbed{ + // { + // Footer: &discordgo.MessageEmbedFooter{ + // Text: GetFooterText(), + // }, + // Description: "testing 123", + // Fields: []*discordgo.MessageEmbedField{}, + // }, + // }, + // AllowedMentions: &discordgo.MessageAllowedMentions{}, + // }, + // }) case discordgo.InteractionApplicationCommandAutocomplete: data := interaction.ApplicationCommandData() var choices []*discordgo.ApplicationCommandOptionChoice LocationOption := data.Options[0] - MakeOption := data.Options[1] - ModelOption := data.Options[2] switch { case LocationOption.Focused: @@ -184,9 +206,6 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I Value: strconv.Itoa(int(location.id)), } } - case MakeOption.Focused: - - case ModelOption.Focused: } err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ diff --git a/form.go b/form.go new file mode 100644 index 0000000..bc55690 --- /dev/null +++ b/form.go @@ -0,0 +1,62 @@ +package main + +import "github.com/bwmarrin/discordgo" + +// FormToComponents converts the form requested into usable modal components. +func FormToComponents(form GetFormResult) []discordgo.MessageComponent { + components := make([]discordgo.MessageComponent, 0, 3) + + for _, field := range form.fields { + var component discordgo.TextInput + + switch field.id { + case "vehicleApt": + component = discordgo.TextInput{ + CustomID: "vehicleApt", + Label: "Apartment Number", + Style: discordgo.TextInputShort, + Required: true, + MinLength: 1, + MaxLength: 5, + } + case "vehicleMake": + component = discordgo.TextInput{ + CustomID: "vehicleMake", + Label: "Make", + Placeholder: "Honda", + Style: discordgo.TextInputShort, + Required: true, + MinLength: 4, + MaxLength: 15, + } + case "vehicleModel": + component = discordgo.TextInput{ + CustomID: "vehicleModel", + Label: "Model", + Style: discordgo.TextInputShort, + Placeholder: "Accord", + Required: true, + MinLength: 1, + MaxLength: 16, + } + case "vehicleLicensePlate": + component = discordgo.TextInput{ + CustomID: "vehicleLicensePlate", + Label: "License Plate", + Placeholder: "ABC123", + Style: discordgo.TextInputShort, + Required: true, + MinLength: 1, + MaxLength: 9, + } + + components = append(components, discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + component, + }, + }) + } + } + + return components +}