Python discord.py-运行时命令(帮助)
我想发出一个命令,这样当有人执行Python discord.py-运行时命令(帮助),python,discord.py,Python,Discord.py,我想发出一个命令,这样当有人执行#runtime时,它会显示bot已激活的时间。到目前为止,一切正常,但问题是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 g
#runtime
时,它会显示bot已激活的时间。到目前为止,一切正常,但问题是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))
我认为实现这一点的简单方法是在bot出现时捕获
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("...")
将
time.sleep(1)
替换为await asyncio.sleep(1)
您还必须将其放置在协同程序中,例如showsAn。更好的解决方案是捕获bot启动时的当前时间,然后从调用命令时的当前时间中减去该时间。区别在于机器人启动的时间。帮助需要消息arg,但我添加了async def任务(消息):num1=0 num2=0 num3=0 num4=0而非客户端。是否关闭:num1=num1+1如果num1=60:num1=0 num2=num2+1如果num2=60:num2=0 num3=0 num3=num3+1如果num3=24:num3=0 num4=num4+1打印(“为{}运行”如果message.content:wait bot.say(“{}:{}:{}:{}:{}:{}:{}}.格式(num4,num3,num2,num1))
删除asyncio.sleep(1)
因为注释中没有空格