docs, provide genuine existence return for GetCode getter func

This commit is contained in:
2024-06-09 22:25:41 -05:00
parent 6453955d96
commit b964bfc565
2 changed files with 10 additions and 4 deletions

View File

@@ -204,9 +204,9 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I
if !guestCodeProvided && guestCodeCondition == GuestCodeNotRequired {
// A guest code could be stored, so check for it.
log.WithField("location", locationId).Debug("No guest code provided for location, but one is not required. Checking for stored code.")
code = GetCode(int64(locationId), int(userId))
code, ok := GetCode(int64(locationId), int(userId))
if code == "" {
if !ok {
// No code was stored, error out.
HandleError(session, interaction, nil, ":x: This location requires a guest code.")
return

10
data.go
View File

@@ -2,6 +2,8 @@ package main
import (
"fmt"
"github.com/go-redis/redis"
)
// LocationExists checks if a location identifier is valid (as known by the cache).
@@ -11,6 +13,7 @@ func LocationExists(location int64) bool {
return ok
}
// StoreCode stores a guest code for a given location and member ID.
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
@@ -19,11 +22,14 @@ func StoreCode(code string, location int64, member_id int) bool {
return already_set
}
func GetCode(location int64, member_id int) string {
// GetCode returns the guest code for a given location and member ID.
func GetCode(location int64, member_id int) (string, bool) {
key := fmt.Sprintf("code:%d:%d", location, member_id)
return db.Get(key).Val()
result := db.Get(key)
return result.Val(), result.Err() == redis.Nil
}
// RemoveCode removes a guest code for a given location and member ID.
func RemoveCode(location int64, member_id int) {
key := fmt.Sprintf("code:%d:%d", location, member_id)
db.Del(key)