diff options
author | Parker <contact@pkrm.dev> | 2025-04-02 18:52:00 -0500 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2025-04-02 18:52:00 -0500 |
commit | b5099937f84e2fb58b69d97b4b8fff17363fe7d9 (patch) | |
tree | ce0626d12ad3daf9bf2b7c103914bf7411ea245e /src/cogs/owner | |
parent | ae1ac1d731813008682ccba70db0f228b15a72e3 (diff) |
First commit. Lots of stuff
Diffstat (limited to 'src/cogs/owner')
-rw-r--r-- | src/cogs/owner/sync.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/cogs/owner/sync.py b/src/cogs/owner/sync.py new file mode 100644 index 0000000..d6647b5 --- /dev/null +++ b/src/cogs/owner/sync.py @@ -0,0 +1,87 @@ +import discord +from discord.ext import commands + + +class TreeSync(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.group(invoke_without_command=True) + @commands.dm_only() + @commands.is_owner() + async def tree(self, ctx): + await ctx.author.send( + "This is a group command. Use either" + f" `{self.bot.command_prefix}tree sync` or" + f" `{self.bot.command_prefix}tree clear` followed by an optional" + " guild ID." + ) + + @commands.dm_only() + @commands.is_owner() + @tree.command() + async def sync( + self, ctx: commands.Context, *, guild: discord.Object = None + ): + """Sync the command tree to a guild or globally.""" + if guild: + self.bot.tree.copy_global_to(guild=guild) + await self.bot.tree.sync(guild=guild) + return await ctx.author.send( + "Synced the command tree to" + f" `{self.bot.get_guild(guild.id).name}`" + ) + else: + await self.bot.tree.sync() + return await ctx.author.send("Synced the command tree globally.") + + @sync.error + async def tree_sync_error(self, ctx, error): + if isinstance(error, commands.ObjectNotFound): + return await ctx.author.send( + "The guild you provided does not exist." + ) + if isinstance(error, commands.CommandInvokeError): + return await ctx.author.send( + "Guild ID provided is not a guild that the bot is in." + ) + else: + return await ctx.author.send( + "An unknown error occurred. Perhaps you've been rate limited." + ) + + @commands.dm_only() + @commands.is_owner() + @tree.command() + async def clear(self, ctx: commands.Context, *, guild: discord.Object): + """Clear the command tree from a guild.""" + self.bot.tree.clear_commands(guild=guild) + await self.bot.tree.sync(guild=guild) + return await ctx.author.send( + "Cleared the command tree from" + f" `{self.bot.get_guild(guild.id).name}`" + ) + + @clear.error + async def tree_sync_error(self, ctx, error): + if isinstance(error, commands.MissingRequiredArgument): + return await ctx.author.send( + "You need to provide a guild ID to clear the command tree" + " from." + ) + if isinstance(error, commands.ObjectNotFound): + return await ctx.author.send( + "The guild you provided does not exist." + ) + if isinstance(error, commands.CommandInvokeError): + return await ctx.author.send( + "Guild ID provided is not a guild that the bot is in." + ) + else: + return await ctx.author.send( + "An unknown error occurred. Perhaps you've been rate limited." + ) + + +async def setup(bot): + await bot.add_cog(TreeSync(bot)) |