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,
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,7 +127,17 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
switch interaction.Type {
case discordgo.InteractionApplicationCommand:
// TODO: Validate license plate
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{
@@ -153,21 +146,50 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
Footer: &discordgo.MessageEmbedFooter{
Text: GetFooterText(),
},
Description: "testing 123",
Fields: []*discordgo.MessageEmbedField{},
Description: "This location requires a guest code to register a vehicle.",
},
},
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{

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
}