Python 为什么我的on_消息事件打印日志两次?

Python 为什么我的on_消息事件打印日志两次?,python,discord.py,Python,Discord.py,这是我的on_message命令的日志位,每当我发送“ping”时,它都会检查以确保它在命令列表中,然后记录它,但它会记录两次并发送一次(它应该只发送一次) async def on_消息(消息): #检查邮件是否来自服务器 如果message.content!="": #测试是否为用户/Bot 如果message.author==bot.user: 返回 #使用的报告Comamnd 其他: splitCommand=message.content.split(“”) join=“” join=

这是我的on_message命令的日志位,每当我发送“ping”时,它都会检查以确保它在命令列表中,然后记录它,但它会记录两次并发送一次(它应该只发送一次)

async def on_消息(消息):
#检查邮件是否来自服务器
如果message.content!="":
#测试是否为用户/Bot
如果message.author==bot.user:
返回
#使用的报告Comamnd
其他:
splitCommand=message.content.split(“”)
join=“”
join=join.join(splitCommand)
time=“[UTC”+str(message.created_at)+””
singleCommand=“\n命令:\'”+splitCommand[0]+“\'”
multiCommand=“\n命令:\'”+加入+“\'”
user=“\n来自用户:\'\”(“+str(message.author)+”)\”
#单字命令
如果命令列表中的splitCommand[0].lower():
打印(时间+单一命令+用户)
#多字命令
如果有(x.isalpha()或x.isspace()表示联接中的x):
如果commandList中的join.lower():
打印(时间+多命令+用户)
elif regex.search(join)!=None和commandList中的join.lower():
打印(时间+多命令+用户)
#删除命令
如果message.content[0]==前缀:
userMessage=message
等待userMessage.delete()
打印(“已删除命令”)
其他:
通过
一个示例输出是:

[UTC 2020-11-24 08:21:42.587000]
Command: 'ping'
From User: '<@379355817700491264> | (24Kings#0001)'
[UTC 2020-11-24 08:21:42.587000]
Command: 'ping'
From User: '<@379355817700491264> | (24Kings#0001)'
Sent: 'pong'
[UTC 2020-11-24 08:21:42.587000]
命令:“ping”
来自用户:“|(24Kings#0001)”
[UTC 2020-11-24 08:21:42.587000]
命令:“ping”
来自用户:“|(24Kings#0001)”
发送:“pong”
但它应该只记录一次?
如果您有任何关于它为何记录两次和修复的想法,我们将不胜感激!:D

首先,最好使用
discord.ext.commands
,然后有三个选项

  • 检查错误并告诉用户(权限、错误参数等)

@bot.event
命令完成时的异步定义(ctx):
打印(f'{ctx.command}已被{ctx.author.display_name}'使用)

这里的命令是一个

我非常熟悉,没有意识到我没有检查splitCommand的长度,所以它记录了多个命令和单个命令非常感谢!我将把它添加到我的日志中:)我在稍后的代码中使用了
on_command\u error()
,但我不知道关于
on_command()
on_command\u completion()
,谢谢!很高兴听到它起作用了。请把它标为答案
[UTC 2020-11-24 08:21:42.587000]
Command: 'ping'
From User: '<@379355817700491264> | (24Kings#0001)'
[UTC 2020-11-24 08:21:42.587000]
Command: 'ping'
From User: '<@379355817700491264> | (24Kings#0001)'
Sent: 'pong'