【问题标题】:Prefixed and non prefix commands are not working together on python discord bot前缀和非前缀命令在 python discord bot 上不能一起工作
【发布时间】:2023-04-04 20:40:02
【问题描述】:
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)

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

【问题讨论】:

  • hel中,sot.say应该是bot.say吗?
  • 啊,是的,我只是没有意识到,我在这里将常规名称更改为“bot”,使其看起来更简单,并且打错了。在我的原始代码中是正确的

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


【解决方案1】:

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

使用on_message时添加bot.process_commands

为什么 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)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 2021-06-05
      • 1970-01-01
      • 2020-04-07
      • 2023-01-25
      • 2021-09-20
      • 2013-12-30
      • 1970-01-01
      • 2011-02-13
      相关资源
      最近更新 更多