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

时间:2020-06-15 01:22:51

标签: 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)

由于某些原因,bot.event有效,而@bot.command(!ping)部分无效。我试图修复它,但我什至没有看discord.py指南。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

由于您要覆盖on_message事件,因此漫游器不再响应任何命令。要解决此问题,请在您的on_message函数中添加await bot.process_commands(message)以便其正常处理命令。

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