为什么 on_message 会停止命令工作? [英] Why does on_message stop commands from working?
问题描述
基本上,一切似乎都能正常工作并启动,但由于某种原因我无法调用任何命令.我已经轻松地环顾了一个小时,查看示例/观看视频,但我终生无法弄清楚出了什么问题.代码如下:
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)
我在 on_message 中的调试输出实际上确实有效并做出了响应,并且整个机器人在没有任何异常的情况下运行,但它只是不会调用命令.
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.
推荐答案
覆盖默认提供的 on_message
禁止运行任何额外的命令.要解决此问题,请在 on_message
的末尾添加 bot.process_commands(message)
行.例如:
Overriding the default provided
on_message
forbids any extra commands from running. To fix this, add abot.process_commands(message)
line at the end of youron_message
. For example:
@bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
默认的on_message
包含对这个协程的调用,但是当你用你自己的on_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.
这篇关于为什么 on_message 会停止命令工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!