From e99330c09aa99e15486d611e04de7c6d12b1fe45 Mon Sep 17 00:00:00 2001 From: No767 <73260931+No767@users.noreply.github.com> Date: Sun, 21 Jan 2024 01:46:46 -0800 Subject: [PATCH 1/2] Add cooldowns for commands and proper error handling --- bot/cogs/admin.py | 3 ++- bot/cogs/config.py | 2 ++ bot/cogs/tickets.py | 6 ++++++ bot/libs/utils/errors.py | 34 ++++++++++++++++------------------ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/bot/cogs/admin.py b/bot/cogs/admin.py index 15bbec7..7725121 100644 --- a/bot/cogs/admin.py +++ b/bot/cogs/admin.py @@ -10,6 +10,7 @@ from discord.ext import commands from discord.ext.commands import Greedy from libs.utils import RoboContext + from rodhaj import Rodhaj GIT_PULL_REGEX = re.compile(r"\s+(?P.*)\b\s+\|\s+[\d]") @@ -104,7 +105,7 @@ def reload_lib_modules(self, module: str) -> list[tuple[str, str]]: # To learn more about it, see the link below (and ?tag ass on the dpy server): # https://about.abstractumbra.dev/discord.py/2023/01/29/sync-command-example.html @commands.guild_only() - @commands.command(name="sync") + @commands.command(name="sync", hidden=True) async def sync( self, ctx: RoboContext, diff --git a/bot/cogs/config.py b/bot/cogs/config.py index 258b81e..1e5b6a3 100644 --- a/bot/cogs/config.py +++ b/bot/cogs/config.py @@ -127,6 +127,7 @@ async def config(self, ctx: GuildContext) -> None: if ctx.invoked_subcommand is None: await ctx.send_help(ctx.command) + @commands.cooldown(1, 20, commands.BucketType.guild) @config.command(name="setup", usage="ticket_name: log_name: ") async def setup(self, ctx: GuildContext, *, flags: SetupFlags) -> None: """First-time setup for Rodhaj @@ -278,6 +279,7 @@ async def setup(self, ctx: GuildContext, *, flags: SetupFlags) -> None: msg = f"Rodhaj channels successfully created! The ticket channel can be found under {ticket_channel.mention}" await ctx.send(msg) + @commands.cooldown(1, 20, commands.BucketType.guild) @config.command(name="delete") async def delete(self, ctx: GuildContext) -> None: """Permanently deletes Rodhaj channels and tickets.""" diff --git a/bot/cogs/tickets.py b/bot/cogs/tickets.py index 9c09695..6dce81f 100644 --- a/bot/cogs/tickets.py +++ b/bot/cogs/tickets.py @@ -20,6 +20,7 @@ if TYPE_CHECKING: from libs.utils import GuildContext, RoboContext + from rodhaj import Rodhaj @@ -320,6 +321,7 @@ def get_solved_tag( ### Feature commands @is_ticket_or_dm() + @commands.cooldown(1, 20, commands.BucketType.channel) @commands.hybrid_command(name="close", aliases=["solved", "closed", "resolved"]) async def close(self, ctx: RoboContext) -> None: """Closes the thread""" @@ -356,7 +358,11 @@ async def close(self, ctx: RoboContext) -> None: self.get_ticket_owner_id.cache_invalidate(closed_ticket.id) await self.notify_finished_ticket(ctx, owner_id) + # 10 command invocations per 12 seconds for each member + # These values should not be tripped unless someone is spamming + # https://github.com/Rapptz/RoboDanny/blob/rewrite/cogs/mod.py#L524C9-L524C74 @is_ticket_thread() + @commands.cooldown(10, 12, commands.BucketType.member) @commands.command(name="reply", aliases=["r"]) async def reply( self, ctx: GuildContext, *, message: Annotated[str, commands.clean_content] diff --git a/bot/libs/utils/errors.py b/bot/libs/utils/errors.py index ab220f0..196e9ed 100644 --- a/bot/libs/utils/errors.py +++ b/bot/libs/utils/errors.py @@ -29,27 +29,27 @@ def create_premade_embed(title: str, description: str) -> ErrorEmbed: return embed +def build_cooldown_embed(error: commands.CommandOnCooldown) -> ErrorEmbed: + embed = ErrorEmbed() + embed.timestamp = discord.utils.utcnow() + embed.title = "Command On Cooldown" + embed.description = ( + f"This command is on cooldown. Try again in {error.retry_after:.2f}s" + ) + return embed + + async def send_error_embed(ctx: commands.Context, error: commands.CommandError) -> None: - if isinstance(error, commands.CommandInvokeError) or isinstance( + if isinstance(error, commands.CommandOnCooldown): + await ctx.send(embed=build_cooldown_embed(error)) + elif isinstance(error, commands.CommandInvokeError) or isinstance( error, commands.HybridCommandError ): await ctx.send(embed=produce_error_embed(error)) - elif isinstance(error, commands.CommandNotFound): - await ctx.send( + elif isinstance(error, commands.NoPrivateMessage): + await ctx.author.send( embed=create_premade_embed( - "Command not found", - "The command you were looking for could not be found", - ) - ) - elif isinstance(error, commands.NotOwner): - # Basically completely silence it making people not know what happened - return - elif isinstance(error, commands.MissingPermissions): - missing_perms = ", ".join(error.missing_permissions).rstrip(",") - await ctx.send( - embed=create_premade_embed( - "Missing Permissions", - f"You are missing the following permissions: {missing_perms}", + "Guild Only", "This command cannot be used in private messages" ) ) elif isinstance(error, commands.MissingRequiredArgument): @@ -59,5 +59,3 @@ async def send_error_embed(ctx: commands.Context, error: commands.CommandError) f"You are missing the following argument(s): {error.param.name}", ) ) - else: - await ctx.send(embed=produce_error_embed(error)) From ee2fb766ad9d6a1f6c612f3349813b8de09a9508 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Jan 2024 09:51:41 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bot/cogs/admin.py | 1 - bot/cogs/tickets.py | 1 - 2 files changed, 2 deletions(-) diff --git a/bot/cogs/admin.py b/bot/cogs/admin.py index 7725121..9a2dcd7 100644 --- a/bot/cogs/admin.py +++ b/bot/cogs/admin.py @@ -10,7 +10,6 @@ from discord.ext import commands from discord.ext.commands import Greedy from libs.utils import RoboContext - from rodhaj import Rodhaj GIT_PULL_REGEX = re.compile(r"\s+(?P.*)\b\s+\|\s+[\d]") diff --git a/bot/cogs/tickets.py b/bot/cogs/tickets.py index 6dce81f..3ef86b7 100644 --- a/bot/cogs/tickets.py +++ b/bot/cogs/tickets.py @@ -20,7 +20,6 @@ if TYPE_CHECKING: from libs.utils import GuildContext, RoboContext - from rodhaj import Rodhaj