【问题标题】:discord.py on member join , give a role commanddiscord.py on member join ,给出角色命令
【发布时间】:2021-10-03 21:52:27
【问题描述】:

所以我做了一个命令,可以设置一个成员加入,给角色命令,它将它存储到一个 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.json吗?
  • 是的,当然:{"865806960997826571": [""]}

标签: python python-3.x discord discord.py


【解决方案1】:

所以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))

【讨论】:

猜你喜欢
  • 2021-07-06
  • 2021-02-07
  • 2020-12-21
  • 2021-12-19
  • 2021-04-09
  • 2020-09-26
  • 2021-07-17
  • 2022-01-10
  • 2021-07-29
相关资源
最近更新 更多