From 9b1171ea8cc6765903d03f2d04ea4cba5704a6cb Mon Sep 17 00:00:00 2001 From: Karlo Prikratki Date: Thu, 29 Dec 2022 17:55:11 +0100 Subject: [PATCH] Use Discord's relative timestamps as command cooldown countdown (#5893) Co-authored-by: Jakub Kuczys --- redbot/cogs/economy/economy.py | 43 ++++++++++------------------------ redbot/core/events.py | 10 ++++---- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/redbot/cogs/economy/economy.py b/redbot/cogs/economy/economy.py index 973884c06..f3d6ded78 100644 --- a/redbot/cogs/economy/economy.py +++ b/redbot/cogs/economy/economy.py @@ -2,6 +2,7 @@ import calendar import logging import random from collections import defaultdict, deque, namedtuple +from datetime import datetime, timezone, timedelta from enum import Enum from math import ceil from typing import cast, Iterable, Union, Literal @@ -339,11 +340,13 @@ class Economy(commands.Cog): ) else: - dtime = self.display_time(next_payday - cur_time) + relative_time = discord.utils.format_dt( + datetime.now(timezone.utc) + timedelta(seconds=next_payday - cur_time), "R" + ) await ctx.send( - _( - "{author.mention} Too soon. For your next payday you have to wait {time}." - ).format(author=author, time=dtime) + _("{author.mention} Too soon. Your next payday is {relative_time}.").format( + author=author, relative_time=relative_time + ) ) else: # Gets the users latest successfully payday and adds the guilds payday time @@ -394,11 +397,13 @@ class Economy(commands.Cog): ) ) else: - dtime = self.display_time(next_payday - cur_time) + relative_time = discord.utils.format_dt( + datetime.now(timezone.utc) + timedelta(seconds=next_payday - cur_time), "R" + ) await ctx.send( - _( - "{author.mention} Too soon. For your next payday you have to wait {time}." - ).format(author=author, time=dtime) + _("{author.mention} Too soon. Your next payday is {relative_time}.").format( + author=author, relative_time=relative_time + ) ) @commands.command() @@ -891,25 +896,3 @@ class Economy(commands.Cog): num=humanize_number(creds), currency=credits_name, role_name=role.name ) ) - - # What would I ever do without stackoverflow? - @staticmethod - def display_time(seconds, granularity=2): - intervals = ( # Source: http://stackoverflow.com/a/24542445 - (_("weeks"), 604800), # 60 * 60 * 24 * 7 - (_("days"), 86400), # 60 * 60 * 24 - (_("hours"), 3600), # 60 * 60 - (_("minutes"), 60), - (_("seconds"), 1), - ) - - result = [] - - for name, count in intervals: - value = seconds // count - if value: - seconds -= value * count - if value == 1: - name = name.rstrip("s") - result.append("{} {}".format(value, name)) - return ", ".join(result[:granularity]) diff --git a/redbot/core/events.py b/redbot/core/events.py index bf2b45643..3811f38be 100644 --- a/redbot/core/events.py +++ b/redbot/core/events.py @@ -317,10 +317,12 @@ def init_events(bot, cli_flags): new_ctx = await bot.get_context(ctx.message) await bot.invoke(new_ctx) return - if delay := humanize_timedelta(seconds=error.retry_after): - msg = _("This command is on cooldown. Try again in {delay}.").format(delay=delay) - else: - msg = _("This command is on cooldown. Try again in 1 second.") + relative_time = discord.utils.format_dt( + datetime.now(timezone.utc) + timedelta(seconds=error.retry_after), "R" + ) + msg = _("This command is on cooldown. Try again {relative_time}.").format( + relative_time=relative_time + ) await ctx.send(msg, delete_after=error.retry_after) elif isinstance(error, commands.MaxConcurrencyReached): if error.per is commands.BucketType.default: