0.3.9.6.a5

Minor fix to remove duplicate slash commands
This commit is contained in:
Franz Rolfsvaag 2025-08-11 02:58:25 +02:00
parent 7c9ec713b7
commit eb1e1da82f
3 changed files with 55 additions and 96 deletions

89
bot.py
View File

@ -9,7 +9,7 @@ from modules.common.boot_notice import post_boot_notice
# Version consists of:
# Major.Enhancement.Minor.Patch.Test (Test is alphanumeric; doesnt trigger auto update)
VERSION = "0.3.9.6.a4"
VERSION = "0.3.9.6.a5"
# ---------- Env loading ----------
@ -111,7 +111,7 @@ async def _guild_selfcheck(g: discord.Guild, c):
@bot.event
async def on_ready():
import asyncio # safe inside function; ensures availability
import asyncio
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("[Intents] members:", bot.intents.members,
@ -126,51 +126,73 @@ async def on_ready():
except Exception as e:
print("[SelfCheck] failed:", repr(e))
# Slash command sync (env-aware dev guild) + always print what exists after sync
# ---------- Slash command scope & sync ----------
#
# Toggle here (or set SHAI_SLASH_GUILD_ONLY=true/false):
guild_only = env_cfg.bool("slash_guild_only", True)
# Choose target guilds for "instant" registration
target_gids = set()
for key in ("home_guild_id", "dev_guild_id"):
val = env_cfg.get(key)
if val:
try:
dev_gid = env_cfg.get("dev_guild_id")
target_gids.add(int(val))
except Exception:
pass
if dev_gid:
guild = bot.get_guild(int(dev_gid))
if guild:
synced = await bot.tree.sync(guild=guild)
print(f"[Slash] Synced {len(synced)} commands to {guild.name} ({guild.id})")
else:
synced = await bot.tree.sync()
print(f"[Slash] Synced {len(synced)} commands globally (dev_guild_id not in cache)")
else:
synced = await bot.tree.sync()
print(f"[Slash] Synced {len(synced)} commands globally")
try:
if guild_only and target_gids:
print(f"[Slash] Mode: GUILD-ONLY to {sorted(target_gids)}")
# --- List what actually exists after sync ---
# Copy all currently-loaded global commands to each target guild
for gid in sorted(target_gids):
g = bot.get_guild(gid)
if not g:
print(f"[Slash] Guild {gid}: not in cache; skipping copy/sync.")
continue
bot.tree.copy_global_to(guild=g)
g_cmds = await bot.tree.sync(guild=g)
names = ", ".join(f"/{c.name}" for c in g_cmds) if g_cmds else "(none)"
print(f"[Slash] Synced {len(g_cmds)} commands to {g.name} ({g.id}): {names}")
# Now remove global commands so only guild-scoped remain
bot.tree.clear_commands(guild=None)
cleared = await bot.tree.sync() # push empty global set (purges old global copies)
print(f"[Slash] Cleared global commands (now {len(cleared)}).")
else:
print("[Slash] Mode: GLOBAL")
# Purge any old per-guild copies in target guilds (to avoid dupes),
# then sync globally once.
for gid in sorted(target_gids):
g = bot.get_guild(gid)
if not g:
print(f"[Slash] Guild {gid}: not in cache; skip purge.")
continue
bot.tree.clear_commands(guild=g)
await bot.tree.sync(guild=g)
print(f"[Slash] Purged guild-specific commands in {g.name} ({g.id}).")
global_cmds = await bot.tree.sync()
names = ", ".join(f"/{c.name}" for c in global_cmds) if global_cmds else "(none)"
print(f"[Slash] Synced {len(global_cmds)} commands globally: {names}")
# --- Always print what actually exists after sync ---
def _fmt_cmds(cmds):
try:
names = [f"/{c.name}" for c in cmds]
return ", ".join(names) if names else "(none)"
return ", ".join(f"/{c.name}" for c in cmds) if cmds else "(none)"
except Exception:
return "(unreadable)"
# Global commands
# Global list
try:
global_cmds = await bot.tree.fetch_commands()
print(f"[Slash] Global commands ({len(global_cmds)}): {_fmt_cmds(global_cmds)}")
except Exception as e:
print("[Slash] Failed to fetch global commands:", repr(e))
# Target guilds to inspect: dev + home (if provided)
target_gids = set()
if dev_gid:
try:
target_gids.add(int(dev_gid))
except Exception:
pass
home_gid = env_cfg.get("home_guild_id")
if home_gid:
try:
target_gids.add(int(home_gid))
except Exception:
pass
# Guild lists
for gid in sorted(target_gids):
g = bot.get_guild(gid)
if not g:
@ -181,7 +203,6 @@ async def on_ready():
print(f"[Slash] {g.name} ({g.id}) guild commands ({len(g_cmds)}): {_fmt_cmds(g_cmds)}")
except Exception as e:
print(f"[Slash] Failed to fetch commands for guild {gid}:", repr(e))
# --- end list ---
except Exception as e:
print("[Slash] Sync failed:", repr(e))

View File

@ -649,30 +649,4 @@ class PirateReportCog(commands.Cog):
dm.remove('reports', lambda r: r.get('report_id') == msg.id)
async def setup(bot):
cog = PirateReportCog(bot)
await bot.add_cog(cog)
home_gid = cfg(bot).int('home_guild_id', 0)
guild_obj = discord.Object(id=home_gid) if home_gid else None
def _rm(name: str):
try:
bot.tree.remove_command(name, guild=guild_obj)
except Exception:
try:
bot.tree.remove_command(name, guild=None)
except Exception:
pass
# make re-loads idempotent
for name in ("report", "edit_pirate", "encounter"):
_rm(name)
if home_gid:
bot.tree.add_command(cog.report, guild=guild_obj)
bot.tree.add_command(cog.edit_pirate, guild=guild_obj)
bot.tree.add_command(cog.encounter, guild=guild_obj)
else:
bot.tree.add_command(cog.report)
bot.tree.add_command(cog.edit_pirate)
bot.tree.add_command(cog.encounter)
await bot.add_cog(PirateReportCog(bot))

View File

@ -900,39 +900,3 @@ class SpicePayCog(commands.Cog):
async def setup(bot: commands.Bot):
cog = SpicePayCog(bot)
await bot.add_cog(cog)
# If you use cfg(bot), great; otherwise fall back to DEFAULT.
try:
from modules.common.settings import cfg as _cfg
home_gid = _cfg(bot).int('home_guild_id', 0)
except Exception:
try:
home_gid = int(bot.config['DEFAULT'].get('home_guild_id', '0'))
except Exception:
home_gid = 0
guild_obj = discord.Object(id=home_gid) if home_gid else None
# Make reloads safe: remove if present, then add.
def _rm(name: str):
try:
bot.tree.remove_command(name, guild=guild_obj)
except Exception:
try:
bot.tree.remove_command(name, guild=None)
except Exception:
pass
for name in ("spicepay", "spicepay_resume", "spicepay_cancel", "spicepay_config"):
_rm(name)
if home_gid:
bot.tree.add_command(cog.spicepay, guild=guild_obj)
bot.tree.add_command(cog.spicepay_resume, guild=guild_obj)
bot.tree.add_command(cog.spicepay_cancel, guild=guild_obj)
bot.tree.add_command(cog.spicepay_config, guild=guild_obj)
else:
bot.tree.add_command(cog.spicepay)
bot.tree.add_command(cog.spicepay_resume)
bot.tree.add_command(cog.spicepay_cancel)
bot.tree.add_command(cog.spicepay_config)