aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-07-12 03:06:00 -0500
committerParker <contact@pkrm.dev>2024-07-12 03:06:00 -0500
commitdfc95e470cf73a560e1a4c2cd6b6f1386b780c73 (patch)
treeb19e8f7a7b791dfad4ed512215bf4742740f7918
parent0cd223c3a52ae630610a5ec99f02df94587fbed8 (diff)
Overhaul `sync` command
-rw-r--r--code/cogs/owner/sync.py59
-rw-r--r--code/cogs/owner/tree_sync.py33
2 files changed, 59 insertions, 33 deletions
diff --git a/code/cogs/owner/sync.py b/code/cogs/owner/sync.py
new file mode 100644
index 0000000..a2d8c0e
--- /dev/null
+++ b/code/cogs/owner/sync.py
@@ -0,0 +1,59 @@
+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(f"This is a group command. You either `{self.bot.command_prefix}tree sync` or `{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(f"Synced the command tree to `{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(f"Cleared the command tree from `{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))
diff --git a/code/cogs/owner/tree_sync.py b/code/cogs/owner/tree_sync.py
deleted file mode 100644
index 5050730..0000000
--- a/code/cogs/owner/tree_sync.py
+++ /dev/null
@@ -1,33 +0,0 @@
-from discord.ext import commands
-from discord import Object
-
-
-class TreeSync(commands.Cog):
- def __init__(self, bot):
- self.bot = bot
-
- @commands.command()
- @commands.dm_only()
- @commands.is_owner()
- async def sync(self, ctx: commands.Context, *, guild: Object = None) -> None:
- if not guild or guild == None:
- await self.bot.tree.sync()
- await ctx.author.send("Synced commands globally")
- return
-
- elif guild != None:
- self.bot.tree.copy_global_to(guild=guild)
- await self.bot.tree.sync(guild=guild)
-
- await ctx.author.send(f"Synced the tree to 1 test guild.")
-
- @sync.error
- async def error_sync(self, ctx, error):
- if isinstance(error, commands.errors.PrivateMessageOnly):
- pass
- else:
- await ctx.author.send("That is not a valid guild ID")
-
-
-async def setup(bot):
- await bot.add_cog(TreeSync(bot))