diff options
author | 2022-10-23 15:14:00 +0100 | |
---|---|---|
committer | 2022-10-23 15:14:00 +0100 | |
commit | e221270b550e9a888819676593dfd256112a7f7b (patch) | |
tree | ef0e3275ba82ee992eaa0425c48ea2d5820cba3d | |
parent | Various improvements and fixes (diff) |
Refactor some commands to avoid unnecessary API calls
Also fixed a docstring and renamed a variable
-rw-r--r-- | bot/exts/recruitment/talentpool/_api.py | 2 | ||||
-rw-r--r-- | bot/exts/recruitment/talentpool/_cog.py | 68 | ||||
-rw-r--r-- | bot/exts/recruitment/talentpool/_review.py | 12 |
3 files changed, 34 insertions, 48 deletions
diff --git a/bot/exts/recruitment/talentpool/_api.py b/bot/exts/recruitment/talentpool/_api.py index c89fa0875..2cb15a14d 100644 --- a/bot/exts/recruitment/talentpool/_api.py +++ b/bot/exts/recruitment/talentpool/_api.py @@ -67,7 +67,7 @@ class NominationAPI: reviewed: bool | None = None, ) -> Nomination: """ - Edit a nomination entry. + Edit a nomination. Passing a value of `None` indicates it shouldn't be updated. """ diff --git a/bot/exts/recruitment/talentpool/_cog.py b/bot/exts/recruitment/talentpool/_cog.py index f1614f284..94737fc6c 100644 --- a/bot/exts/recruitment/talentpool/_cog.py +++ b/bot/exts/recruitment/talentpool/_cog.py @@ -236,12 +236,12 @@ class TalentPool(Cog, name="Talentpool"): try: await self.api.post_nomination(user.id, ctx.author.id, reason) - except ResponseCodeError as err: - if err.status == 400: - if err.response_json.get("user", False): + except ResponseCodeError as e: + match (e.status, e.response_json): + case (400, {"user": _}): await ctx.send(f":x: {user.mention} can't be found in the database tables.") return - elif err.response_json.get('actor', False): + case (400, {"actor": _}): await ctx.send(f":x: You have already nominated {user.mention}.") return raise @@ -351,6 +351,7 @@ class TalentPool(Cog, name="Talentpool"): if len(reason) > REASON_MAX_CHARS: await ctx.send(f":x: The reason's length must not exceed {REASON_MAX_CHARS} characters.") return + if isinstance(target, int): nomination_id = target else: @@ -361,27 +362,20 @@ class TalentPool(Cog, name="Talentpool"): await ctx.send(f":x: {target.mention} doesn't have an active nomination.") return + log.trace(f"Changing reason for nomination with id {nomination_id} of actor {actor} to {repr(reason)}") + try: - nomination = await self.api.get_nomination(nomination_id) + nomination = await self.api.edit_nomination_entry(nomination_id, actor_id=actor.id, reason=reason) except ResponseCodeError as e: - if e.response.status == 404: - log.trace(f"Nomination API 404: Can't find a nomination with id {nomination_id}") - await ctx.send(f":x: Can't find a nomination with id `{nomination_id}`.") - return - else: - raise - - if not nomination.active: - await ctx.send(f":x: <@{nomination.user_id}> doesn't have an active nomination.") - return - - if not any(entry.actor_id == actor.id for entry in nomination.entries): - await ctx.send(f":x: {actor.mention} doesn't have an entry in this nomination.") - return - - log.trace(f"Changing reason for nomination with id {nomination_id} of actor {actor} to {repr(reason)}") + match (e.status, e.response_json): + case (400, {"actor": _}): + await ctx.send(f":x: {actor.mention} doesn't have an entry in this nomination.") + return + case (404, _): + await ctx.send(f":x: Can't find a nomination with id `{target}`.") + return + raise - await self.api.edit_nomination_entry(nomination_id, actor_id=actor.id, reason=reason) await ctx.send(f":white_check_mark: Updated the nomination reason for <@{nomination.user_id}>.") @nomination_edit_group.command(name='end_reason') @@ -392,25 +386,19 @@ class TalentPool(Cog, name="Talentpool"): await ctx.send(f":x: The reason's length must not exceed {REASON_MAX_CHARS} characters.") return + log.trace(f"Changing end reason for nomination with id {nomination_id} to {repr(reason)}") try: - nomination = await self.api.get_nomination(nomination_id) + nomination = await self.api.edit_nomination(nomination_id, end_reason=reason) except ResponseCodeError as e: - if e.response.status == 404: - log.trace(f"Nomination API 404: Can't find a nomination with id {nomination_id}") - await ctx.send(f":x: Can't find a nomination with id `{nomination_id}`.") - return - else: - raise - - if nomination.active: - await ctx.send( - f":x: Can't edit the nomination end reason for <@{nomination.user_id}> because it's still active." - ) - return - - log.trace(f"Changing end reason for nomination with id {nomination_id} to {repr(reason)}") + match (e.status, e.response_json): + case (400, {"end_reason": _}): + await ctx.send(f":x: Can't edit nomination with id `{nomination_id}` because it's still active.") + return + case (404, _): + await ctx.send(f":x: Can't find a nomination with id `{nomination_id}`.") + return + raise - await self.api.edit_nomination(nomination_id, end_reason=reason) await ctx.send(f":white_check_mark: Updated the nomination end reason for <@{nomination.user_id}>.") @nomination_group.command(aliases=('gr',)) @@ -500,10 +488,8 @@ class TalentPool(Cog, name="Talentpool"): entries_string = "\n\n".join(entries) - active = nomination.active - start_date = time.discord_timestamp(nomination.inserted_at) - if active: + if nomination.active: lines = textwrap.dedent( f""" =============== diff --git a/bot/exts/recruitment/talentpool/_review.py b/bot/exts/recruitment/talentpool/_review.py index 1ea46d158..a74e1ce2b 100644 --- a/bot/exts/recruitment/talentpool/_review.py +++ b/bot/exts/recruitment/talentpool/_review.py @@ -119,7 +119,7 @@ class Reviewer: """ now = datetime.now(timezone.utc) - possible: list[Nomination] = [] + possible_nominations: list[Nomination] = [] nominations = await self.api.get_nominations(active=True) for nomination in nominations: time_since_nomination = now - nomination.inserted_at @@ -127,19 +127,19 @@ class Reviewer: not nomination.reviewed and time_since_nomination > MIN_NOMINATION_TIME ): - possible.append(nomination) + possible_nominations.append(nomination) - if not possible: + if not possible_nominations: log.debug("No users ready to review.") return None - oldest_date = min(nom.inserted_at for nom in possible) - max_entries = max(len(nom.entries) for nom in possible) + oldest_date = min(nomination.inserted_at for nomination in possible_nominations) + max_entries = max(len(nomination.entries) for nomination in possible_nominations) def sort_key(nomination: Nomination) -> float: return self.score_nomination(nomination, oldest_date, now, max_entries) - return max(possible, key=sort_key) + return max(possible_nominations, key=sort_key) @staticmethod def score_nomination(nomination: Nomination, oldest_date: datetime, now: datetime, max_entries: int) -> float: |