【问题标题】:Role doesn't get added autorole | discord.py角色未添加自动角色 |不和谐.py
【发布时间】:2021-05-31 04:40:49
【问题描述】:
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://imgur.com/SRRsJgT 我也试过这个: https://pastebin.com/eCp4HN7F

【问题讨论】:

    标签: python discord.py roles


    【解决方案1】:

    我可以看到你的代码中有两个问题,一个是从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 键是一个字符串。这也将支持多个自动角色。

    参考资料:

    【讨论】:

    • 谢谢!这就是我现在拥有的 pastebin.com/fF6PQWHv 但这仍然不起作用
    • 您是否遇到错误,您启用了哪些意图?
    • 我没有收到错误,并且我启用了服务器成员意图。
    • 让它工作,但现在它只在他们也启用欢迎消息时才有效,因为他们的机器人使用相同的事件装饰器。 pastebin.com/JCwkUxZ7
    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    相关资源
    最近更新 更多