Begin switching over to modal-based dynamic fields

This commit is contained in:
2023-12-17 06:05:27 -06:00
parent 5b4da7b0c3
commit 8597308f7c
2 changed files with 117 additions and 36 deletions

View File

@@ -116,26 +116,9 @@ var RegisterCommandDefinition = &discordgo.ApplicationCommand{
}, },
{ {
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
Name: "make", Name: "code",
Description: "Make of Vehicle (e.g. Honda)", Description: "The guest code, if required",
MaxLength: 15, Required: false,
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,
}, },
}, },
} }
@@ -144,30 +127,69 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
switch interaction.Type { switch interaction.Type {
case discordgo.InteractionApplicationCommand: case discordgo.InteractionApplicationCommand:
// TODO: Validate license plate location_id, parse_err := strconv.Atoi(interaction.ApplicationCommandData().Options[0].StringValue())
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ if parse_err != nil {
Type: discordgo.InteractionResponseChannelMessageWithSource, panic(parse_err)
Data: &discordgo.InteractionResponseData{ }
Embeds: []*discordgo.MessageEmbed{
{ form := GetForm(uint(location_id))
Footer: &discordgo.MessageEmbedFooter{ if form.err != nil {
Text: GetFooterText(), 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: case discordgo.InteractionApplicationCommandAutocomplete:
data := interaction.ApplicationCommandData() data := interaction.ApplicationCommandData()
var choices []*discordgo.ApplicationCommandOptionChoice var choices []*discordgo.ApplicationCommandOptionChoice
LocationOption := data.Options[0] LocationOption := data.Options[0]
MakeOption := data.Options[1]
ModelOption := data.Options[2]
switch { switch {
case LocationOption.Focused: case LocationOption.Focused:
@@ -184,9 +206,6 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
Value: strconv.Itoa(int(location.id)), Value: strconv.Itoa(int(location.id)),
} }
} }
case MakeOption.Focused:
case ModelOption.Focused:
} }
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{

62
form.go Normal file
View File

@@ -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
}