我如何获得具有特定权限的所有角色

How can i get all roles with a certain permission

我正在创建一个票务系统,我希望它能在多个服务器上工作,但我不知道如何让服务器中的所有角色都具有特定权限。或者可能是一个额外的命令,供人们添加可以看到频道的角色。但我不知道怎么办? 这是我的 atm:

@bot.command(name='ticket', aliases=['Ticket'])
async def ticket(ctx):
    ticketnum = random.randint(1000,9999)
    guild = ctx.message.guild
    channel = await guild.create_text_channel(name=f'Ticket-{ticketnum}')
    channel.set_permissions(guild.default_role, read_messages=False)
    await ctx.send(f'Created ticket: {channel}')

有一个commands.has_permissions()装饰器,或者commands.has_guild_permissions(),你可以在命令上使用:

@commands.has_permissions(administrator=True) # Just an example - use the permission you want
@bot.command(...)
async def ticket(ctx):
    # Rest of your code here

这将检查 用户 是否拥有可用的权限。


下面的代码是一个等效的例子(不是唯一的方法)。

以下是您如何获得具有特定权限的所有角色:

roles = [r for r in ctx.guild.roles if any(p[1] and p[0] == "administrator" for p in r.permissions)]

# p[1] is checking that the second element of the tuple is True
# As the permissions are returned in the format:
# ("permission_name", boolean)
# Where True is saying the role has the permission enabled
# and False is saying that the role doesn't have the permission

并且您可以使用此列表来检查作者是否具有以下任何角色:

@bot.command(...)
async def ticket(ctx):
    roles = [r for r in ctx.guild.roles if any(p[1] and p[0] == "administrator" for p in r.permissions)]
    if any(r in ctx.author.roles for r in roles):
        # Let the code run
    else:
        # Send an error message

编辑:

方法一:要为多个角色设置权限,您只需遍历您创建的角色列表中的每个角色即可:

@bot.command(...)
async def ticket(ctx):
    # Some code
    roles = [...]
    for role in roles:
        await channel.set_permissions(role, permission_name=True) # Or false

这可能会导致您的机器人受到速率限制,因此我建议 asyncio.sleep()在每次权限更新之间稍微调整一下。

方法二:或者当你create_text_channel()时,你可以为所有目标角色添加一堆覆盖,例子见here添加覆盖。

方法 3: 然而,我会推荐一个更简单的解决方案 - 拥有一个预先存在的目标频道,并具有您希望新频道拥有的权限。
这将使您能够简单地 clone() 说频道:

@bot.command()
async def ticket(ctx):
    # Some code
    target_channel = bot.get_channel(112233445566778899)
    new_channel = await target_channel.clone(name="...")

然后你可以 edit() new_channel 的位置,如果你愿意的话。


参考文献: