当我添加 'on_message'-client 事件时,我的 Discord Bot 不会响应任何命令
My Discord Bot will not respond to any commands when I add an 'on_message'-client event
正如标题所示,我在使用 Discord Bot 时遇到了问题。我有 2 个客户端事件和更多命令,为了概述,这些命令未包含在下面的代码中。当我注释掉 'on_message'-事件时,所有命令都正常运行,但是一旦我取消注释,'on_message' 和命令都不起作用,并且控制台中也没有错误消息,所以我不知道我在这里做错了什么。
client = commands.Bot(command_prefix='!', help_command=None)
@client.event
async def on_ready():
channel = client.get_channel(821133655081484318)
print(f'Welcome, {client.user.name} is up and running!')
@client.event
async def on_message(message):
with open("author.json", "r") as f:
author_dic = json.load(f)
if message.author.bot or message.content.startswith("!"):
return None
try:
for list in author_dic.values():
for el in list:
if str(message.author) == el:
channelobj = client.get_channel(list[1])
if message.channel != channelobj:
await channelobj.send(message.content)
except IndexError:
return None
@client.command()
async def getid(ctx, given_name=None):
...
client.run("TOKEN")
我真的很高兴有人帮助我,因为我有点迷路了,已经提前谢谢了。
您必须添加 await client.process_commands(message)
(check this issue in documentation)。
@client.event
async def on_message(message):
# rest of your on_message code
await client.process_commands(message) # add this line at the end of your on_message event
正如标题所示,我在使用 Discord Bot 时遇到了问题。我有 2 个客户端事件和更多命令,为了概述,这些命令未包含在下面的代码中。当我注释掉 'on_message'-事件时,所有命令都正常运行,但是一旦我取消注释,'on_message' 和命令都不起作用,并且控制台中也没有错误消息,所以我不知道我在这里做错了什么。
client = commands.Bot(command_prefix='!', help_command=None)
@client.event
async def on_ready():
channel = client.get_channel(821133655081484318)
print(f'Welcome, {client.user.name} is up and running!')
@client.event
async def on_message(message):
with open("author.json", "r") as f:
author_dic = json.load(f)
if message.author.bot or message.content.startswith("!"):
return None
try:
for list in author_dic.values():
for el in list:
if str(message.author) == el:
channelobj = client.get_channel(list[1])
if message.channel != channelobj:
await channelobj.send(message.content)
except IndexError:
return None
@client.command()
async def getid(ctx, given_name=None):
...
client.run("TOKEN")
我真的很高兴有人帮助我,因为我有点迷路了,已经提前谢谢了。
您必须添加 await client.process_commands(message)
(check this issue in documentation)。
@client.event
async def on_message(message):
# rest of your on_message code
await client.process_commands(message) # add this line at the end of your on_message event