Validate location existence, validate code pattern

This commit is contained in:
2024-06-09 18:44:26 -05:00
parent 87e7da60fa
commit 92849b83ba
2 changed files with 16 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"regexp"
"strconv"
"time"
@@ -15,6 +16,8 @@ import (
// 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 codePattern = regexp.MustCompile(`^[a-zA-Z0-9]{4,12}$`)
var CodeCommandDefinition = &discordgo.ApplicationCommand{
Name: "code",
Description: "Set the guest code for a given location",
@@ -51,9 +54,12 @@ func CodeCommandHandler(session *discordgo.Session, interaction *discordgo.Inter
code := data.Options[1].StringValue()
userId, _ := strconv.Atoi(interaction.Member.User.ID)
// TODO: Validate that the location exists
// TODO: Validate that the code has no invalid characters
already_set := StoreCode(code, int64(location_id), user_id)
// Validate that the location exists
if !LocationExists(int64(locationId)) {
HandleError(session, interaction, nil, "The location provided does not exist.")
return
}
// Validate that the code has no invalid characters
if !codePattern.MatchString(code) {
HandleError(session, interaction, nil, "The code provided contains invalid characters.")