mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Use Discord's relative timestamps as command cooldown countdown (#5893)
Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
parent
0e97c26b2d
commit
9b1171ea8c
@ -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])
|
|
||||||
|
|||||||
@ -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:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user