You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py 命令冷却秒数转小时:分钟:秒格式实现求助

Discord.py 冷却时间格式转换实现

你可以直接通过简单的数学计算把秒数转换为小时:分钟:秒格式,修改后的完整代码如下:

@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        await ctx.send("User existiert nicht!")
    if isinstance(error, commands.CommandOnCooldown):
        # 先取整得到总剩余秒数
        total_seconds = round(error.retry_after)
        # 计算小时、分钟、秒
        hours = total_seconds // 3600
        left_seconds = total_seconds % 3600
        minutes = left_seconds // 60
        seconds = left_seconds % 60
        # 格式化输出,02d表示不足两位时前面补0
        cooldown_time = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
        msg = f'**Du bist in einem Cooldown** Versuch es in {cooldown_time} erneut.'
        # 如果需要中文提示可以替换为下面这句
        # msg = f'**你当前处于冷却状态** 请在{cooldown_time}后再尝试。'
        await ctx.send(msg)

如果你不需要数字补零,把格式符里的:02d改为:d即可,比如1小时5分3秒会显示为1:5:3而非01:05:03。

内容的提问来源于stack exchange,提问作者Navis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:54:02