为什么on_message会停止命令的工作?

Ace*_*ean 4 python python-3.x discord discord.py discord.py-rewrite

基本上,一切似乎都可以正常运行并启动,但是由于某些原因,我无法调用任何命令。我已经很轻松地环顾了一个小时,然后浏览示例/观看视频,但我终生无法找出问题所在。代码如下:

import discord
import asyncio
from discord.ext import commands

bot = commands.Bot(command_prefix = '-')
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.event
async def on_message(message):
    if message.content.startswith('-debug'):
        await message.channel.send('d')

@bot.command(pass_context=True)
async def ping(ctx):
    await ctx.channel.send('Pong!')

@bot.command(pass_context=True)
async def add(ctx, *, arg):
    await ctx.send(arg)
Run Code Online (Sandbox Code Playgroud)

我在on_message中拥有的调试输出实际上可以正常工作并做出响应,并且整个bot都可以运行,没有任何异常,但是它不会调用命令。

Pat*_*ugh 10

从文档中:

覆盖提供的默认值将on_message禁止运行任何其他命令。要解决此问题,请bot.process_commands(message)在的末尾添加一行on_message。例如:

@bot.event
async def on_message(message):
    # do some extra stuff here

    await bot.process_commands(message)
Run Code Online (Sandbox Code Playgroud)

默认值on_message包含对此协程的调用,但是当您用自己的协程覆盖它时on_message,您需要自己调用它。