Skip to content

tux.database.controllers.guild_config

Classes:

Name Description
GuildConfigController

Classes

GuildConfigController()

Initialize the controller with database tables.

Methods:

Name Description
ensure_guild_exists

Ensure the guild exists in the database.

insert_guild_config

Insert a new guild config into the database.

get_guild_config

Get a guild config from the database.

get_guild_prefix

Get a guild prefix from the database.

get_perm_level_role

Get the role id for a specific permission level.

get_perm_level_roles

Get the role ids for all permission levels from the lower_bound up to but not including 8.

Source code in tux/database/controllers/guild_config.py
Python
def __init__(self):
    """Initialize the controller with database tables."""
    self.table: GuildConfigActions[GuildConfig] = db.client.guildconfig
    self.guild_table: GuildActions[Guild] = db.client.guild

Functions

ensure_guild_exists(guild_id: int) -> Any async

Ensure the guild exists in the database.

Source code in tux/database/controllers/guild_config.py
Python
async def ensure_guild_exists(self, guild_id: int) -> Any:
    """Ensure the guild exists in the database."""
    guild: Any = await self.guild_table.find_first(where={"guild_id": guild_id})
    if guild is None:
        return await self.guild_table.create(data={"guild_id": guild_id})
    return guild
insert_guild_config(guild_id: int) -> Any async

Insert a new guild config into the database.

Source code in tux/database/controllers/guild_config.py
Python
async def insert_guild_config(self, guild_id: int) -> Any:
    """Insert a new guild config into the database."""
    await self.ensure_guild_exists(guild_id)
    return await self.table.create(data={"guild_id": guild_id})
get_guild_config(guild_id: int) -> Any async

Get a guild config from the database.

Source code in tux/database/controllers/guild_config.py
Python
async def get_guild_config(self, guild_id: int) -> Any:
    """Get a guild config from the database."""
    return await self.table.find_first(where={"guild_id": guild_id})
get_guild_prefix(guild_id: int) -> str | None async

Get a guild prefix from the database.

Source code in tux/database/controllers/guild_config.py
Python
async def get_guild_prefix(self, guild_id: int) -> str | None:
    """Get a guild prefix from the database."""
    config: Any = await self.table.find_first(where={"guild_id": guild_id})
    return None if config is None else config.prefix
get_perm_level_role(guild_id: int, level: str) -> int | None async

Get the role id for a specific permission level.

Source code in tux/database/controllers/guild_config.py
Python
async def get_perm_level_role(self, guild_id: int, level: str) -> int | None:
    """
    Get the role id for a specific permission level.
    """
    try:
        role_id = await self.get_guild_config_field_value(guild_id, level)  # type: ignore
        logger.debug(f"Retrieved role_id {role_id} for guild {guild_id} and level {level}")
    except Exception as e:
        logger.error(f"Error getting perm level role: {e}")
        return None
    return role_id
get_perm_level_roles(guild_id: int, lower_bound: int) -> list[int] | None async

Get the role ids for all permission levels from the lower_bound up to but not including 8.

Source code in tux/database/controllers/guild_config.py
Python
async def get_perm_level_roles(self, guild_id: int, lower_bound: int) -> list[int] | None:
    """
    Get the role ids for all permission levels from the lower_bound up to but not including 8.
    """
    perm_level_roles: dict[int, str] = {
        0: "perm_level_0_role_id",
        1: "perm_level_1_role_id",
        2: "perm_level_2_role_id",
        3: "perm_level_3_role_id",
        4: "perm_level_4_role_id",
        5: "perm_level_5_role_id",
        6: "perm_level_6_role_id",
        7: "perm_level_7_role_id",
    }

    try:
        role_ids: list[int] = []

        for level in range(lower_bound, 8):
            if role_field := perm_level_roles.get(level):
                role_id = await self.get_guild_config_field_value(guild_id, role_field)  # type: ignore

                if role_id:
                    role_ids.append(role_id)

        logger.debug(f"Retrieved role_ids {role_ids} for guild {guild_id} with lower bound {lower_bound}")

    except Exception as e:
        logger.error(f"Error getting perm level roles: {e}")
        return None

    return role_ids