Python报错:__init__()缺少必填仅关键字参数'intents',如何解决?
解决discord.py中
__init__() missing 1 required keyword-only argument: 'intents'错误 这个错误是因为discord.py 2.0及以上版本强制要求创建commands.Bot实例时必须指定intents参数,这是Discord API的权限控制机制,用来限定机器人能接收的事件类型。
修复步骤:
在代码中配置Intents
导入discord.Intents,根据需求创建意图实例,再传给commands.Bot:import discord from discord.ext import commands # 启用默认意图,同时开启消息内容读取权限(如果需要处理消息命令) intents = discord.Intents.default() intents.message_content = True # 初始化Bot时传入intents参数 blox = commands.Bot(command_prefix='!', intents=intents)如果你不需要全部默认权限,可以自定义开启特定意图:
intents = discord.Intents.none() intents.guilds = True # 必备:处理服务器相关事件 intents.messages = True # 接收消息事件 intents.message_content = True # 读取消息内容(需开发者后台开启)在Discord开发者后台开启对应权限
登录Discord开发者门户,进入你的机器人应用,在「Bot」选项卡下找到「Privileged Gateway Intents」,开启Message Content Intent(如果你的机器人需要读取消息内容来响应命令)。
内容的提问来源于stack exchange,提问作者Kunal Jain
相关产品推荐
相关产品推荐

