【问题标题】:cooldown format discord.py冷却格式 discord.py
【发布时间】:2021-05-20 03:18:37
【问题描述】:

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

我有一个命令,我想每 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)')

有人知道解决方法吗?任何帮助表示赞赏:)

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

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

        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)
    

    【讨论】:

    • 你好,我试过了,但由于某种原因它仍然会在几秒钟内显示出来。
    • 即使我的功能内置正确?对我来说效果很好。
    • 是否取决于格式?如果是这样,你是如何格式化的?
    • 编码总是格式敏感的。看看我的新编辑。
    • 我输入了代码,如果我尝试它,它总是告诉我我的等待在异步函数之外..
    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 2021-05-13
    • 2022-01-18
    • 2021-03-30
    • 2021-05-02
    • 2021-04-21
    • 2022-01-16
    相关资源
    最近更新 更多