mirror of
https://github.com/Xevion/banner.git
synced 2025-12-10 18:06:35 -06:00
Setup command deletion & id storage after registration
This commit is contained in:
37
main.go
37
main.go
@@ -164,17 +164,46 @@ func main() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unregister the old command
|
||||||
|
oldCommandId, err := kv.Get(ctx, fmt.Sprintf("%s:command:%s:id", environment, cmdDefinition.Name)).Result()
|
||||||
|
if err != nil {
|
||||||
|
if err != redis.Nil {
|
||||||
|
// It's an unlikely case, but if required, we could get the old command ID from discord to unregister it.
|
||||||
|
log.Err(err).Str("name", cmdDefinition.Name).Str("key", key).Msg("Cannot get old command ID from redis")
|
||||||
|
} else {
|
||||||
|
log.Debug().Str("name", cmdDefinition.Name).Str("key", key).Msg("Old command ID not found in redis")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = session.ApplicationCommandDelete(session.State.User.ID, "", oldCommandId)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Str("name", cmdDefinition.Name).Str("key", key).Msg("Cannot unregister old command")
|
||||||
|
} else {
|
||||||
|
log.Info().Str("name", cmdDefinition.Name).Str("key", key).Msg("Unregistered old command")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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", key).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()
|
|
||||||
if err != nil {
|
|
||||||
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", key).Msg("Registered command")
|
log.Info().Str("name", cmdDefinition.Name).Str("key", key).Msg("Registered command")
|
||||||
|
|
||||||
|
// Store the hash for the new registered command
|
||||||
|
err = kv.Set(ctx, key, hash, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
// No panic - the command is still registered, hash is only to prevent unnecessary registrations
|
||||||
|
log.Err(err).Str("name", cmdDefinition.Name).Str("key", key).Msg("Cannot set command hash in redis")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the command ID to unregister later
|
||||||
|
err = kv.Set(ctx, fmt.Sprintf("%s:command:%s:id", environment, cmdDefinition.Name), cmdInstance.ID, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
// No
|
||||||
|
log.Err(err).Str("name", cmdDefinition.Name).Str("key", key).Msg("Cannot set command ID in redis")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cloes session, ensure http client closes idle connections
|
// Cloes session, ensure http client closes idle connections
|
||||||
|
|||||||
Reference in New Issue
Block a user