Discord机器人ReactRoles功能报错10011:未知角色问题求助
解决Discord ReactRoles的
10011 Unknown Role错误及相关问题 核心错误根源
你的代码存在两个关键问题导致该错误:
add_roles方法调用错误,错误地将成员自身作为参数传入;- 部分变量类型和成员获取逻辑存在冗余/错误,导致角色匹配失败。
分步修正方案
1. 修复add_roles调用
原代码中await this_member.add_roles(this_member, this_role)完全错误,Member.add_roles()方法仅需传入要添加的角色对象,无需传入成员自身。正确写法:
await this_member.add_roles(this_role)
2. 修正频道ID类型
bot.get_channel()要求传入整数类型的ID,你定义的ChID是字符串,直接使用数字更稳妥:
ChID = 1018712244843970610 Channel = bot.get_channel(ChID)
3. 优化成员对象获取
无需通过ctx绕弯子,直接使用回调参数里的user,并转换为服务器成员对象(避免私有用户无法操作的问题):
this_member = await reaction.message.guild.fetch_member(user.id)
4. 增加角色存在性检查(可选)
为避免角色ID错误导致的崩溃,可添加判断:
if this_role: await this_member.add_roles(this_role) else: await Channel.send(f"无法找到角色ID {角色ID},请核对ID正确性!")
修正后的完整代码
async def on_reaction_add(reaction, user): ChID = 1018712244843970610 Channel = bot.get_channel(ChID) # 忽略机器人自身的反应 if user.bot: return if reaction.message.channel.id != ChID: await Channel.send("这是ChID不匹配的测试....") return this_member = await reaction.message.guild.fetch_member(user.id) if reaction.emoji == "🏃": await Channel.purge(limit=1) await Channel.send(f"{this_member.display_name} - this is a test for running....🏃") this_role = discord.utils.get(this_member.guild.roles, id=1018802229341327370) await this_member.add_roles(this_role) elif reaction.emoji == "🔨": await Channel.purge(limit=1) await Channel.send(f"{this_member.display_name} - this is a test for hammer....🔨") this_role = discord.utils.get(this_member.guild.roles, id=1018802151679602798) await this_member.add_roles(this_role) elif reaction.emoji == "🦆": await Channel.purge(limit=1) await Channel.send(f"{this_member.display_name} - this is a test for duck....🦆") this_role = discord.utils.get(this_member.guild.roles, id=1018802188555927572) await this_member.add_roles(this_role) elif reaction.emoji == "❤️": await Channel.purge(limit=1) await Channel.send(f"{this_member.display_name} - this is a test for heart....❤️") this_role = discord.utils.get(this_member.guild.roles, id=1018802188555927572) await this_member.add_roles(this_role)
额外注意事项
- 确保机器人拥有管理角色权限,且机器人的角色位置高于要添加的角色,否则会触发权限错误;
- 若仍出现角色找不到的问题,可打印
this_member.guild.roles查看服务器内所有角色ID,核对代码中的ID是否完全一致。
内容的提问来源于stack exchange,提问作者newtboot
相关产品推荐
相关产品推荐

