Skip to content

Commit 44d3447

Browse files
HandyS11claude
andcommitted
feat: add ResetCommandsOnStartup flag to clear stale global commands
Adds an opt-in DiscordOptions.ResetCommandsOnStartup toggle. When enabled, DiscordBotService deletes all global application commands on ready before registering the current command set per guild, so leftover commands from a bot that previously used the same Discord application can be cleared. Documents the one-shot usage in docs/development/running-locally.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 059abec commit 44d3447

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

docs/development/running-locally.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@
2525

2626
A missing or empty `Discord:Token` makes the host fail fast at startup with a clear `OptionsValidationException`.
2727

28+
## Clearing stale slash commands
29+
30+
If the Discord application was previously used by another bot, leftover **global** commands can
31+
appear in every guild alongside this bot's commands. Run once with `Discord:ResetCommandsOnStartup`
32+
enabled to delete all global commands at startup before the current commands are registered:
33+
34+
```bash
35+
dotnet run --project src/RustPlusBot.Host -- --Discord:ResetCommandsOnStartup=true
36+
```
37+
38+
The flag also reads from the `Discord__ResetCommandsOnStartup` environment variable or a
39+
`"Discord": { "ResetCommandsOnStartup": true }` entry in configuration. It is a one-shot: once the
40+
stale global commands are gone, leave it off for normal runs. Per-guild commands are already
41+
overwritten on every startup, so a normal run after the reset leaves only the current command set.
42+
2843
## Provisioning the workspace
2944

3045
1. Invite the bot with the **Manage Channels**, **Manage Roles**, **Manage Messages**, and

src/RustPlusBot.Discord/DiscordBotService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ private async Task OnReadyAsync()
6868
return;
6969
}
7070

71+
if (_options.ResetCommandsOnStartup)
72+
{
73+
await client.Rest.DeleteAllGlobalCommandsAsync().ConfigureAwait(false);
74+
logger.LogWarning(
75+
"ResetCommandsOnStartup is enabled: deleted all global application commands before registration.");
76+
}
77+
7178
foreach (var guild in client.Guilds)
7279
{
7380
await interactions.RegisterCommandsToGuildAsync(guild.Id).ConfigureAwait(false);

src/RustPlusBot.Discord/DiscordOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ public sealed class DiscordOptions
55
{
66
/// <summary>The bot token.</summary>
77
public string Token { get; set; } = string.Empty;
8+
9+
/// <summary>
10+
/// When <see langword="true"/>, deletes all global application commands on startup before
11+
/// registering the bot's commands. Use as a one-shot to clear stale commands left by another
12+
/// bot that previously used this application, then leave disabled for normal runs.
13+
/// </summary>
14+
public bool ResetCommandsOnStartup { get; set; }
815
}

0 commit comments

Comments
 (0)