Refactor slash command synchronization logic to prevent duplicate commands in Discord. Update environment variables for command sync modes and enhance command clearing functionality. Improve command registration handling in the bot to streamline user experience.
This commit is contained in:
@@ -303,28 +303,29 @@ class Owner(commands.Cog):
|
||||
@commands.hybrid_command(name="syncslash", help="Re-register all slash (/) commands with Discord.")
|
||||
@commands.is_owner()
|
||||
async def syncslash(self, ctx, scope: str = "all"):
|
||||
"""Force-sync slash commands. scope: all | global | guild"""
|
||||
"""Force-sync slash commands. scope: all | global | clear"""
|
||||
if ctx.interaction:
|
||||
await ctx.defer(ephemeral=True)
|
||||
from utils.slash_sync import sync_slash_commands
|
||||
|
||||
scope = (scope or "all").lower().strip()
|
||||
if scope == "global":
|
||||
result = await sync_slash_commands(self.client, guilds=[], global_sync=True)
|
||||
elif scope == "guild":
|
||||
if not ctx.guild:
|
||||
return await ctx.send("Use this in a server, or pass scope `all` / `global`.")
|
||||
result = await sync_slash_commands(
|
||||
self.client, guilds=[ctx.guild], global_sync=False
|
||||
)
|
||||
result = await sync_slash_commands(self.client, guilds=list(self.client.guilds), global_sync=True)
|
||||
elif scope in ("guild", "clear"):
|
||||
# Clear guild-scoped copies in this server (or all) to remove duplicates
|
||||
targets = [ctx.guild] if ctx.guild and scope == "clear" else list(self.client.guilds)
|
||||
if not targets:
|
||||
return await ctx.send("No guild to clear.")
|
||||
result = await sync_slash_commands(self.client, guilds=targets, global_sync=(scope != "clear"))
|
||||
else:
|
||||
result = await sync_slash_commands(self.client)
|
||||
|
||||
guild_n = len(result.get("guilds") or {})
|
||||
cleared = len(result.get("cleared_guilds") or [])
|
||||
err_n = len(result.get("errors") or [])
|
||||
await ctx.send(
|
||||
f"{TICK} Slash sync done — tree `{result.get('local_tree')}`, "
|
||||
f"global `{result.get('global')}`, guilds `{guild_n}`, errors `{err_n}`."
|
||||
f"{TICK} Slash sync done — mode `{result.get('mode')}`, "
|
||||
f"tree `{result.get('local_tree')}`, global `{result.get('global')}`, "
|
||||
f"cleared guilds `{cleared}`, errors `{err_n}`."
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user