Skip to content

tux.database.controllers.afk

Classes:

Name Description
AfkController

Controller for managing AFK status records.

Classes

AfkController()

Bases: BaseController[AFKModel]

Controller for managing AFK status records.

This controller provides methods for tracking, checking, and managing AFK (Away From Keyboard) status of guild members.

Initialize the AfkController with the afkmodel table.

Methods:

Name Description
get_afk_member

Get the AFK record for a member in a guild.

is_afk

Check if a member is AFK in a guild.

is_perm_afk

Check if a member is permanently AFK in a guild.

find_one

Return the first row that matches where or None.

set_afk

Insert or update an AFK record for a member.

find_many

Return a list of rows matching where (or all rows).

remove_afk

Remove an AFK record for a member.

count_afk_members

Count the number of AFK members in a guild.

get_all_afk_members

Get all AFK members in a guild.

execute_transaction

Execute callback inside a database session / transaction block.

safe_get_attr

Return getattr(obj, attr, default) - keeps old helper available.

connect_or_create_relation

Return a dict with a single key that can be merged into data dicts.

Source code in tux/database/controllers/afk.py
Python
def __init__(self) -> None:
    """Initialize the AfkController with the afkmodel table."""
    super().__init__(AFKModel)

Functions

get_afk_member(member_id: int, *, guild_id: int) -> AFKModel | None async

Get the AFK record for a member in a guild.

Parameters:

Name Type Description Default
member_id int

The ID of the member to check

required
guild_id int

The ID of the guild to check in

required

Returns:

Type Description
AFKModel | None

The AFK record if found, None otherwise

Source code in tux/database/controllers/afk.py
Python
async def get_afk_member(self, member_id: int, *, guild_id: int) -> AFKModel | None:
    """Get the AFK record for a member in a guild.

    Parameters
    ----------
    member_id : int
        The ID of the member to check
    guild_id : int
        The ID of the guild to check in

    Returns
    -------
    AFKModel | None
        The AFK record if found, None otherwise
    """
    return await self.find_one(where={"member_id": member_id, "guild_id": guild_id})
is_afk(member_id: int, *, guild_id: int) -> bool async

Check if a member is AFK in a guild.

Parameters:

Name Type Description Default
member_id int

The ID of the member to check

required
guild_id int

The ID of the guild to check in

required

Returns:

Type Description
bool

True if the member is AFK, False otherwise

Source code in tux/database/controllers/afk.py
Python
async def is_afk(self, member_id: int, *, guild_id: int) -> bool:
    """Check if a member is AFK in a guild.

    Parameters
    ----------
    member_id : int
        The ID of the member to check
    guild_id : int
        The ID of the guild to check in

    Returns
    -------
    bool
        True if the member is AFK, False otherwise
    """
    entry = await self.get_afk_member(member_id, guild_id=guild_id)
    return entry is not None
_execute_query(op: Callable[[AsyncSession], Any], span_desc: str) -> Any async

Run op inside a managed session & sentry span (if enabled).

Source code in tux/database/controllers/afk.py
Python
        The ID of the member to check
    guild_id : int
        The ID of the guild to check in

    Returns
    -------
    bool
        True if the member is AFK, False otherwise
    """
    entry = await self.get_afk_member(member_id, guild_id=guild_id)
    return entry is not None

async def is_perm_afk(self, member_id: int, *, guild_id: int) -> bool:
    """Check if a member is permanently AFK in a guild.

    Parameters
    ----------
    member_id : int
        The ID of the member to check
is_perm_afk(member_id: int, *, guild_id: int) -> bool async

Check if a member is permanently AFK in a guild.

Parameters:

Name Type Description Default
member_id int

The ID of the member to check

required
guild_id int

The ID of the guild to check in

required

Returns:

Type Description
bool

True if the member is permanently AFK, False otherwise

Source code in tux/database/controllers/afk.py
Python
async def is_perm_afk(self, member_id: int, *, guild_id: int) -> bool:
    """Check if a member is permanently AFK in a guild.

    Parameters
    ----------
    member_id : int
        The ID of the member to check
    guild_id : int
        The ID of the guild to check in

    Returns
    -------
    bool
        True if the member is permanently AFK, False otherwise
    """
    is_user_perm_afk = await self.find_one(
        where={"member_id": member_id, "guild_id": guild_id, "perm_afk": True},
    )
    return is_user_perm_afk is not None
find_one(*, where: dict[str, Any], include: dict[str, bool] | None = None, **__: Any) -> ModelT | None async

Return the first row that matches where or None.

Source code in tux/database/controllers/afk.py
Python
    bool
        True if the member is permanently AFK, False otherwise
    """
    is_user_perm_afk = await self.find_one(
        where={"member_id": member_id, "guild_id": guild_id, "perm_afk": True},
    )
    return is_user_perm_afk is not None

async def set_afk(
    self,
    member_id: int,
    nickname: str,
    reason: str,
    guild_id: int,
    perm_afk: bool = False,
set_afk(member_id: int, nickname: str, reason: str, guild_id: int, perm_afk: bool = False, until: datetime | None = None, enforced: bool = False) -> AFKModel async

Insert or update an AFK record for a member.

Parameters:

Name Type Description Default
member_id int

The ID of the member to set as AFK

required
nickname str

The nickname of the member

required
reason str

The reason for being AFK

required
guild_id int

The ID of the guild

required
perm_afk bool

Whether the AFK status is permanent

False

Returns:

Type Description
AFKModel

The created or updated AFK record

Source code in tux/database/controllers/afk.py
Python
async def set_afk(
    self,
    member_id: int,
    nickname: str,
    reason: str,
    guild_id: int,
    perm_afk: bool = False,
    until: datetime | None = None,
    enforced: bool = False,
) -> AFKModel:
    """Insert or update an AFK record for a member.

    Parameters
    ----------
    member_id : int
        The ID of the member to set as AFK
    nickname : str
        The nickname of the member
    reason : str
        The reason for being AFK
    guild_id : int
        The ID of the guild
    perm_afk : bool
        Whether the AFK status is permanent

    Returns
    -------
    AFKModel
        The created or updated AFK record
    """
    create_data = {
        "member_id": member_id,
        "nickname": nickname,
        "reason": reason,
        "perm_afk": perm_afk,
        "guild": self.connect_or_create_relation("guild_id", guild_id),
        "until": until,
        "enforced": enforced,
        "since": datetime.now(UTC),
    }
    update_data = {
        "nickname": nickname,
        "reason": reason,
        "perm_afk": perm_afk,
        "until": until,
        "enforced": enforced,
        "since": datetime.now(UTC),
    }

    return await self.upsert(
        where={"member_id": member_id},
        create=create_data,
        update=update_data,
        include={"guild": True},
    )
find_many(*, where: dict[str, Any] | None = None, include: dict[str, bool] | None = None, order: dict[str, str] | None = None, take: int | None = None, skip: int | None = None) -> list[ModelT] async

Return a list of rows matching where (or all rows).

Source code in tux/database/controllers/afk.py
Python
guild_id : int
    The ID of the guild
perm_afk : bool
    Whether the AFK status is permanent

Returns
-------
AFKModel
    The created or updated AFK record
"""
create_data = {
    "member_id": member_id,
    "nickname": nickname,
    "reason": reason,
    "perm_afk": perm_afk,
    "guild": self.connect_or_create_relation("guild_id", guild_id),
    "until": until,
    "enforced": enforced,
    "since": datetime.now(UTC),
}
update_data = {
    "nickname": nickname,
    "reason": reason,
    "perm_afk": perm_afk,
    "until": until,
    "enforced": enforced,
    "since": datetime.now(UTC),
}
remove_afk(member_id: int) -> AFKModel | None async

Remove an AFK record for a member.

Parameters:

Name Type Description Default
member_id int

The ID of the member to remove AFK status from

required

Returns:

Type Description
AFKModel | None

The deleted AFK record if found, None otherwise

Source code in tux/database/controllers/afk.py
Python
async def remove_afk(self, member_id: int) -> AFKModel | None:
    """Remove an AFK record for a member.

    Parameters
    ----------
    member_id : int
        The ID of the member to remove AFK status from

    Returns
    -------
    AFKModel | None
        The deleted AFK record if found, None otherwise
    """
    return await self.delete(where={"member_id": member_id})
count_afk_members(guild_id: int) -> int async

Count the number of AFK members in a guild.

Parameters:

Name Type Description Default
guild_id int

The ID of the guild to count AFK members for

required

Returns:

Type Description
int

The number of AFK members in the guild

Source code in tux/database/controllers/afk.py
Python
async def count_afk_members(self, guild_id: int) -> int:
    """Count the number of AFK members in a guild.

    Parameters
    ----------
    guild_id : int
        The ID of the guild to count AFK members for

    Returns
    -------
    int
        The number of AFK members in the guild
    """
    return await self.count(where={"guild_id": guild_id})
get_all_afk_members(guild_id: int) -> list[AFKModel] async

Get all AFK members in a guild.

Parameters:

Name Type Description Default
guild_id int

The ID of the guild to get AFK members for

required

Returns:

Type Description
list[AFKModel]

List of AFK members in the guild

Source code in tux/database/controllers/afk.py
Python
async def get_all_afk_members(self, guild_id: int) -> list[AFKModel]:
    """Get all AFK members in a guild.

    Parameters
    ----------
    guild_id : int
        The ID of the guild to get AFK members for

    Returns
    -------
    list[AFKModel]
        List of AFK members in the guild
    """
    return await self.find_many(where={"guild_id": guild_id})
execute_transaction(callback: Callable[[], Any]) -> Any async

Execute callback inside a database session / transaction block.

safe_get_attr(obj: Any, attr: str, default: Any = None) -> Any staticmethod

Return getattr(obj, attr, default) - keeps old helper available.

connect_or_create_relation(id_field: str, model_id: Any, *_: Any, **__: Any) -> dict[str, Any] staticmethod

Return a dict with a single key that can be merged into data dicts.

The calling code does something like::

Text Only
data = {"guild": connect_or_create_relation("guild_id", guild_id)}

We map that pattern to a very small helper that collapses to {"guild_id": guild_id}.