我们如何在discord.py上看到正常运行时间机器人?

2024-03-29 05:46:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 命令time时间online
1条回答
网友
1楼 · 发布于 2024-03-29 05:46:46

您可以在启动bot时节省时间,然后将其与调用命令的时间进行比较

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")

相关问题 更多 >