discord.py在成员连接上,给出一个role命令



所以我做了一个可以设置成员连接的命令,给出角色命令,它将其存储到json文件中。但是当有人加入时,它会出现错误:return self._roles.get(role_id) TypeError: unhashable type: 'list'。下面是我的代码:

with open('join_roles.json', 'r') as f:
join_roles = json.load(f)
@client.event
async def on_member_join(member):
guild = member.guild
print(join_roles.get(str(guild.id)))
role = member.guild.get_role(join_roles.get(str(guild.id)))
await member.add_roles(role)
@client.command()
@commands.has_permissions(administrator=True)
async def joinrole(ctx, role):
join_roles[str(ctx.guild.id)] = role.split()
with open('join_roles.json', 'w') as f:
json.dump(join_roles, f)
await ctx.send(embed=discord.Embed(title=f':white_check_mark:| Member join role set to {role}!', color=0x2596be))

所以join_roles.get(str(guild.id))返回list,而这对guild.get_role()不起作用。这可以指向您的json数据显示如下:

{
"GuildID": ["RoleID"]
}

由于join_roles[str(ctx.guild.id)] = role.split()将列表放入json数据。一个更好的解决方案是使用一个返回Role的RoleConverter,这将允许通过:ID,角色提及和角色名称来扮演角色。

with open('join_roles.json', 'r') as f:
join_roles = json.load(f)
@client.event
async def on_member_join(member):
guild = member.guild
print(join_roles.get(str(guild.id)))
role = member.guild.get_role(join_roles.get(str(guild.id)))
await member.add_roles(role)
@client.command()
@commands.has_permissions(administrator=True)
async def joinrole(ctx, role):
role = await commands.RoleConverter().convert(ctx, role)
join_roles[str(ctx.guild.id)] = role.id
with open('join_roles.json', 'w') as f:
json.dump(join_roles, f)
await ctx.send(embed=discord.Embed(title=f':white_check_mark:| Member join role set to {role.name}!', color=0x2596be))

最新更新