嘿,如果成员加入 on_member_join event
我的代码是这个
@client.event
async def on_member_join(member):
gu_id = int(member.guild.id)
a = gu.find_one({"_id": gu_id}) # I use mongodb and find the guild
role_name = str(a['role'])
role = member.guild.get_role(role_name)
print(role_name)
await member.add_roles(role)
但我总是收到这个错误
AttributeError: 'NoneType' 对象没有属性 'id'
我也用角色 id 尝试过这个,但后来我收到了这个错误
discord.errors.Forbidden:403 Forbidden(错误代码:50013):缺少权限
答案 0 :(得分:0)
可能是因为 gu_id
试图从 id
分配 member.guild
值,但它(无论 guild
是什么)实际上不具有该属性,或者它没有被在到达此代码段之前在其他逻辑中的某处传递。
也许您可以先检查 member.guild
在其他地方的使用情况,这是我要开始的地方。
答案 1 :(得分:0)
Guild.get_role
获取角色的 ID,您传递的是名称。所以如果你尝试传递 ID,如果你仍然想传递名称,这是有道理的:
role = discord.utils.get(member.guild.roles, name="some name")
注意区分大小写
另一个错误本质上意味着机器人没有添加角色所需的权限,要么它在层次结构中太低,要么没有必要的权限,要么你是服务器和机器人的所有者根本无法为您添加角色。