为什么on_message会阻止命令工作?

时间:2018-03-17 00:05:13

标签: 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)

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

2 个答案:

答案 0 :(得分:7)

From the rewrite documentation:

  

覆盖默认提供的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)

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

答案 1 :(得分:0)

确定问题源于您对 on_message 的定义,您实际上可以将 debug 实现为命令,因为它似乎与您的机器人具有相同的前缀:

@bot.command()
async def debug(ctx):
    await ctx.send("d")