Add & use lo[dash], breakup & improve CMD option search/link

This commit is contained in:
2024-02-02 11:46:34 -06:00
parent 4f01635b69
commit 686caa11b4
3 changed files with 49 additions and 37 deletions

View File

@@ -6,10 +6,12 @@ import (
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/samber/lo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/zekroTJA/timedmap" "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 SubmissionContexts = timedmap.New(5 * time.Minute)
var CodeCommandDefinition = &discordgo.ApplicationCommand{ var CodeCommandDefinition = &discordgo.ApplicationCommand{
@@ -122,25 +124,29 @@ func CodeCommandHandler(session *discordgo.Session, interaction *discordgo.Inter
} }
} }
var RegisterCommandDefinition = &discordgo.ApplicationCommand{ var (
Name: "register", LocationOption = &discordgo.ApplicationCommandOption{
Description: "Register a vehicle for parking", Type: discordgo.ApplicationCommandOptionString,
Options: []*discordgo.ApplicationCommandOption{ Name: "location",
{ Description: "The complex to register with",
Type: discordgo.ApplicationCommandOptionString, Required: true,
Name: "location", Autocomplete: true,
Description: "The complex to register with", }
Required: true, GuestCodeOption = &discordgo.ApplicationCommandOption{
Autocomplete: true, 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) { func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
log := logrus.WithFields(logrus.Fields{ log := logrus.WithFields(logrus.Fields{
@@ -160,14 +166,15 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
return return
} }
var form GetFormResult
var code string
var useStoredCode bool 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) userId, parseErr := strconv.Atoi(interaction.Member.User.ID)
if parseErr != nil { if parseErr != nil {
HandleError(session, interaction, parseErr, "Error occurred while parsing user id") 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 // 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 { if !guestCodeProvided && guestCodeCondition == GuestCodeNotRequired {
// A guest code could be stored, so check for it. // 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.") 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 { if guestCodeProvided {
form = GetVipForm(uint(location_id), code) form = GetVipForm(uint(location_id), code)
@@ -207,7 +216,7 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
if form.requireGuestCode { if form.requireGuestCode {
// Handling is the same for both cases, but the message differs & the code is removed if it was stored. // Handling is the same for both cases, but the message differs & the code is removed if it was stored.
if useStoredCode { 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)) RemoveCode(int64(location_id), int(userId))
} else { } else {
HandleError(session, interaction, nil, ":x: This location requires a guest code and the one provided was not valid.") 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 // Convert the form into message components for a modal presented to the user
registrationFormComponents := FormToComponents(form) registrationFormComponents := FormToComponents(form)
// The message ID of the original interaction is used as the identifier for the registration context (uin664) // The message ID of the original interaction is used as the identifier for the registration context (uint64)
registerIdentifier, parseErr := strconv.ParseUint(interaction.Message.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")
} }
requiredFormKeys := make([]string, len(form.fields))
for i, field := range form.fields {
requiredFormKeys[i] = field.id
}
// Store the registration context for later use // Store the registration context for later use
SubmissionContexts.Set(registerIdentifier, &RegisterContext{ SubmissionContexts.Set(registerIdentifier, &RegisterContext{
hiddenKeys: form.hiddenInputs, hiddenKeys: form.hiddenInputs,
propertyId: uint(location_id), propertyId: uint(location_id),
requiredFormKeys: requiredFormKeys, requiredFormKeys: lo.Map(form.fields, func(field Field, _ int) string {
residentId: 0, return field.id
}),
residentId: 0,
}, time.Hour) }, time.Hour)
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{

2
go.mod
View File

@@ -13,9 +13,11 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/joho/godotenv v1.5.1 // indirect
github.com/redis/go-redis/v9 v9.3.0 // 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/sirupsen/logrus v1.9.3 // indirect
github.com/zekroTJA/timedmap v1.5.2 // indirect github.com/zekroTJA/timedmap v1.5.2 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // 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/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect golang.org/x/sys v0.5.0 // indirect
) )

4
go.sum
View File

@@ -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 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 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0=
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= 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 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 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-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 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 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/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-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=