Use Discord's relative timestamps as command cooldown countdown (#5893)

Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
Karlo Prikratki 2022-12-29 17:55:11 +01:00 committed by GitHub
parent 0e97c26b2d
commit 9b1171ea8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 34 deletions

View File

@ -2,6 +2,7 @@ import calendar
import logging import logging
import random import random
from collections import defaultdict, deque, namedtuple from collections import defaultdict, deque, namedtuple
from datetime import datetime, timezone, timedelta
from enum import Enum from enum import Enum
from math import ceil from math import ceil
from typing import cast, Iterable, Union, Literal from typing import cast, Iterable, Union, Literal
@ -339,11 +340,13 @@ class Economy(commands.Cog):
) )
else: 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( await ctx.send(
_( _("{author.mention} Too soon. Your next payday is {relative_time}.").format(
"{author.mention} Too soon. For your next payday you have to wait {time}." author=author, relative_time=relative_time
).format(author=author, time=dtime) )
) )
else: else:
# Gets the users latest successfully payday and adds the guilds payday time # Gets the users latest successfully payday and adds the guilds payday time
@ -394,11 +397,13 @@ class Economy(commands.Cog):
) )
) )
else: 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( await ctx.send(
_( _("{author.mention} Too soon. Your next payday is {relative_time}.").format(
"{author.mention} Too soon. For your next payday you have to wait {time}." author=author, relative_time=relative_time
).format(author=author, time=dtime) )
) )
@commands.command() @commands.command()
@ -891,25 +896,3 @@ class Economy(commands.Cog):
num=humanize_number(creds), currency=credits_name, role_name=role.name 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])

View File

@ -317,10 +317,12 @@ def init_events(bot, cli_flags):
new_ctx = await bot.get_context(ctx.message) new_ctx = await bot.get_context(ctx.message)
await bot.invoke(new_ctx) await bot.invoke(new_ctx)
return return
if delay := humanize_timedelta(seconds=error.retry_after): relative_time = discord.utils.format_dt(
msg = _("This command is on cooldown. Try again in {delay}.").format(delay=delay) datetime.now(timezone.utc) + timedelta(seconds=error.retry_after), "R"
else: )
msg = _("This command is on cooldown. Try again in 1 second.") msg = _("This command is on cooldown. Try again {relative_time}.").format(
relative_time=relative_time
)
await ctx.send(msg, delete_after=error.retry_after) await ctx.send(msg, delete_after=error.retry_after)
elif isinstance(error, commands.MaxConcurrencyReached): elif isinstance(error, commands.MaxConcurrencyReached):
if error.per is commands.BucketType.default: if error.per is commands.BucketType.default: