diff options
author | 2021-05-09 18:02:31 +0200 | |
---|---|---|
committer | 2021-05-09 18:02:31 +0200 | |
commit | 68daca390201f3b20d38ead73d769c51e462d20b (patch) | |
tree | 8ed1876cb153dc2e1cf345ac2edfcb5a162ec625 | |
parent | Nominations: promote the mention regex to a constant (diff) |
Duckpond: make use of count_unique_users_reaction
-rw-r--r-- | bot/exts/fun/duck_pond.py | 20 | ||||
-rw-r--r-- | bot/exts/recruitment/talentpool/_review.py | 14 | ||||
-rw-r--r-- | bot/utils/messages.py | 12 |
3 files changed, 25 insertions, 21 deletions
diff --git a/bot/exts/fun/duck_pond.py b/bot/exts/fun/duck_pond.py index ee440dec2..c78b9c141 100644 --- a/bot/exts/fun/duck_pond.py +++ b/bot/exts/fun/duck_pond.py @@ -9,7 +9,7 @@ from discord.ext.commands import Cog, Context, command from bot import constants from bot.bot import Bot from bot.utils.checks import has_any_role -from bot.utils.messages import send_attachments +from bot.utils.messages import count_unique_users_reaction, send_attachments from bot.utils.webhooks import send_webhook log = logging.getLogger(__name__) @@ -78,18 +78,12 @@ class DuckPond(Cog): Only counts ducks added by staff members. """ - duck_reactors = set() - - # iterate over all reactions - for reaction in message.reactions: - # check if the current reaction is a duck - if not self._is_duck_emoji(reaction.emoji): - continue - - # update the set of reactors with all staff reactors - duck_reactors |= {user.id async for user in reaction.users() if self.is_staff(user)} - - return len(duck_reactors) + return await count_unique_users_reaction( + message, + lambda r: self._is_duck_emoji(r.emoji), + self.is_staff, + False + ) async def relay_message(self, message: Message) -> None: """Relays the message's content and attachments to the duck pond channel.""" diff --git a/bot/exts/recruitment/talentpool/_review.py b/bot/exts/recruitment/talentpool/_review.py index 2df5b2496..10bdac988 100644 --- a/bot/exts/recruitment/talentpool/_review.py +++ b/bot/exts/recruitment/talentpool/_review.py @@ -153,10 +153,18 @@ class Reviewer: seen = await count_unique_users_reaction( messages[0], lambda r: "ducky" in str(r) or str(r) == "\N{EYES}", - False + count_bots=False + ) + upvotes = await count_unique_users_reaction( + messages[0], + lambda r: str(r) == "\N{THUMBS UP SIGN}", + count_bots=False + ) + downvotes = await count_unique_users_reaction( + messages[0], + lambda r: str(r) == "\N{THUMBS DOWN SIGN}", + count_bots=False ) - upvotes = await count_unique_users_reaction(messages[0], lambda r: str(r) == "\N{THUMBS UP SIGN}", False) - downvotes = await count_unique_users_reaction(messages[0], lambda r: str(r) == "\N{THUMBS DOWN SIGN}", False) # Remove the first and last paragraphs stripped_content = content.split("\n\n", maxsplit=1)[1].rsplit("\n\n", maxsplit=1)[0] diff --git a/bot/utils/messages.py b/bot/utils/messages.py index d0d56e273..e886204dd 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -8,7 +8,7 @@ from io import BytesIO from typing import Callable, List, Optional, Sequence, Union import discord -from discord import Message, MessageType, Reaction +from discord import Message, MessageType, Reaction, User from discord.errors import HTTPException from discord.ext.commands import Context @@ -167,20 +167,22 @@ async def send_attachments( async def count_unique_users_reaction( message: discord.Message, - predicate: Callable[[Reaction], bool] = lambda _: True, + reaction_predicate: Callable[[Reaction], bool] = lambda _: True, + user_predicate: Callable[[User], bool] = lambda _: True, count_bots: bool = True ) -> int: """ Count the amount of unique users who reacted to the message. - A predicate function can be passed to check if this reaction should be counted, along with a count_bot flag. + A reaction_predicate function can be passed to check if this reaction should be counted, + another user_predicate to check if the user should also be counted along with a count_bot flag. """ unique_users = set() for reaction in message.reactions: - if predicate(reaction): + if reaction_predicate(reaction): async for user in reaction.users(): - if count_bots or not user.bot: + if (count_bots or not user.bot) and user_predicate(user): unique_users.add(user.id) return len(unique_users) |