Fix interaction identifier, improve focused option finding

This commit is contained in:
2024-02-02 13:18:25 -06:00
parent 254cc2e0b3
commit 2ae78890e9

View File

@@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/zekroTJA/timedmap" "github.com/zekroTJA/timedmap"
@@ -240,9 +241,9 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
} }
// Convert the form into message components for a modal presented to the user // Convert the form into message components for a modal presented to the user
registrationFormComponents := FormToComponents(form) registrationFormComponents := FormToModalComponents(form)
// The message ID of the original interaction is used as the identifier for the registration context (uint64) // The ID of the original interaction is used as the identifier for the registration context (uint64)
registerIdentifier, parseErr := strconv.ParseUint(interaction.ID, 10, 64) registerIdentifier, parseErr := strconv.ParseUint(interaction.ID, 10, 64)
if parseErr != nil { if parseErr != nil {
HandleError(session, interaction, parseErr, "Error occurred while parsing interaction message identifier") HandleError(session, interaction, parseErr, "Error occurred while parsing interaction message identifier")
@@ -255,47 +256,46 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
requiredFormKeys: lo.Map(form.fields, func(field Field, _ int) string { requiredFormKeys: lo.Map(form.fields, func(field Field, _ int) string {
return field.id return field.id
}), }),
residentId: 0, residentId: 0, // TODO: Find where this comes from, what it is, etc.
}, time.Hour) }, time.Hour)
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ registrationFormComponents = append(registrationFormComponents, discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "email",
Label: "Email Address (for confirmation)",
Style: discordgo.TextInputShort,
Required: false,
MinLength: 1,
},
}})
response := discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal, Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
CustomID: "register:" + interaction.Message.ID, CustomID: "register:" + interaction.ID,
Title: "Vehicle Registration", Title: "Vehicle Registration",
Components: registrationFormComponents, Components: registrationFormComponents,
}, },
})
if err != nil {
panic(err)
} }
// TODO: Validate license plate err := session.InteractionRespond(interaction.Interaction, &response)
// session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ if err != nil {
// Type: discordgo.InteractionResponseChannelMessageWithSource, log.WithField("dump", spew.Sdump(response)).Error(err)
// Data: &discordgo.InteractionResponseData{ }
// Embeds: []*discordgo.MessageEmbed{
// {
// Footer: &discordgo.MessageEmbedFooter{
// Text: GetFooterText(),
// },
// Description: "testing 123",
// Fields: []*discordgo.MessageEmbedField{},
// },
// },
// AllowedMentions: &discordgo.MessageAllowedMentions{},
// },
// })
// Autocomplete is used to provide the user with a list of locations to choose from // Autocomplete is used to provide the user with a list of locations to choose from
case discordgo.InteractionApplicationCommandAutocomplete: case discordgo.InteractionApplicationCommandAutocomplete:
data := interaction.ApplicationCommandData() data := interaction.ApplicationCommandData()
var choices []*discordgo.ApplicationCommandOptionChoice var choices []*discordgo.ApplicationCommandOptionChoice
LocationOption := data.Options[0] // Find the focused option
focusedOption, _ := lo.Find(data.Options, func(option *discordgo.ApplicationCommandInteractionDataOption) bool {
return option.Focused
})
switch { switch focusedOption.Name {
case LocationOption.Focused: case LocationOption.Name:
// Seed value is based on the user ID + a 15 minute interval) // Seed value is based on the user ID + a 15 minute interval)
user_id, _ := strconv.Atoi(interaction.Member.User.ID) user_id, _ := strconv.Atoi(interaction.Member.User.ID)
seed_value := int64(user_id) + (time.Now().Unix() / 15 * 60) seed_value := int64(user_id) + (time.Now().Unix() / 15 * 60)
@@ -312,17 +312,7 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
default: default:
// An option was focused, but it does not have a handler. // An option was focused, but it does not have a handler.
var focusedOption *discordgo.ApplicationCommandInteractionDataOption log.WithFields(logrus.Fields{"focusedOption": focusedOption.Name, "focusedOption.value": focusedOption.Value}).Warn("Unhandled autocomplete option")
focusedIndex := 0
for i, option := range data.Options {
if option.Focused {
focusedOption = option
focusedIndex = i
break
}
}
log.WithFields(logrus.Fields{"focusedIndex": focusedIndex, "focusedOption": focusedOption.Name, "focusedOption.value": focusedOption.Value}).Warn("Unhandled autocomplete option")
} }
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
@@ -331,6 +321,7 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
Choices: choices, // This is basically the whole purpose of autocomplete interaction - return custom options to the user. Choices: choices, // This is basically the whole purpose of autocomplete interaction - return custom options to the user.
}, },
}) })
if err != nil { if err != nil {
panic(err) panic(err)
} }