discord.py机器人前缀命令失效问题排查求助
Discord.py 前缀命令无响应问题解决
你的问题根源在于自定义了on_message事件,但没有触发discord.py的命令处理逻辑。当重写on_message时,默认的命令解析流程会被覆盖,必须手动调用await bot.process_commands(message)才能让前缀命令生效。
修改后的代码如下:
import discord from discord.ext import commands intents = discord.Intents(messages=True, guilds=True) intents.message_content = True intents.messages = True intents.guild_messages = True bot = commands.Bot(command_prefix="$", intents=intents) @bot.event async def on_ready(): print(f'Logged on as {bot.user}!') @bot.event async def on_message(message): print(f'Message from {message.author}: {message.content}') # 过滤机器人自身消息,避免循环 if message.author == bot.user : return # 手动触发命令处理逻辑 await bot.process_commands(message) @bot.command() async def ping(ctx): print("test") await ctx.channel.send("pong") bot.run('MY_BOT_TOKEN')
另外建议取消注释过滤机器人自身消息的代码,防止机器人响应自己发送的消息引发不必要的循环。
内容的提问来源于stack exchange,提问作者GreGre
相关产品推荐
相关产品推荐

