首页 > 解决方案 > 冷却错误时如何在 discord.py 命令中将秒转换为分钟或小时

问题描述

@client.event
async def on_command_error(ctx,error):
  if isinstance(error , commands.CommandOnCooldown):
    await ctx.send(f'{ctx.author.mention}**hold your horses buddy <:iac_namaste:841266597318361098>**')
    await ctx.send('Try again in {:2f}s'.format(error.retry_after))

我在我的一个命令上添加了 20 分钟的冷却时间

@commands.cooldown(1 , 1200, type=commands.BucketType.user)

它工作正常,但我没有得到 min 或 hr 格式的输出

在此处输入图像描述

我希望它在 20 分钟内发送类似 try again 之类的信息,并显示还剩多少分钟

标签: pythondictionarydatetimediscorddiscord.py

解决方案


最好的方法是只使用新的不和谐时间戳格式

import time  # Add this to the top

# Then in the error handler
await ctx.send('Try again in <t:{}:R>'.format(int(time.time() + error.retry_after)))

推荐阅读