不和.py使用datetime的bot返回此值(TypeError:不支持的操作数类型:'日期时间。日期时间'和'int')

2024-10-03 13:17:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个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)

如果没有这个错误,我如何计算经过的时间?在


Tags: importmessagedatetimeasyncdefbot错误start
1条回答
网友
1楼 · 发布于 2024-10-03 13:17:33

首先,确保start以datetime对象开始(当前它从0开始)。当前,如果在运行您的命令之前没有说出包含word的内容,那么您将得到错误。也应该end = datetime.now()end = datetime.datetime.now()?在

相关问题 更多 >