discord.py-运行时命令(帮助)

时间:2018-10-02 17:39:24

标签: python discord.py

我想发出一条命令,以便当某人执行#runtime时,它显示机器人已活动了多少时间。到目前为止,一切正常,但是问题是while True:部分阻止了命令的执行。有人可以帮助我吗?

代码:

import os
import discord
import asyncio
import chalk
import time
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get

print("DONE LOADING!\n")

bot = commands.Bot(command_prefix='#')

client = discord.Client()

num1 = 0

num2 = 0

num3 = 0

num4 = 0

while True:
    num1 = num1+1
    if num1 == 60:
        num1 = 0
        num2 = num2+1
    if num2 == 60:
        num2 = 0
        num3 = num3+1
    if num3 == 24:
        num3 = 0
        num4 = num4+1
    time.sleep(1)

@bot.command(pass_context=True)
async def runtime(ctx):
    await bot.say("Running for {}d {}h {}m {}s".format(num4, num3, num2, num1))

1 个答案:

答案 0 :(得分:0)

我认为,执行此操作的简单方法是在机器人启动时捕获datetime,然后使用它来计算所需的经过时间。

from datetime import datetime
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def runtime(ctx):
    now = datetime.utcnow()
    elapsed = now - starttime
    seconds = elapsed.seconds
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    await bot.say("Running for {}d {}h {}m {}s".format(elapsed.days, hours, minutes, seconds))

starttime = datetime.utcnow()
bot.run("...")