From 686caa11b463b3a7c385a3f4ad1846af3c79aa48 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 2 Feb 2024 11:46:34 -0600 Subject: [PATCH] Add & use lo[dash], breakup & improve CMD option search/link --- commands.go | 80 ++++++++++++++++++++++++++++------------------------- go.mod | 2 ++ go.sum | 4 +++ 3 files changed, 49 insertions(+), 37 deletions(-) diff --git a/commands.go b/commands.go index 86b4a6a..c0f1f65 100644 --- a/commands.go +++ b/commands.go @@ -6,10 +6,12 @@ import ( "time" "github.com/bwmarrin/discordgo" + "github.com/samber/lo" "github.com/sirupsen/logrus" "github.com/zekroTJA/timedmap" ) +// In order for the modal submission to be useful, the context for it's initial request must be stored. var SubmissionContexts = timedmap.New(5 * time.Minute) var CodeCommandDefinition = &discordgo.ApplicationCommand{ @@ -122,25 +124,29 @@ func CodeCommandHandler(session *discordgo.Session, interaction *discordgo.Inter } } -var RegisterCommandDefinition = &discordgo.ApplicationCommand{ - Name: "register", - Description: "Register a vehicle for parking", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "location", - Description: "The complex to register with", - Required: true, - Autocomplete: true, +var ( + LocationOption = &discordgo.ApplicationCommandOption{ + Type: discordgo.ApplicationCommandOptionString, + Name: "location", + Description: "The complex to register with", + Required: true, + Autocomplete: true, + } + GuestCodeOption = &discordgo.ApplicationCommandOption{ + Type: discordgo.ApplicationCommandOptionString, + Name: "code", + Description: "The guest code, if required", + Required: false, + } + RegisterCommandDefinition = &discordgo.ApplicationCommand{ + Name: "register", + Description: "Register a vehicle for parking", + Options: []*discordgo.ApplicationCommandOption{ + LocationOption, + GuestCodeOption, }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "code", - Description: "The guest code, if required", - Required: false, - }, - }, -} + } +) func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) { log := logrus.WithFields(logrus.Fields{ @@ -160,14 +166,15 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I return } - var form GetFormResult - var code string var useStoredCode bool + var code string + + // Check if a guest code was provided (and set it) + _, guestCodeProvided := lo.Find(data.Options, func(option *discordgo.ApplicationCommandInteractionDataOption) bool { + code = option.StringValue() + return option.Name == GuestCodeOption.Name + }) - guestCodeProvided := len(data.Options) > 1 - if guestCodeProvided { - code = data.Options[1].StringValue() - } userId, parseErr := strconv.Atoi(interaction.Member.User.ID) if parseErr != nil { HandleError(session, interaction, parseErr, "Error occurred while parsing user id") @@ -179,7 +186,7 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I // TODO: Add case for when guest code is provided but not required - // Circumstane under which error is certain + // Circumstance under which error is certain if !guestCodeProvided && guestCodeCondition == GuestCodeNotRequired { // A guest code could be stored, so check for it. log.WithField("location", location_id).Debug("No guest code provided for location, but one is not required. Checking for stored code.") @@ -200,6 +207,8 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I } } + // Get the form for the location + var form GetFormResult if guestCodeProvided { form = GetVipForm(uint(location_id), code) @@ -207,7 +216,7 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I if form.requireGuestCode { // Handling is the same for both cases, but the message differs & the code is removed if it was stored. if useStoredCode { - HandleError(session, interaction, nil, ":x: This location requires a guest code and the one stored was not valid & deleted.") + HandleError(session, interaction, nil, ":x: This location requires a guest code and the one stored was not valid (and subsequently deleted).") RemoveCode(int64(location_id), int(userId)) } else { HandleError(session, interaction, nil, ":x: This location requires a guest code and the one provided was not valid.") @@ -233,23 +242,20 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I // Convert the form into message components for a modal presented to the user registrationFormComponents := FormToComponents(form) - // The message ID of the original interaction is used as the identifier for the registration context (uin664) - registerIdentifier, parseErr := strconv.ParseUint(interaction.Message.ID, 10, 64) + // The message ID of the original interaction is used as the identifier for the registration context (uint64) + registerIdentifier, parseErr := strconv.ParseUint(interaction.ID, 10, 64) if parseErr != nil { HandleError(session, interaction, parseErr, "Error occurred while parsing interaction message identifier") } - requiredFormKeys := make([]string, len(form.fields)) - for i, field := range form.fields { - requiredFormKeys[i] = field.id - } - // Store the registration context for later use SubmissionContexts.Set(registerIdentifier, &RegisterContext{ - hiddenKeys: form.hiddenInputs, - propertyId: uint(location_id), - requiredFormKeys: requiredFormKeys, - residentId: 0, + hiddenKeys: form.hiddenInputs, + propertyId: uint(location_id), + requiredFormKeys: lo.Map(form.fields, func(field Field, _ int) string { + return field.id + }), + residentId: 0, }, time.Hour) err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ diff --git a/go.mod b/go.mod index e391582..c2bc173 100644 --- a/go.mod +++ b/go.mod @@ -13,9 +13,11 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/joho/godotenv v1.5.1 // indirect github.com/redis/go-redis/v9 v9.3.0 // indirect + github.com/samber/lo v1.39.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/zekroTJA/timedmap v1.5.2 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect + golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/net v0.7.0 // indirect golang.org/x/sys v0.5.0 // indirect ) diff --git a/go.sum b/go.sum index 029be4a..07c55bc 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/redis/go-redis v6.15.9+incompatible h1:F+tnlesQSl3h9V8DdmtcYFdvkHLhbb github.com/redis/go-redis v6.15.9+incompatible/go.mod h1:ic6dLmR0d9rkHSzaa0Ab3QVRZcjopJ9hSSPCrecj/+s= github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0= github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= +github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -34,6 +36,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8 golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=