【问题标题】:Discord Bot Commands not working with on_messageDiscord Bot 命令不适用于 on_message
【发布时间】:2021-09-22 15:18:13
【问题描述】:
我有一些命令可以完美运行,但是当我添加 on_message 时却没有。我读到您需要添加 await bot.process_commands(message) 行,但它仍然对我不起作用。为什么?
@bot.event
async def on_message(message):
if message.content.lower() == 'prefix':
prefix = guilds.find_one({"_id": message.guild.id})["prefix"]
await message.channel.send(f"> The prefix for this server is: {prefix}")
else:
return
await bot.process_commands(message)
【问题讨论】:
标签:
python
discord.py
command
【解决方案1】:
函数在遇到return 语句时立即结束,按照您当前的逻辑,它只会在if 语句为True 时处理命令。只需删除 else 部分即可。
@bot.event
async def on_message(message):
if message.content.lower() == 'prefix':
prefix = guilds.find_one({"_id": message.guild.id})["prefix"]
await message.channel.send(f"> The prefix for this server is: {prefix}")
await bot.process_commands(message)
【解决方案2】:
await bot.process_commands(message) 代码仅在消息内容为“前缀”时才可访问。要解决这个问题,请将 await bot.process_commands(message) 放在 else 正文中:
@bot.event
async def on_message(message):
if message.content.lower() == 'prefix':
prefix = guilds.find_one({"_id": message.guild.id})["prefix"]
await message.channel.send(f"> The prefix for this server is: {prefix}")
else:
await bot.process_commands(message)