角色未添加自动角色 |discord.py


autoroles = {}
@client.event
async def on_ready():
global autoroles
with open("autorole.json", "r") as f:
autoroles = json.load(f)
@client.event
async def on_member_join(member):
#my welcome command is also here
await member.add_roles(autoroles[member.guild])
@client.command()
@commands.has_permissions(administrator=True)
async def autorole(ctx, *, role : discord.Role=None):

embed=discord.Embed(color=0x7289da, description=f"**Set autorole to** {role.mention}**?**")
embed.set_footer(text="React with the wave reaction to confirm it!")
msg = await ctx.send(embed=embed)
def checkifnotbot(reaction, user):
return user != client.user
await msg.add_reaction('👋')
reaction, user = await client.wait_for("reaction_add", timeout=60.0, check=checkifnotbot)
if str(reaction.emoji) == "👋":   
embedw=discord.Embed(description=f"👋**Autorole set!**n> Everytime someone joins the server they get the {role.mention} role!", color=0x7289da)     
await msg.edit(embed=embedw)
await msg.clear_reactions()

global autoroles
autoroles[str(ctx.guild.id)] = role.id
with open("autorole.json", "w") as f:
json.dump(autoroles, f)

我想要发生什么:如果有人使用autorole命令,一旦有人加入服务器,角色就会被添加(这适用于多个不和谐服务器)问题:我没有得到任何错误,但是角色没有被添加。我认为这是因为。add_roles不需要id,而是一个角色对象。但我不知道该怎么做注意:设置工作,它被添加到json文件https://i.stack.imgur.com/Uzbv6.jpg我也试过这个:https://pastebin.com/eCp4HN7F

我可以在你的代码中看到两个问题,一个是从json中获得角色的id,另一个是添加角色。要添加角色,您需要指定discord.Role以使其正常工作。

@client.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, 
id=autoroles[str(member.guild.id)])
await member.add_roles(role)

看到我使用str(member.guild.id)作为json的键是一个字符串。这也将支持多个自动角色。

引用:

  • utils.get
  • add_roles