Skip to content

tux.cogs.guild.rolecount

Classes:

Name Description
RoleCount

Classes

RoleCount(bot: Tux)

Bases: Cog

Methods:

Name Description
rolecount

Show the number of users in each role.

Source code in tux/cogs/guild/rolecount.py
Python
def __init__(self, bot: Tux):
    self.bot = bot
    self.roles_emoji_mapping = {
        "ds": distro_ids,
        "lg": lang_ids,
        "de": des_ids,
        "misc": misc_ids,
        "vanity": vanity_ids,
    }

Functions

rolecount(interaction: discord.Interaction, which: discord.app_commands.Choice[str]) -> None async

Show the number of users in each role.

Parameters:

Name Type Description Default
interaction Interaction

The interaction object.

required
which Choice[str]

The role type to list.

required
Source code in tux/cogs/guild/rolecount.py
Python
@app_commands.command(name="rolecount")
@app_commands.describe(which="Which option to list!")
@app_commands.choices(
    which=[
        app_commands.Choice(name="Distro", value="ds"),
        app_commands.Choice(name="Language", value="lg"),
        app_commands.Choice(name="DE/WM", value="de"),
        app_commands.Choice(name="Misc", value="misc"),
        app_commands.Choice(name="Vanity", value="vanity"),
    ],
)
async def rolecount(
    self,
    interaction: discord.Interaction,
    which: discord.app_commands.Choice[str],
) -> None:
    """
    Show the number of users in each role.

    Parameters
    ----------
    interaction : discord.Interaction
        The interaction object.
    which : discord.app_commands.Choice[str]
        The role type to list.
    """

    if interaction.guild:
        # Get the roles and emojis for the selected option
        roles_emojis: list[list[int | str]] = self.roles_emoji_mapping.get(which.value, [])
        # Process the roles and emojis for the selected option
        await self._process_roles(interaction, roles_emojis, which)
_process_roles(interaction: discord.Interaction, roles_emojis: list[list[int | str]], which: discord.app_commands.Choice[str]) -> None async

Process the roles and emojis for the selected option.

Parameters:

Name Type Description Default
interaction Interaction

The interaction object.

required
roles_emojis list[list[int | str]]

The list of roles and emojis.

required
which Choice[str]

The selected option.

required
Source code in tux/cogs/guild/rolecount.py
Python
async def _process_roles(
    self,
    interaction: discord.Interaction,
    roles_emojis: list[list[int | str]],
    which: discord.app_commands.Choice[str],
) -> None:
    """
    Process the roles and emojis for the selected option.

    Parameters
    ----------
    interaction : discord.Interaction
        The interaction object.
    roles_emojis : list[list[int | str]]
        The list of roles and emojis.
    which : discord.app_commands.Choice[str]
        The selected option.
    """

    role_data: list[tuple[discord.Role, list[int | str]]] = []

    for role_emoji in roles_emojis:
        role_id = int(role_emoji[0])

        if interaction.guild and (role := interaction.guild.get_role(role_id)):
            role_data.append((role, role_emoji))

    # Sort roles by the number of members in descending order
    sorted_roles = sorted(role_data, key=lambda x: len(x[0].members), reverse=True)

    pages: list[discord.Embed] = []

    embed = self._create_embed(interaction, which)

    role_count = 0

    for role, role_emoji in sorted_roles:
        role_count, embed = self._format_embed(
            embed,
            interaction,
            role,
            role_count,
            (str(role_emoji[0]), str(role_emoji[1])),
            which,
            pages,
        )

    if embed.fields:
        pages.append(embed)

    await self._send_response(interaction, pages)
_format_embed(embed: discord.Embed, interaction: discord.Interaction, role: discord.Role, role_count: int, role_emoji: tuple[str, str], which: discord.app_commands.Choice[str], pages: list[discord.Embed]) -> tuple[int, discord.Embed]

Format the embed with the role data.

Parameters:

Name Type Description Default
embed Embed

The embed to format.

required
interaction Interaction

The interaction object.

required
role Role

The role to format.

required
role_count int

The current role count.

required
role_emoji tuple[str, str]

The role emoji. The first element is the role ID and the second is the emoji name.

required
which Choice[str]

The selected option.

required
pages list[Embed]

The list of embeds to send.

required

Returns:

Type Description
tuple[int, Embed]

The updated role count and embed.

Source code in tux/cogs/guild/rolecount.py
Python
def _format_embed(
    self,
    embed: discord.Embed,
    interaction: discord.Interaction,
    role: discord.Role,
    role_count: int,
    role_emoji: tuple[str, str],
    which: discord.app_commands.Choice[str],
    pages: list[discord.Embed],
) -> tuple[int, discord.Embed]:
    """
    Format the embed with the role data.

    Parameters
    ----------
    embed : discord.Embed
        The embed to format.
    interaction : discord.Interaction
        The interaction object.
    role : discord.Role
        The role to format.
    role_count : int
        The current role count.
    role_emoji : tuple[str, str]
        The role emoji. The first element is the role ID and the second is the emoji name.
    which : discord.app_commands.Choice[str]
        The selected option.
    pages : list[discord.Embed]
        The list of embeds to send.

    Returns
    -------
    tuple[int, discord.Embed]
        The updated role count and embed.
    """

    if role_count >= 9:
        pages.append(embed)
        embed = self._create_embed(interaction, which)
        role_count = 0

    emoji = discord.utils.get(self.bot.emojis, name=role_emoji[1]) or f":{role_emoji[1]}:" or "❔"

    embed.add_field(
        name=f"{emoji!s} {role.name}",
        value=f"{len(role.members)} users",
        inline=True,
    )

    role_count += 1

    return role_count, embed
_create_embed(interaction: discord.Interaction, which: discord.app_commands.Choice[str]) -> discord.Embed

Create an embed for the role data.

Parameters:

Name Type Description Default
interaction Interaction

The interaction object.

required
which Choice[str]

The selected option.

required

Returns:

Type Description
Embed

The created embed.

Source code in tux/cogs/guild/rolecount.py
Python
def _create_embed(
    self,
    interaction: discord.Interaction,
    which: discord.app_commands.Choice[str],
) -> discord.Embed:
    """
    Create an embed for the role data.

    Parameters
    ----------
    interaction : discord.Interaction
        The interaction object.
    which : discord.app_commands.Choice[str]
        The selected option.

    Returns
    -------
    discord.Embed
        The created embed.
    """

    return EmbedCreator.create_embed(
        bot=self.bot,
        embed_type=EmbedCreator.INFO,
        user_name=interaction.user.name,
        user_display_avatar=interaction.user.display_avatar.url,
        title=f"{which.name} Roles",
        description="Number of users in each role",
    )
_send_response(interaction: discord.Interaction, pages: list[discord.Embed]) -> None async

Send the response to the interaction.

Parameters:

Name Type Description Default
interaction Interaction

The interaction object.

required
pages list[Embed]

The list of embeds to send.

required
Source code in tux/cogs/guild/rolecount.py
Python
async def _send_response(
    self,
    interaction: discord.Interaction,
    pages: list[discord.Embed],
) -> None:
    """
    Send the response to the interaction.

    Parameters
    ----------
    interaction : discord.Interaction
        The interaction object.
    pages : list[discord.Embed]
        The list of embeds to send.
    """

    if pages:
        menu = ViewMenu(interaction, menu_type=ViewMenu.TypeEmbed)

        for page in pages:
            menu.add_page(page)

        menu.add_button(ViewButton.go_to_first_page())
        menu.add_button(ViewButton.back())
        menu.add_button(ViewButton.next())
        menu.add_button(ViewButton.go_to_last_page())
        menu.add_button(ViewButton.end_session())

        await menu.start()