【问题标题】:discord.py check if a user has a particular rolediscord.py 检查用户是否具有特定角色
【发布时间】:2019-08-27 08:34:53
【问题描述】:

我正在尝试使用它来检查用户是否具有被标记的角色。

discord.utils.get(ctx.guild.roles, name=rolename) 和 discord.utils.get(user.roles, name=rolename)

@bot.command()
@commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member, rolename: discord.Role):
    role = discord.utils.get(ctx.guild.roles, name=rolename)
    if(not role in user.roles):
        await user.add_roles(rolename)
        embed=discord.Embed(title=f"{user.name} Has been added to a role called: {rolename.name}", color=discord.Color.dark_purple())
        await ctx.send(embed=embed)
    else:
        await ctx.send(f"Hey {ctx.author.name}, {user.name} already has the role called: {rolename.name}")

没有错误只是不断给出角色而不是检查用户是否具有角色。

【问题讨论】:

  • 我已经编辑了您的问题,使其具有更具描述性的标题。为了在未来获得最佳结果,“不确定如何执行此操作”不会告诉潜在用户有关问题的任何信息

标签: python discord discord.py-rewrite


【解决方案1】:
@bot.command()
@commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
    if rolename in user.roles:
        await ctx.send("Person already has role")
    else:
        await user.add_roles(rolename)
        await ctx.send("Person doesn't have the role and it has been given to him/her")

确保机器人的角色高于您尝试赋予用户的角色,因为如果不是,那么您将收到错误50013 Missing Permissions.

【讨论】:

    【解决方案2】:

    这是解决办法

    @commands.has_permissions(manage_roles=True)
    async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
        if rolename not in user.roles:
            await user.add_roles(rolename)
            await ctx.send("Person doesn't have the role and it has been given to him/her")
        else:
            await ctx.send("Person already has role")```
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2019-04-04
      • 2010-12-22
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多