【问题标题】:Why does on_message stop commands from working?为什么 on_message 会停止命令工作?
【发布时间】:2018-12-16 15:44:11
【问题描述】:

基本上,一切似乎都可以正常工作并启动,但由于某种原因,我无法调用任何命令。我已经轻松地环顾了一个小时并查看示例/观看视频,但我一生都无法弄清楚出了什么问题。代码如下:

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

【问题讨论】:

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


    【解决方案1】:

    From the documentation:

    覆盖默认提供的on_message 禁止运行任何额外的命令。要解决此问题,请在 on_message 的末尾添加 bot.process_commands(message) 行。例如:

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

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

    【讨论】:

      【解决方案2】:

      确定问题源于您对on_message 的定义,您实际上可以将debug 作为命令实现,因为它似乎与您的机器人具有相同的前缀:

      @bot.command()
      async def debug(ctx):
          await ctx.send("d")
      

      【讨论】:

        猜你喜欢
        • 2021-01-17
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多