Why does on_message stop commands from working?

混江龙づ霸主 提交于 2019-11-25 23:45:41

问题


Basically, everything appears to work fine and start up, but for some reason I can\'t call any of the commands. I\'ve been looking around for easily an hour now and looking at examples/watching videos and I can\'t for the life of me figure out what is wrong. Code below:

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)

The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won\'t call the commands.


回答1:


From the documentation:

Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:

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

    await bot.process_commands(message)

The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.



来源:https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!