简体   繁体   English

冷却格式 discord.py

[英]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.我有一个命令,我想每 2 小时使用一次,我已经完成了,但是冷却错误是我的问题。 I want to format it like: 1 hour, 30 mins.我想将其格式化为:1 小时 30 分钟。 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:我也试过这个,但它会显示 1.0 小时,30.0 分钟:

@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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