diff options
| author | 2020-08-13 18:49:47 +0200 | |
|---|---|---|
| committer | 2020-08-13 18:57:11 +0200 | |
| commit | 1958978e71dc5bd9e4ae007091db72de147afc12 (patch) | |
| tree | 839c3f9de98101eec1a2343dae2088deb28a81a8 | |
| parent | Verification: improve confirmation message handling (diff) | |
Verification: add `_send_requests` helper
Generic request dispatch method to avoid code duplication with error
handling & bad status logging.
| -rw-r--r-- | bot/cogs/verification.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/bot/cogs/verification.py b/bot/cogs/verification.py index cbf2c51c3..e89f491cf 100644 --- a/bot/cogs/verification.py +++ b/bot/cogs/verification.py @@ -84,6 +84,9 @@ MENTION_UNVERIFIED = discord.AllowedMentions( everyone=False, roles=[discord.Object(constants.Roles.unverified)] ) +# An async function taking a Member param +Request = t.Callable[[discord.Member], t.Awaitable] + def is_verified(member: discord.Member) -> bool: """ @@ -230,6 +233,33 @@ class Verification(Cog): return result + async def _send_requests(self, members: t.Collection[discord.Member], request: Request) -> int: + """ + Pass `members` one by one to `request` handling Discord exceptions. + + This coroutine serves as a generic `request` executor for kicking members and adding + roles, as it allows us to define the error handling logic in one place only. + + Returns the amount of successful requests. Failed requests are logged at info level. + """ + log.info(f"Sending {len(members)} requests") + n_success, bad_statuses = 0, set() + + for member in members: + if is_verified(member): # Member could have verified in the meantime + continue + try: + await request(member) + except discord.HTTPException as http_exc: + bad_statuses.add(http_exc.status) + else: + n_success += 1 + + if bad_statuses: + log.info(f"Failed to send {len(members) - n_success} requests due to following statuses: {bad_statuses}") + + return n_success + async def _kick_members(self, members: t.Collection[discord.Member]) -> int: """ Kick `members` from the PyDis guild. |