Use key directly, skip base64

This commit is contained in:
2023-12-24 22:39:30 -06:00
parent bebf017ae3
commit 96efd4bdf2

16
main.go
View File

@@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
@@ -170,36 +169,37 @@ func main() {
// Register commands // Register commands
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions)) registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
for i, cmdDefinition := range commandDefinitions { for i, cmdDefinition := range commandDefinitions {
// Compare the hash to the hash stored in redis // Create a hash of the command definition
hash := xxhash.Sum64(structhash.Dump(cmdDefinition, 1)) hash := xxhash.Sum64(structhash.Dump(cmdDefinition, 1))
key := fmt.Sprintf("command:%s:xxhash", cmdDefinition.Name) key := fmt.Sprintf("command:%s:xxhash", cmdDefinition.Name)
keyB64 := base64.StdEncoding.EncodeToString([]byte(key))
// Get the stored hash
storedHash, err := kv.Get(ctx, key).Uint64() storedHash, err := kv.Get(ctx, key).Uint64()
if err != nil { if err != nil {
if err != redis.Nil { if err != redis.Nil {
log.Err(err).Msg("Cannot get command hash from redis") log.Err(err).Msg("Cannot get command hash from redis")
} else { } else {
log.Debug().Str("command", cmdDefinition.Name).Str("key", keyB64).Msg("Command hash not found in redis") log.Debug().Str("command", cmdDefinition.Name).Str("key", key).Msg("Command hash not found in redis")
} }
} }
// If the hash is the same, skip registering the command // If the hash is the same, skip registering the command
if hash == storedHash { if hash == storedHash {
log.Debug().Str("command", cmdDefinition.Name).Str("key", keyB64).Msg("Command hash matches, skipping registration") log.Debug().Str("command", cmdDefinition.Name).Str("key", key).Msg("Command hash matches, skipping registration")
continue continue
} }
// Register the command // Register the command
cmdInstance, err := session.ApplicationCommandCreate(session.State.User.ID, "", cmdDefinition) cmdInstance, err := session.ApplicationCommandCreate(session.State.User.ID, "", cmdDefinition)
if err != nil { if err != nil {
log.Panic().Err(err).Str("name", cmdDefinition.Name).Str("key", keyB64).Msg("Cannot register command") log.Panic().Err(err).Str("name", cmdDefinition.Name).Str("key", key).Msg("Cannot register command")
} }
err = kv.Set(ctx, key, hash, 0).Err() err = kv.Set(ctx, key, hash, 0).Err()
if err != nil { if err != nil {
log.Err(err).Str("name", cmdDefinition.Name).Str("key", keyB64).Msg("Cannot set command hash in redis") log.Err(err).Str("name", cmdDefinition.Name).Str("key", key).Msg("Cannot set command hash in redis")
} }
registeredCommands[i] = cmdInstance registeredCommands[i] = cmdInstance
log.Info().Str("name", cmdDefinition.Name).Str("key", keyB64).Msg("Registered command") log.Info().Str("name", cmdDefinition.Name).Str("key", key).Msg("Registered command")
} }
// Cloes session, ensure http client closes idle connections // Cloes session, ensure http client closes idle connections