python - 我们如何在 discord.py 上看到 uptime bot?
问题描述
例如,我想做一个显示两者正常运行时间的命令Online Time: 1h 10m 15s
解决方案
您将保存机器人启动的时间,然后将其与您调用命令的时间进行比较。
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")
推荐阅读
- java - 每次单击/点击时要更改的按钮文本
- flutter - Flutter Provider-Consumer 异常错误
- typescript - 如何动态加载具体实现并参数化对象创建?
- javascript - 在移动设备上更改 DIV 的 id
- node.js - Express Server 不会在 Mac 上解决 Promise
- python - 403 Forbidden while making ajax request to endpoint in Django
- python - 当我无权访问或没有可用信息时,有没有办法跳过 URL(Json 文件)
- python - 如何使用 lxml 解析以 iso-8859-1 编码的 xml 文件?
- c# - 我想用c# ITEXTSHARP 5.5.13.2生成一个两层的PDF文件,怎么生成呢?
- javascript - 链接到没有 id 属性的页面位置