1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 18:23:12 +00:00

Fix documentation

This commit is contained in:
wlinator 2024-07-07 16:36:48 +02:00
parent 30d000924b
commit b61ca2d606
3 changed files with 73 additions and 24 deletions

View file

@ -12,19 +12,25 @@ from lib import metadata
class LumiBot(bridge.Bot):
async def on_ready(self):
"""
Called when the bot is ready.
Logs various information about the bot and the environment it is running on.
Note: This function isn't guaranteed to only be called once. The event is called when a RESUME request fails.
"""
logger.info(f"{metadata.__title__} v{metadata.__version__}")
logger.info(f"Logged in with ID {self.user.id if self.user else 'Unknown'}")
logger.info(f"discord.py API version: {discord.__version__}")
logger.info(f"Python version: {platform.python_version()}")
logger.info(f"Running on: {platform.system()} {platform.release()} ({os.name})")
"""
https://docs.pycord.dev/en/stable/api/events.html#discord.on_ready
This function isn't guaranteed to only be called once.
Event is called when RESUME request fails.
"""
async def process_commands(self, message: discord.Message):
"""
Processes commands sent by users.
Args:
message (discord.Message): The message object containing the command.
"""
if message.author.bot:
return
@ -38,6 +44,16 @@ class LumiBot(bridge.Bot):
async def convert_to_text_channel(
ctx: commands.Context, channel_id: int
) -> Optional[discord.TextChannel]:
"""
Converts a channel ID to a TextChannel object.
Args:
ctx (commands.Context): The context in which the command was invoked.
channel_id (int): The ID of the channel to convert.
Returns:
Optional[discord.TextChannel]: The TextChannel object, or None if conversion fails.
"""
converter = TextChannelConverter()
try:
@ -54,6 +70,16 @@ class LumiBot(bridge.Bot):
async def convert_to_member(
ctx: commands.Context, user_id: int
) -> Optional[discord.Member]:
"""
Converts a user ID to a Member object.
Args:
ctx (commands.Context): The context in which the command was invoked.
user_id (int): The ID of the user to convert.
Returns:
Optional[discord.Member]: The Member object, or None if conversion fails.
"""
converter = commands.MemberConverter()
try:
@ -69,26 +95,48 @@ class LumiBot(bridge.Bot):
return member
@staticmethod
async def get_or_fetch_channel(guild, channel_id):
async def get_or_fetch_channel(
guild: discord.Guild, channel_id: int
) -> Optional[discord.abc.GuildChannel]:
"""
Retrieves a channel from the guild's cache or fetches it from the API if not found.
Args:
guild (discord.Guild): The guild object.
channel_id (int): The ID of the channel to retrieve or fetch.
Returns:
Optional[discord.abc.GuildChannel]: The channel object, or None if not found or an error occurs.
"""
channel = guild.get_channel(channel_id)
if not channel:
try:
channel = await guild.fetch_channel(channel_id)
except (discord.HTTPException, discord.NotFound, discord.Forbidden):
return None
return channel
@staticmethod
async def get_or_fetch_member(guild, user_id):
async def get_or_fetch_member(
guild: discord.Guild, user_id: int
) -> Optional[discord.Member]:
"""
Retrieves a member from the guild's cache or fetches them from the API if not found.
Args:
guild (discord.Guild): The guild object.
user_id (int): The ID of the member to retrieve or fetch.
Returns:
Optional[discord.Member]: The member object, or None if not found or an error occurs.
"""
member = guild.get_member(user_id)
if not member:
try:
member = await guild.fetch_member(user_id)
except (discord.HTTPException, discord.NotFound, discord.Forbidden):
return None

View file

@ -20,9 +20,10 @@ _messages = JsonCache.read_json("levels")
class XPHandler:
def __init__(self, client: LumiBot, message: discord.Message) -> None:
"""
Initializes the XPHandler with the given message.
Initializes the XPHandler with the given client and message.
Args:
client (LumiBot): The bot client.
message (discord.Message): The message object.
"""
self.client = client
@ -48,10 +49,10 @@ class XPHandler:
if _xp.cooldown_time and _now < _xp.cooldown_time:
return False
# award the amount of XP specified in .env
# Award the amount of XP specified in .env
_xp.xp += _xp.xp_gain
# check if total xp now exceeds the xp required to level up
# Check if total XP now exceeds the XP required to level up
if _xp.xp >= XpService.xp_needed_for_next_level(_xp.level):
_xp.level += 1
_xp.xp = 0
@ -125,7 +126,7 @@ class XPHandler:
guild_config (GuildConfig): The guild configuration.
Returns:
Optional[discord.abc.GuildChannel]: The level up notification channel, or None if not found.
Optional[discord.TextChannel]: The level up notification channel, or None if not found.
"""
if guild_config.level_channel_id and message.guild:
context = await self.client.get_context(message)
@ -204,7 +205,7 @@ class XPHandler:
break
if level_range is None:
# generic fallback
# Generic fallback
return XPHandler.level_message_generic(level, author)
message_list = _messages[level_range]
@ -219,7 +220,7 @@ class XpListener(commands.Cog):
Initializes the XpListener with the given client.
Args:
client (commands.Bot): The bot client.
client (LumiBot): The bot client.
"""
self.client: LumiBot = client

View file

@ -13,7 +13,7 @@ xp_gain_cooldown: int = int(os.environ.get("LUMI_XP_GAIN_COOLDOWN", 8))
class XpService:
"""
Stores and retrieves XP from the database for a given user.
Manages XP for a user, including storing, retrieving, and updating XP in the database.
"""
def __init__(self, user_id: int, guild_id: int) -> None:
@ -50,7 +50,7 @@ class XpService:
def fetch_or_create_xp(self) -> None:
"""
Gets a user's XP from the database or inserts a new row if it doesn't exist yet.
Retrieves a user's XP from the database or inserts a new row if it doesn't exist yet.
"""
query: str = "SELECT user_xp, user_level, cooldown FROM xp WHERE user_id = %s AND guild_id = %s"
@ -75,10 +75,10 @@ class XpService:
def calculate_rank(self) -> Optional[int]:
"""
Checks which rank a user is in the guild.
Determines the rank of a user in the guild based on their XP and level.
Returns:
int | None: The rank of the user in the guild, or None if not found.
Optional[int]: The rank of the user in the guild, or None if not found.
"""
query: str = """
SELECT user_id, user_xp, user_level
@ -100,13 +100,13 @@ class XpService:
@staticmethod
def load_leaderboard(guild_id: int) -> List[Tuple[int, int, int, int]]:
"""
Returns the guild's XP leaderboard.
Retrieves the guild's XP leaderboard.
Args:
guild_id (int): The ID of the guild.
Returns:
list: A list of tuples containing user_id, user_xp, user_level, and needed_xp_for_next_level.
List[Tuple[int, int, int, int]]: A list of tuples containing user_id, user_xp, user_level, and needed_xp_for_next_level.
"""
query: str = """
SELECT user_id, user_xp, user_level
@ -155,7 +155,7 @@ class XpService:
@staticmethod
def xp_needed_for_next_level(current_level: int) -> int:
"""
Calculates the amount of XP needed to go to the next level, based on the current level.
Calculates the amount of XP needed to reach the next level, based on the current level.
Args:
current_level (int): The current level of the user.
@ -191,7 +191,7 @@ class XpService:
class XpRewardService:
"""
Manages XP rewards for a guild.
Manages XP rewards for a guild, including storing, retrieving, and updating rewards in the database.
"""
def __init__(self, guild_id: int) -> None: