From 7e25475b78df01646cbc82176443f955bb6d1964 Mon Sep 17 00:00:00 2001 From: Shirayuki Nekomata Date: Wed, 4 Dec 2019 15:01:40 +0700 Subject: Improved type hinting for `format_infraction_with_duration` --- bot/utils/time.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/utils/time.py b/bot/utils/time.py index a024674ac..9520b32f8 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -113,7 +113,11 @@ def format_infraction(timestamp: str) -> str: return dateutil.parser.isoparse(timestamp).strftime(INFRACTION_FORMAT) -def format_infraction_with_duration(expiry: str, date_from: datetime.datetime = None, max_units: int = 2) -> str: +def format_infraction_with_duration( + expiry: Optional[str], + date_from: datetime.datetime = None, + max_units: int = 2 +) -> Optional[str]: """ Format an infraction timestamp to a more readable ISO 8601 format WITH the duration. -- cgit v1.2.3 From 51f80015c5db9ab8e85ea2304789491d4c72c053 Mon Sep 17 00:00:00 2001 From: Shirayuki Nekomata Date: Wed, 4 Dec 2019 15:03:16 +0700 Subject: Created `until_expiration` to get the remaining time until the infraction expires. --- bot/utils/time.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/bot/utils/time.py b/bot/utils/time.py index 9520b32f8..ac64865d6 100644 --- a/bot/utils/time.py +++ b/bot/utils/time.py @@ -138,3 +138,23 @@ def format_infraction_with_duration( duration_formatted = f" ({duration})" if duration else '' return f"{expiry_formatted}{duration_formatted}" + + +def until_expiration(expiry: Optional[str], max_units: int = 2) -> Optional[str]: + """ + Get the remaining time until infraction's expiration, in a human-readable version of the relativedelta. + + Unlike `humanize_delta`, this function will force the `precision` to be `seconds` by not passing it. + `max_units` specifies the maximum number of units of time to include (e.g. 1 may include days but not hours). + By default, max_units is 2. + """ + if not expiry: + return None + + now = datetime.datetime.utcnow() + since = dateutil.parser.isoparse(expiry).replace(tzinfo=None, microsecond=0) + + if since < now: + return None + + return humanize_delta(relativedelta(since, now), max_units=max_units) -- cgit v1.2.3 From 82eb5e1c46e378a6f3778e17cc342193b910ded5 Mon Sep 17 00:00:00 2001 From: Shirayuki Nekomata Date: Wed, 4 Dec 2019 15:04:20 +0700 Subject: Implemented remaining time until expiration for infraction searching. Will show the remaining time, `Expired.` or `Inactive.` based on the status of the infraction ( It can be inactive but not expired, like an early unmute ) --- bot/cogs/moderation/management.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bot/cogs/moderation/management.py b/bot/cogs/moderation/management.py index abfe5c2b3..2f5e09f1b 100644 --- a/bot/cogs/moderation/management.py +++ b/bot/cogs/moderation/management.py @@ -232,6 +232,12 @@ class ModManagement(commands.Cog): user_id = infraction["user"] hidden = infraction["hidden"] created = time.format_infraction(infraction["inserted_at"]) + + if active: + remaining = time.until_expiration(infraction["expires_at"]) or 'Expired.' + else: + remaining = 'Inactive.' + if infraction["expires_at"] is None: expires = "*Permanent*" else: @@ -247,6 +253,7 @@ class ModManagement(commands.Cog): Reason: {infraction["reason"] or "*None*"} Created: {created} Expires: {expires} + Remaining: {remaining} Actor: {actor.mention if actor else actor_id} ID: `{infraction["id"]}` {"**===============**" if active else "==============="} -- cgit v1.2.3 From c1aeb6d263172168f77845408e8d2756f6cb2813 Mon Sep 17 00:00:00 2001 From: Shirayuki Nekomata Date: Wed, 4 Dec 2019 17:12:25 +0700 Subject: Apply suggestions from Mark - removing `.` at the end and use double quote instead of single. Co-Authored-By: Mark --- bot/cogs/moderation/management.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/cogs/moderation/management.py b/bot/cogs/moderation/management.py index 2f5e09f1b..74f75781d 100644 --- a/bot/cogs/moderation/management.py +++ b/bot/cogs/moderation/management.py @@ -234,9 +234,9 @@ class ModManagement(commands.Cog): created = time.format_infraction(infraction["inserted_at"]) if active: - remaining = time.until_expiration(infraction["expires_at"]) or 'Expired.' + remaining = time.until_expiration(infraction["expires_at"]) or "Expired" else: - remaining = 'Inactive.' + remaining = "Inactive" if infraction["expires_at"] is None: expires = "*Permanent*" -- cgit v1.2.3