为什么 bot.commands 在discord.py 中不起作用?

0 python discord.py

这是我的代码:

import discord 
import asyncio
from discord import Game
from discord.ext import commands

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

@bot.event
async def on_ready():
print(bot.user.id)
print("botstart")
game = discord.Game("abc")
await bot.change_presence(status=discord.Status.online, activity=game)

@bot.event
async def on_message(message):
if message.content.startswith("hi"):
    await message.channel.send("Hello")

    if message.content.startswith("ping"):
    await message.channel.send("Pong")

@bot.command
async def ping(ctx):
ctx.send("Pong!")

bot.run("(my token)", bot=True)
Run Code Online (Sandbox Code Playgroud)

由于某种原因,bot.event可以工作,但@bot.command(!ping)部分不行。我尝试修复它,但我无法修复,甚至观看了discord.py 指南。

我究竟做错了什么?

小智 5

由于您覆盖了 on_message 事件,因此机器人不再响应任何命令。要修复此问题,请添加await bot.process_commands(message)到 on_message 函数中,使其能够正常处理命令。

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