简体   繁体   中英

cooldown format discord.py

I didn't where to ask so I will ask here. (Sorry if this os a duplicated question or anything).

I have a command that I want to use every 2 hours, which I have done, but the cooldown error is my issue. I want to format it like: 1 hour, 30 mins. I have tried this but it does the hours in minutes and minutes in seconds.

@command.error
async def command_error(self, ctx, error):
 if isinstance(error, commands.CommandOnCooldown):
 
        cd = round(error.retry_after)
        hours = str(cd // 3600)
        minutes = str(cd % 60)
        await ctx.send(f'You can use this command again in {hours} hour(s), {minutes} minute(s)')

I also tried this but it would display like 1.0 hours, 30.0 mins:

@command.error #thats the name of my command
    async def command_error(self, ctx, error):
     if isinstance(error, commands.CommandOnCooldown):
 
        m, s = divmod(error.retry_after, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 60)
        await ctx.send(f'You can use this command again in {h} hour(s), {m} minute(s)')

Anyone know a fix or something? Any help is appreciated:)

To convert the time you can use something like this:

    def better_time(self, cd:int):
        time = f"{cd}s"
        if cd > 60:
            minutes = cd - (cd % 60)
            seconds = cd - minutes
            minutes = int(minutes/ 60)
            time = f"{minutes}min {seconds}s"
            if minutes > 60:
                hoursglad = minutes -(minutes % 60)
                hours = int(hoursglad/ 60)
                minutes = minutes - (hours*60)
                time = f"{hours}h {minutes}min {seconds}s"
        return time

And to implement it into your code and send the remaining time you can just pass the following:

if isinstance(error, commands.CommandOnCooldown):
    cd = round(error.retry_after)
    if cd == 0:
        cd = 1
    await ctx.send(f"**{ctx.author.mention } - Cooldown, try again in** `{self.better_time(cd)}`**.**", delete_after=3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM