如何在discord.py中为Discord管理员bot发出正常运行时间命令

时间:2020-09-05 01:19:10

标签: discord discord.py

所以...为了给您一些背景信息,我需要告诉您我做了什么。我创建了一个不一致的bot进行审核(mellobot.net),并且需要有关命令的帮助。我想为实际的漫游器本身放置一个-uptime命令,以DD HH MM形式显示时间,但不知道命令行的外观。 (我想要诸如NightBots!uptime之类的东西,供抽搐用户用于抽搐流)是否有任何discord.py书呆子可以帮助我解决这种困境?

3 个答案:

答案 0 :(得分:0)

我不确定,但是也许您可以使用datetime模块。当漫游器为on_ready时,您可以输入日期,并执行如下的!uptime命令:

@client.event
async def on_ready():
    global startdate
    startdate = datetime.now()
@client.command()
async def uptime(ctx):
    now = datetime.now()
    uptime = startdate - now
    uptime = uptime.strftime('%d/%h/%M')# I don't remember the date's well, might be the lowercases are false.
    await ctx.send(f'Uptime: {uptime}')

答案 1 :(得分:0)

存储程序开始的时间,然后做一些数学运算。 on_ready()事件可以并且会在机器人的正常运行时间内多次触发,如果您仅在其中打印消息,则通常会发生不好的事情。

from datetime import datetime

bot = commands.Bot(command_prefix='>')
bot.launch_time = datetime.utcnow()

@bot.command()
async def uptime(ctx):
    delta_uptime = 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.send(f"{days}d, {hours}h, {minutes}m")

答案 2 :(得分:0)

这是discord.py重写的最新workign命令

@commands.command()
    async def uptime(self, ctx):
        delta_uptime = datetime.datetime.utcnow() - self.bot.launch_time
        hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
        minutes, seconds = divmod(remainder, 60)
        days, hours = divmod(hours, 24)
        e = discord.Embed(title=f"I've been up  for {days}d, {hours}h, {minutes}m, {seconds}s,", color=discord.Color.green())
        await ctx.send(embed=e)```