mirror of
https://github.com/Xevion/r2park.git
synced 2025-12-06 15:16:05 -06:00
Validate location existence, validate code pattern
This commit is contained in:
12
commands.go
12
commands.go
@@ -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.")
|
||||
|
||||
7
data.go
7
data.go
@@ -4,6 +4,13 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// LocationExists checks if a location identifier is valid (as known by the cache).
|
||||
// Cache rarely will change, so this is a good way to check if a location is valid.
|
||||
func LocationExists(location int64) bool {
|
||||
_, ok := cachedLocationsMap[uint(location)]
|
||||
return ok
|
||||
}
|
||||
|
||||
func StoreCode(code string, location int64, member_id int) bool {
|
||||
key := fmt.Sprintf("code:%d:%d", location, member_id)
|
||||
already_set := db.Exists(key).Val() == 1
|
||||
|
||||
Reference in New Issue
Block a user