admin.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from discord.ext import commands
  2. import discord
  3. async def setup(bot: commands.Bot):
  4. await bot.add_cog(Admin(bot))
  5. class Admin(commands.Cog):
  6. """Administrative functionality."""
  7. def __init__(self, bot: commands.Bot):
  8. self.bot = bot
  9. @commands.command(
  10. name="gsync",
  11. description="Sync commands globally",
  12. brief="Synchronise commands with all Discord guilds",
  13. help="Update the bot commands for all Discord guilds"
  14. )
  15. async def gsync(self, ctx: commands.Context):
  16. print(self.bot.tree.get_commands())
  17. print(self.bot.tree.fetch_commands())
  18. guild_count = 0
  19. for guild in self.bot.guilds:
  20. fmt = await ctx.bot.tree.sync(guild=guild)
  21. guild_count += 1
  22. await ctx.send(content=f"Synced tree of {len(fmt)} commands to {guild_count}/{len(self.bot.guilds)} guilds", ephemeral=True)
  23. @commands.command(
  24. name="sync",
  25. description="Sync commands",
  26. brief="Synchronise commands with Discord",
  27. help="Update the bot commands"
  28. )
  29. async def sync(self, ctx: commands.Context):
  30. print(self.bot.tree.get_commands())
  31. print(self.bot.tree.fetch_commands())
  32. ctx.bot.tree.copy_global_to(guild=ctx.guild)
  33. synced = await ctx.bot.tree.sync(guild=ctx.guild)
  34. await ctx.send(content=f"Synced {len(synced)} commands to the current guild", ephemeral=True)
  35. @commands.command(
  36. name="testsync",
  37. description="Sync commands",
  38. brief="Synchronise commands with Discord",
  39. help="Update the bot commands"
  40. )
  41. async def testsync(self, ctx: commands.Context):
  42. guild = discord.Object(id=962355492850638939)
  43. ctx.bot.tree.copy_global_to(guild=guild)
  44. synced = await ctx.bot.tree.sync(guild=guild)
  45. await ctx.send(content=f"Synced {len(synced)} commands to the test guild", ephemeral=True)