Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions tux/database/controllers/levels.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import math
from typing import NoReturn, cast

Check warning on line 3 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L3

Added line #L3 was not covered by tests

from loguru import logger

Expand Down Expand Up @@ -78,18 +79,29 @@
Returns
-------
tuple[float, int]
A tuple containing the XP and level of the member, or (0.0, 0) if not found
A tuple containing the XP and level of the member.
"""

def _fail(msg: str) -> NoReturn:
raise ValueError(msg)

Check warning on line 86 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L85-L86

Added lines #L85 - L86 were not covered by tests

try:
record = await self.find_one(where={"member_id": member_id, "guild_id": guild_id})
if record is None:
logger.debug(

Check warning on line 91 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L91

Added line #L91 was not covered by tests
f"Level record not found for member_id: {member_id}, guild_id: {guild_id}. Returning 0.0, 0",
)
return 0.0, 0

Check warning on line 94 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L94

Added line #L94 was not covered by tests

xp = getattr(record, "xp", None)
level = getattr(record, "level", None)

Check warning on line 97 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L96-L97

Added lines #L96 - L97 were not covered by tests
if xp is None or level is None:
_fail(f"Levels record missing xp/level for member {member_id} in guild {guild_id}")

Check warning on line 99 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L99

Added line #L99 was not covered by tests

if record:
return (self.safe_get_attr(record, "xp", 0.0), self.safe_get_attr(record, "level", 0))
return (0.0, 0) # noqa: TRY300
return cast(float, xp), cast(int, level)

Check warning on line 101 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L101

Added line #L101 was not covered by tests

except Exception as e:
logger.error(f"Error querying XP and level for member_id: {member_id}, guild_id: {guild_id}: {e}")
return (0.0, 0)
_fail(f"Error querying XP and level for member_id: {member_id}, guild_id: {guild_id}: {e}")

Check warning on line 104 in tux/database/controllers/levels.py

View check run for this annotation

Codecov / codecov/patch

tux/database/controllers/levels.py#L104

Added line #L104 was not covered by tests

async def get_last_message_time(self, member_id: int, guild_id: int) -> datetime.datetime | None:
"""Get the last message time of a member in a guild.
Expand Down