繁体   English   中英

冷却格式 discord.py

[英]cooldown format discord.py

我没有在哪里问,所以我会在这里问。 (对不起,如果这是一个重复的问题或任何东西)。

我有一个命令,我想每 2 小时使用一次,我已经完成了,但是冷却错误是我的问题。 我想将其格式化为:1 小时 30 分钟。 我已经尝试过了,但它以分钟为单位,以秒为单位。

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

我也试过这个,但它会显示 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)')

任何人都知道修复或什么? 任何帮助表示赞赏:)

要转换时间,您可以使用以下内容:

    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

要将其实现到您的代码中并发送剩余时间,您只需传递以下内容:

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