Discord Bot 命令不适用于 on_message

Discord Bot Commands not working with on_message

我有几个命令运行得很好,但是当我添加 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)

await bot.process_commands(message)代码只有在消息内容为'prefix'时才能到达。要解决此问题,请将 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)

函数在遇到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)