简体   繁体   English

discord.py on member join ,给一个角色命令

[英]discord.py on member join , give a role command

So I made a command that can set a on member join, give role command where it stores it into a json file.所以我做了一个命令,可以设置一个成员加入,给角色命令,它将它存储到一个 json 文件中。 But when someone joins it comes up with the error: return self._roles.get(role_id) TypeError: unhashable type: 'list'.但是当有人加入时会出现错误:返回 self._roles.get(role_id) TypeError: unhashable type: 'list'。 Here is my code:这是我的代码:

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

So join_roles.get(str(guild.id)) returns a list and that won't work on a guild.get_role() .所以join_roles.get(str(guild.id))返回一个list ,它不适用于guild.get_role() This could point to your json data being displayed like so:这可能指向您的 json 数据显示如下:

{
    "GuildID": ["RoleID"]
}

Due to join_roles[str(ctx.guild.id)] = role.split() which puts a list into the json data.由于join_roles[str(ctx.guild.id)] = role.split()将列表放入 json 数据中。 A much better solution is to use a RoleConverter which returns Role this would allow roles by: ID, Role mention and by Role name.更好的解决方案是使用RoleConverter返回Role这将允许角色通过: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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM