Skip to content

Commit

Permalink
feat(config): add REREGISTER_COMMANDS env var to delete and re-regist…
Browse files Browse the repository at this point in the history
…er commands when necessary
  • Loading branch information
tyzbit committed Dec 16, 2024
1 parent 3c88af1 commit 698c281
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ Set some environment variables before launching, or add a `.env` file.
If database environment variables are provided, the bot will save configuration to an external database.
Otherwise, it will save configuration to a local sqlite database at `/var/go-discord-archiver/local.db`

| Variable | Value(s) |
| :---------------- | :------------------------------------------------------------------------------------- |
| ADMINISTRATOR_IDS | Comma separated IDs of users allowed to use administrator commands |
| DB_NAME | Database name for database |
| DB_HOST | Hostname for database |
| DB_PASSWORD | Password for database user |
| DB_USER | Username for database user |
| LOG_LEVEL | `trace`, `debug`, `info`, `warn`, `error` |
| COOKIE | Archive.org login cookie, get this from a web browser's Dev Tools visiting Archive.org |
| TOKEN | The Discord token the bot should use |
| Variable | Value(s) |
| :------------------ | :------------------------------------------------------------------------------------- |
| DB_NAME | Database name for database |
| DB_HOST | Hostname for database |
| DB_PASSWORD | Password for database user |
| DB_USER | Username for database user |
| REREGISTER_COMMANDS | Delete and re-register commands. Only use when command names are changed, unset after |
| LOG_LEVEL | `trace`, `debug`, `info`, `warn`, `error` |
| COOKIE | Archive.org login cookie, get this from a web browser's Dev Tools visiting Archive.org |
| TOKEN | The Discord token the bot should use |

## Usage

Expand Down
18 changes: 13 additions & 5 deletions bot/discord_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ func (bot *ArchiverBot) BotReadyHandler(s *discordgo.Session, r *discordgo.Ready

// Use this to clean up commands if IDs have changed
// TODO remove later if unnecessary
// log.Debug("removing all commands")
// bot.deleteAllCommands()
// var err error
// globals.RegisteredCommands, err = bot.DG.ApplicationCommandBulkOverwrite(bot.DG.State.User.ID, "", globals.Commands)
log.Debug("registering slash commands")
if bot.Config.ReregisterAllCommands {
envVar := getTagValue(ArchiverBotConfig{}, "ReregisterAllCommands", "env")
log.Warn(envVar + " is enabled, commands will be re-registered on every startup " +
"(not advised, existing commands break for users and you will get rate-limited)")
log.Info("re-registering slash commands")
bot.deleteAllCommands()
_, err := bot.DG.ApplicationCommandBulkOverwrite(bot.DG.State.User.ID, "", globals.Commands)
if err != nil {
log.Errorf("error re-registering commands, err: %s", err)
}
} else {
log.Debug("updating registered slash commands")
}
registeredCommands, err := bot.DG.ApplicationCommands(bot.DG.State.User.ID, "")
if err != nil {
log.Errorf("unable to look up registered application commands, err: %s", err)
Expand Down
16 changes: 8 additions & 8 deletions bot/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ type ArchiverBot struct {
// ArchiverBotConfig is attached to ArchiverBot so config settings can be
// accessed easily
type ArchiverBotConfig struct {
AdminIds []string `env:"ADMINISTRATOR_IDS"`
DBHost string `env:"DB_HOST"`
DBName string `env:"DB_NAME"`
DBPassword string `env:"DB_PASSWORD"`
DBUser string `env:"DB_USER"`
LogLevel string `env:"LOG_LEVEL"`
Token string `env:"TOKEN"`
Cookie string `env:"COOKIE"`
DBHost string `env:"DB_HOST"`
DBName string `env:"DB_NAME"`
DBPassword string `env:"DB_PASSWORD"`
DBUser string `env:"DB_USER"`
ReregisterAllCommands bool `env:"REREGISTER_COMMANMDS"`
LogLevel string `env:"LOG_LEVEL"`
Token string `env:"TOKEN"`
Cookie string `env:"COOKIE"`
}

// Servers
Expand Down

0 comments on commit 698c281

Please sign in to comment.