From 334cd4fa2a2d89a0df87d8fa72714edb665db2a3 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Fri, 15 Oct 2021 18:44:12 +0200 Subject: [PATCH] Add optional message to send when bot can't react in `ctx.tick()` and `ctx.react_quietly()` (#4092) * Update context.py * Pre-emptive check to avoid hitting the API --- redbot/core/commands/context.py | 35 ++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/redbot/core/commands/context.py b/redbot/core/commands/context.py index 71434a511..02470d728 100644 --- a/redbot/core/commands/context.py +++ b/redbot/core/commands/context.py @@ -99,35 +99,52 @@ class Context(DPYContext): command = command or self.command await self.bot.send_help_for(self, command) - async def tick(self) -> bool: + async def tick(self, *, message: Optional[str] = None) -> bool: """Add a tick reaction to the command message. + Keyword Arguments + ----------------- + message : str, optional + The message to send if adding the reaction doesn't succeed. + Returns ------- bool :code:`True` if adding the reaction succeeded. """ - try: - await self.message.add_reaction(TICK) - except discord.HTTPException: - return False - else: - return True + return await self.react_quietly(TICK, message=message) async def react_quietly( - self, reaction: Union[discord.Emoji, discord.Reaction, discord.PartialEmoji, str] + self, + reaction: Union[discord.Emoji, discord.Reaction, discord.PartialEmoji, str], + *, + message: Optional[str] = None, ) -> bool: """Adds a reaction to the command message. + Parameters + ---------- + reaction : Union[discord.Emoji, discord.Reaction, discord.PartialEmoji, str] + The emoji to react with. + + Keyword Arguments + ----------------- + message : str, optional + The message to send if adding the reaction doesn't succeed. + Returns ------- bool :code:`True` if adding the reaction succeeded. """ try: + if not self.channel.permissions_for(self.me).add_reactions: + raise RuntimeError await self.message.add_reaction(reaction) - except discord.HTTPException: + except (RuntimeError, discord.HTTPException): + if message is not None: + await self.send(message) return False else: return True