前缀和非前缀命令在python discord bot上不能一起使用

问题描述 投票:2回答:2
import asyncio
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import chalk


bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    await bot.change_presence(game=discord.Game(name='Test'))
    print("All systems online and working " + bot.user.name)
    await bot.send_message(discord.Object(id=386518608550952965), "All systems online and working")

@bot.command(pass_context=True)
async def hel(ctx):
    await bot.say("A help message is sent to user")


@bot.command
async def on_message(message):
    if message.content.startswith("ping"):
        await bot.send_message(message.channel, "Pong")




bot.run("TOKEN", bot=True)

我试图在我的不和谐测试服务器上得到这个工作但是当我像这样使用它时,只有第一个“on_ready”和!hel命令工作,ping不打印任何东西,但当我删除!hel命令代码部分,ping有效,有什么方法可以让它们一起工作吗?

python python-3.x discord discord.py
2个回答
2
投票

使用@bot.command时将@bot.event更改为on_message

使用bot.process_commands时添加on_message

为什么on_message使我的命令停止工作?

覆盖默认提供的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)

http://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

您的代码应如下所示:

import asyncio
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import chalk


bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    await bot.change_presence(game=discord.Game(name='Test'))
    print("All systems online and working " + bot.user.name)
    await bot.send_message(discord.Object(id=386518608550952965), "All systems online and working")

@bot.command(pass_context=True)
async def hel(ctx):
    await bot.say("A help message is sent to user")


@bot.event
async def on_message(message):
    if message.content.startswith("ping"):
        await bot.send_message(message.channel, "Pong")

    await bot.process_commands(message)


bot.run("TOKEN", bot=True)

0
投票

尝试用@bot.command替换on_message上方的@bot.event

© www.soinside.com 2019 - 2024. All rights reserved.