diff options
| author | 2020-09-20 13:27:23 +0200 | |
|---|---|---|
| committer | 2020-09-20 13:27:23 +0200 | |
| commit | 28bfbe65b3fe113948a027970eb7cb22f5b467b9 (patch) | |
| tree | b4a6c2244c201d64e4e853efd79e08bc05456b4f | |
| parent | Remove unit tests for duck pond (diff) | |
Use helper for duckpond's locked relay feature
I've created a helper for duckpond's relay feature to allow me to use it
separately from the command we're planning to add to the Cog. I opted
not to include the lock in the original relay method to separate the
logic more clearly.
Signed-off-by: Sebastiaan Zeeff <[email protected]>
| -rw-r--r-- | bot/cogs/duck_pond.py | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/bot/cogs/duck_pond.py b/bot/cogs/duck_pond.py index 12f4cb7b8..6156c3238 100644 --- a/bot/cogs/duck_pond.py +++ b/bot/cogs/duck_pond.py @@ -105,6 +105,25 @@ class DuckPond(Cog): except discord.HTTPException: log.exception("Failed to send an attachment to the webhook") + async def locked_relay(self, message: discord.Message) -> bool: + """Relay a message after obtaining the relay lock.""" + if self.relay_lock is None: + # Lazily load the lock to ensure it's created within the + # appropriate event loop. + self.relay_lock = asyncio.Lock() + + async with self.relay_lock: + # check if the message has a checkmark after acquiring the lock + if await self.has_green_checkmark(message): + return False + + # relay the message + await self.relay_message(message) + + # add a green checkmark to indicate that the message was relayed + await message.add_reaction("✅") + return True + def _payload_has_duckpond_emoji(self, emoji: discord.PartialEmoji) -> bool: """Test if the RawReactionActionEvent payload contains a duckpond emoji.""" if emoji.is_unicode_emoji(): @@ -150,21 +169,7 @@ class DuckPond(Cog): # If we've got more than the required amount of ducks, send the message to the duck_pond. if duck_count >= constants.DuckPond.threshold: - if self.relay_lock is None: - # Lazily load the lock to ensure it's created within the - # appropriate event loop. - self.relay_lock = asyncio.Lock() - - async with self.relay_lock: - # check if the message has a checkmark after acquiring the lock - if await self.has_green_checkmark(message): - return - - # relay the message - await self.relay_message(message) - - # add a green checkmark to indicate that the message was relayed - await message.add_reaction("✅") + await self.locked_relay(message) @Cog.listener() async def on_raw_reaction_remove(self, payload: RawReactionActionEvent) -> None: |