mirror of
https://github.com/Xevion/r2park.git
synced 2025-12-06 23:15:58 -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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"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.
|
// 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 codePattern = regexp.MustCompile(`^[a-zA-Z0-9]{4,12}$`)
|
||||||
|
|
||||||
var CodeCommandDefinition = &discordgo.ApplicationCommand{
|
var CodeCommandDefinition = &discordgo.ApplicationCommand{
|
||||||
Name: "code",
|
Name: "code",
|
||||||
Description: "Set the guest code for a given location",
|
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()
|
code := data.Options[1].StringValue()
|
||||||
userId, _ := strconv.Atoi(interaction.Member.User.ID)
|
userId, _ := strconv.Atoi(interaction.Member.User.ID)
|
||||||
|
|
||||||
// TODO: Validate that the location exists
|
// Validate that the location exists
|
||||||
// TODO: Validate that the code has no invalid characters
|
if !LocationExists(int64(locationId)) {
|
||||||
already_set := StoreCode(code, int64(location_id), user_id)
|
HandleError(session, interaction, nil, "The location provided does not exist.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Validate that the code has no invalid characters
|
// Validate that the code has no invalid characters
|
||||||
if !codePattern.MatchString(code) {
|
if !codePattern.MatchString(code) {
|
||||||
HandleError(session, interaction, nil, "The code provided contains invalid characters.")
|
HandleError(session, interaction, nil, "The code provided contains invalid characters.")
|
||||||
|
|||||||
7
data.go
7
data.go
@@ -4,6 +4,13 @@ import (
|
|||||||
"fmt"
|
"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 {
|
func StoreCode(code string, location int64, member_id int) bool {
|
||||||
key := fmt.Sprintf("code:%d:%d", location, member_id)
|
key := fmt.Sprintf("code:%d:%d", location, member_id)
|
||||||
already_set := db.Exists(key).Val() == 1
|
already_set := db.Exists(key).Val() == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user