基本上,一切似乎都可以正常工作并启动,但由于某种原因我无法调用任何命令。我现在已经轻松地环顾四周并查看示例/观看视频,但我终生无法找出问题所在。代码如下:
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
覆盖它时,您需要自己调用它。
关于python - 为什么 on_message() 停止命令工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331096/