From b964bfc5656b8008f75abd8a58f2a2cdfabaa7f1 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 9 Jun 2024 22:25:41 -0500 Subject: [PATCH] docs, provide genuine existence return for GetCode getter func --- commands.go | 4 ++-- data.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index e871130..0108659 100644 --- a/commands.go +++ b/commands.go @@ -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 diff --git a/data.go b/data.go index 6bff94e..929c56e 100644 --- a/data.go +++ b/data.go @@ -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)