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
This commit is contained in:
jack1142 2021-10-15 18:44:12 +02:00 committed by GitHub
parent a8f35f762c
commit 334cd4fa2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -99,35 +99,52 @@ class Context(DPYContext):
command = command or self.command command = command or self.command
await self.bot.send_help_for(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. """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 Returns
------- -------
bool bool
:code:`True` if adding the reaction succeeded. :code:`True` if adding the reaction succeeded.
""" """
try: return await self.react_quietly(TICK, message=message)
await self.message.add_reaction(TICK)
except discord.HTTPException:
return False
else:
return True
async def react_quietly( 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: ) -> bool:
"""Adds a reaction to the command message. """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 Returns
------- -------
bool bool
:code:`True` if adding the reaction succeeded. :code:`True` if adding the reaction succeeded.
""" """
try: try:
if not self.channel.permissions_for(self.me).add_reactions:
raise RuntimeError
await self.message.add_reaction(reaction) await self.message.add_reaction(reaction)
except discord.HTTPException: except (RuntimeError, discord.HTTPException):
if message is not None:
await self.send(message)
return False return False
else: else:
return True return True