为什么 on_message 停止命令运行?
Why does on_message stop commands from working?
基本上,一切似乎 都可以正常工作并启动,但由于某种原因我无法调用任何命令。我已经轻松地环顾四周并查看了 examples/watching 个视频,但我终究无法弄清楚哪里出了问题。代码如下:
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 中的调试输出确实有效并响应,整个机器人运行时没有任何异常,但它不会调用命令。
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)
默认 on_message
包含对此协程的调用,但是当您用自己的 on_message
覆盖它时,您需要自己调用它。
确定问题源于您对 on_message
的定义,您实际上可以将 debug
作为命令执行,因为它似乎与您的机器人具有相同的前缀:
@bot.command()
async def debug(ctx):
await ctx.send("d")
基本上,一切似乎 都可以正常工作并启动,但由于某种原因我无法调用任何命令。我已经轻松地环顾四周并查看了 examples/watching 个视频,但我终究无法弄清楚哪里出了问题。代码如下:
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 中的调试输出确实有效并响应,整个机器人运行时没有任何异常,但它不会调用命令。
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
覆盖它时,您需要自己调用它。
确定问题源于您对 on_message
的定义,您实际上可以将 debug
作为命令执行,因为它似乎与您的机器人具有相同的前缀:
@bot.command()
async def debug(ctx):
await ctx.send("d")