Skip to content

discord_lfg.utils.roles

Create a standardised role.

logger = logging.getLogger(__name__) module-attribute #

RoleDefinition(name, count, emoji, identifier) dataclass #

Defines a role fed in by the config.

count instance-attribute #

emoji instance-attribute #

identifier instance-attribute #

name instance-attribute #

create_roles_from_config(roles, role_counts) #

Creates roles from a config input.

PARAMETER DESCRIPTION
roles

A dictionary which has the following structure: { name: { emoji: emoji string, identifier: single-character indicator, } }

TYPE: dict[str, dict[str, str]]

role_counts

a lookup of role name to the count of the number of role spots for a command.

TYPE: dict[str, int]

RETURNS DESCRIPTION
dict[str, RoleDefinition]

Dictionary of role name to definition

Source code in src\discord_lfg\utils\roles.py
def create_roles_from_config(
    roles: dict[str, dict[str, str]], role_counts: dict[str, int]
) -> dict[str, RoleDefinition]:
    """Creates roles from a config input.

    Args:
        roles: A dictionary which has the following structure:
            {
                name: {
                    emoji: emoji string,
                    identifier: single-character indicator,
                }
            }
        role_counts: a lookup of role name to the count of the number of role spots for a command.

    Returns:
        Dictionary of role name to definition
    """
    return {
        role_name: RoleDefinition(
            role_name,
            int(role_counts[role_name]),
            str(role.get("emoji")),
            str(role.get("identifier")),
        )
        for role_name, role in roles.items()
        if role_name in role_counts
    }

get_guild_role_mention_for_group_role(group_role, guild_roles, channel_name, channel_role_mentions) #

Generates an expected role and retrieves this if it matches a real one.

Source code in src\discord_lfg\utils\roles.py
def get_guild_role_mention_for_group_role(
    group_role: str,
    guild_roles: list[discord.Role],
    channel_name: str,
    channel_role_mentions: dict[str, str],
) -> str:
    """Generates an expected role and retrieves this if it matches a real one."""
    logger.debug(f"getting role mentions: {group_role}, {channel_name}, {channel_role_mentions}")
    if channel_name not in channel_role_mentions:
        logger.debug("did not find matching channel_name in channel_role_mentions")
        return ""
    mention_expected = f"{group_role}{channel_role_mentions[channel_name]}".lower()
    logger.debug(f"expected: {mention_expected}")
    logger.debug(f"guild_roles mentions: {[role.name for role in guild_roles]}")
    guild_role_tags = {role.name.lower(): role.mention for role in guild_roles}
    if mention_expected in guild_role_tags:
        logger.debug(f"found matching tag: {mention_expected}")
        return guild_role_tags[mention_expected]
    return ""