clean up leftover guild commands in prod and warn about duplicates in dev
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9d0b349023
commit
52bbf74e47
1 file changed
+15
-3
+15
-3
@@ -74,12 +74,24 @@ class Bot(commands.Bot):
|
||||
name = file[:-3]
|
||||
await self.load_extension(f"cogs.{name}")
|
||||
|
||||
# Sync slash commands
|
||||
# Sync slash commands. A sync only replaces the commands in its own scope, so commands left
|
||||
# in the other scope (global or guild) from running in the other mode show up as duplicates
|
||||
guild = discord.Object(id=self.guild_id)
|
||||
if os.environ.get("BOT_CONFIG_MODE") == "prod":
|
||||
await self.tree.sync()
|
||||
try:
|
||||
self.tree.clear_commands(guild=guild)
|
||||
await self.tree.sync(guild=guild)
|
||||
except discord.HTTPException as e:
|
||||
self.logger.warning(f"Could not remove leftover guild commands in {self.guild_id}: {e}")
|
||||
else:
|
||||
self.tree.copy_global_to(guild=discord.Object(id=self.guild_id))
|
||||
await self.tree.sync(guild=discord.Object(id=self.guild_id))
|
||||
self.tree.copy_global_to(guild=guild)
|
||||
await self.tree.sync(guild=guild)
|
||||
if global_commands := await self.tree.fetch_commands():
|
||||
self.logger.warning(
|
||||
f"This bot application also has {len(global_commands)} global slash commands, which show up "
|
||||
+ "twice in the dev guild. Use a separate bot application for development"
|
||||
)
|
||||
|
||||
async def __aexit__(self, *args):
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user