python - 为什么 on_message() 停止命令工作?

标签 python python-3.x discord discord.py

基本上,一切似乎都可以正常工作并启动,但由于某种原因我无法调用任何命令。我现在已经轻松地环顾四周并查看示例/观看视频,但我终生无法找出问题所在。代码如下:

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 中的调试输出确实有效并做出响应,整个机器人运行时没有任何异常,但它只是不会调用命令。

最佳答案

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)

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

关于python - 为什么 on_message() 停止命令工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331096/

相关文章:

python - 关于 Flask session 的一些问题

python - 类型错误 : encoding or errors without a string argument

javascript - 如何实际使用使用discord oauth2请求的数据

linux - select() 在 python2 和 python3 上的行为是否不同?

java - JDA Stream mp4 文件不和谐

javascript - 如何使用 Javascript 从 JSON 文件中选择一个随机对象(?)?

python - 将评论插入 jupyter notebook

python - 在 Python 中识别相似项目以减少/澄清简历结果的数量

python - 设计中的嵌套字典可以吗?

python - 代码在 Python 3 中运行,而不是在 Python 2 中运行