有多少用户扮演特定角色

时间:2020-07-15 04:41:00

标签: discord.py

我正在为特定服务器创建一个漫游器,我想在命令中插入多少用户具有特定角色。

我不知道

这是我设置的部分,我想念变量:

embed.add_field(
        name=f'I membri totali del {name_server} sono suddivisi in:',
        value=f'{triumvirato}: **{count_triumvirato}**\n{co_triumvirato}: **{count_co_triumvirato}**\n{presidente}: **{count_presidente}**\n{senatore}: **{count_senatore}**\n{moderatore}: **{count_moderatore}**\n{membro}: **{count_membro}**\n{accademico}: **{count_accademico}**\n{onorario}: **{count_onorario}**\n{gamer}: **{count_gamer}**\n{clandestino}: **{count_clandestino}**\n\nI membri ufficiali sono **{official_member_count}** e se contassimo pure gli accademici il totale salirebbe a **{official_e_accademici_member_count}**\nIl resto è composto da **{non_official_member_count}**',
        inline=False
    )

这是我应该设置但不知道的所有变量:

#specific member info
    triumvirato = #specific role to mention in the embed
    count_triumvirato = #here must give the total number of users who have a specific role
    co_triumvirato = #specific role to mention in the embed
    count_co_triumvirato = #here must give the total number of users who have a specific role
    presidente = #specific role to mention in the embed
    count_presidente = #here must give the total number of users who have a specific role
    senatore = #specific role to mention in the embed
    count_senatore = #here must give the total number of users who have a specific role
    moderatore = #specific role to mention in the embed
    count_moderatore = #here must give the total number of users who have a specific role
    membro = #specific role to mention in the embed
    count_membro = #here must give the total number of users who have a specific role
    accademico = #specific role to mention in the embed
    count_accademico = #here must give the total number of users who have a specific role
    onorario = #specific role to mention in the embed
    count_onorario = #here must give the total number of users who have a specific role
    gamer = #specific role to mention in the embed
    count_gamer = #here must give the total number of users who have a specific role
    clandestino = #specific role to mention in the embed
    count_clandestino = #here must give the total number of users who have a specific role
    official_member_count = # sum in the top 6 (from triumvirato to membro)
    official_e_accademici_member_count = # sum between official_member_count and accademico
    non_official_member_count = # sum between onorario, gamers and clandestino

我希望有人能帮助我,让我如此满意。

2 个答案:

答案 0 :(得分:1)

@client.command()
async def role(ctx):
    i = 0
    guild = ctx.guild
    for roles in guild.roles:
        if roles.name == "YOUR ROLE NAME":
            for member in guild.members:
                for member_role in member.roles:
                    if member_role.name == roles.name:
                       i += 1

这是执行此操作的另一种方法。 “ i”将返回具有此角色的用户数。

答案 1 :(得分:0)

from discord.utils import get
#inside command
guild = ctx.guild
role = get(guild.roles, name='Name')
user_with_role = [m for m in guild.members if role in m.roles]

这应该返回一个名为Name的角色的成员列表,列表中的len返回用户数。 no = len(user_with_role)