我正在尝试创建一个Discord bot来计算自从有人说“word”之后经过的时间,但是我当前的方法返回上面的错误。我对python和不和.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)
如果没有这个错误,我如何计算经过的时间?在
首先,确保start以
datetime
对象开始(当前它从0开始)。当前,如果在运行您的命令之前没有说出包含word
的内容,那么您将得到错误。也应该end = datetime.now()
是end = datetime.datetime.now()
?在相关问题 更多 >
编程相关推荐