我正在尝试创建一个Discord机器人,该机器人可以计算自有人说出“单词”以来经过的时间,但是我当前的方法返回上述错误。我是python和discord.py的新手,请多多包涵。
我的搜索出现了this,但是即使知道为什么会发生错误,我也不确定是否有其他解决方法。
import discord, os, re
import datetime
import asyncio
from discord.ext import commands
from datetime import datetime
token = ('')
bot = commands.Bot(command_prefix='!')
client = discord.client
start = 0
end = 0
elapsed = 0
elapsedstr = str
@bot.event
async def on_ready():
print("Bot is live")
@bot.event
async def on_message(message):
if 'word' in message.content:
global start
start = datetime.datetime.now()
await bot.process_commands(message)
@bot.command()
async def lastsaid(ctx):
print('running lastsaid')
global end
global elapsed
end = datetime.now()
elapsed = end - start
elapsedstr = datetime.now.strftime('%H:%M:%S', datetime.gmtime(elapsed))
await ctx.send(('It has been ' + elapsedstr + ' since someone said the word!'))
print('sent ls')
if __name__ == '__main__':
bot.run(token)
如何计算无此错误的时间?
答案 0 :(得分:0)
首先,确保start从datetime
对象开始(当前从0开始)。当前,如果在运行命令之前未输入包含word
的内容,则会收到错误消息。另外,end = datetime.now()
应该是end = datetime.datetime.now()
吗?