discord.py中机器人为何不响应其他机器人账号的命令?
解决discord.py机器人无法响应其他机器人命令的问题
discord.py的commands.Bot默认会忽略来自其他机器人账号的消息,这就是第二个机器人无法响应第一个机器人命令的原因——虽然on_message事件能接收机器人消息,但命令框架的内置过滤规则会跳过这些消息。
要关闭这个设置,只需要在初始化commands.Bot时添加ignore_bots=False参数即可:
修改后的代码如下:
import discord from discord.ext import commands prefix = '.' intent = discord.Intents.default() intent.message_content = True client = commands.Bot( command_prefix=prefix, intents=intent, activity=discord.Game(' .help'), ignore_bots=False # 关闭忽略机器人消息的默认设置 ) @client.command() async def test(ctx): print("ah")
添加这个参数后,第二个机器人就会处理所有机器人账号发送的命令消息了。如果需要更精准的控制(比如只允许特定机器人触发命令),可以额外在命令的检查函数或on_message事件中添加判断逻辑。
内容的提问来源于stack exchange,提问作者993 1
相关产品推荐
相关产品推荐

