首页 > 解决方案 > 我们如何在 discord.py 上看到 uptime bot?

问题描述

例如,我想做一个显示两者正常运行时间的命令Online Time: 1h 10m 15s

标签: pythondiscord.py

解决方案


您将保存机器人启动的时间,然后将其与您调用命令的时间进行比较。

import datetime as dt

# after you initialize bot itself
bot.launch_time = dt.datetime.utcnow()

@bot.command()
async def uptime(ctx):
    delta_uptime = dt.datetime.utcnow() - bot.launch_time
    hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
    minutes, seconds = divmod(remainder, 60)
    days, hours = divmod(hours, 24)
    await ctx.reply(f"Online Time: {days}d, {hours}h, {minutes}m, {seconds}s")

推荐阅读