1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 22:23:13 +00:00
Lumi/Client.py

174 lines
5.4 KiB
Python
Raw Normal View History

2024-03-23 10:45:48 +00:00
import os
import platform
2024-06-20 18:57:45 +00:00
from loguru import logger
2024-03-23 10:45:48 +00:00
2024-03-29 17:17:51 +00:00
import discord
2024-07-07 14:31:51 +00:00
from discord.ext import bridge, commands
from discord.ext.commands import TextChannelConverter
from typing import Optional
2024-03-23 10:45:48 +00:00
from lib import metadata
2024-03-23 10:45:48 +00:00
2024-06-15 22:45:24 +00:00
class LumiBot(bridge.Bot):
2024-03-23 10:45:48 +00:00
async def on_ready(self):
2024-07-07 14:36:48 +00:00
"""
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.
"""
2024-06-20 18:57:45 +00:00
logger.info(f"{metadata.__title__} v{metadata.__version__}")
2024-07-07 14:31:51 +00:00
logger.info(f"Logged in with ID {self.user.id if self.user else 'Unknown'}")
2024-06-20 18:57:45 +00:00
logger.info(f"discord.py API version: {discord.__version__}")
logger.info(f"Python version: {platform.python_version()}")
2024-06-21 20:00:31 +00:00
logger.info(f"Running on: {platform.system()} {platform.release()} ({os.name})")
2024-03-23 10:45:48 +00:00
2024-07-07 14:36:48 +00:00
async def process_commands(self, message: discord.Message):
2024-03-23 10:45:48 +00:00
"""
2024-07-07 14:36:48 +00:00
Processes commands sent by users.
2024-03-23 10:45:48 +00:00
2024-07-07 14:36:48 +00:00
Args:
message (discord.Message): The message object containing the command.
"""
2024-03-28 19:53:14 +00:00
if message.author.bot:
return
ctx = await self.get_context(message)
2024-03-29 09:54:51 +00:00
if ctx.command:
2024-07-08 19:50:07 +00:00
# await ctx.trigger_typing()
2024-03-29 09:54:51 +00:00
await self.invoke(ctx)
2024-07-08 19:50:07 +00:00
@staticmethod
async def convert_to_user(ctx: commands.Context | bridge.Context, user_id: int) -> Optional[discord.User]:
"""
Converts a user ID to a User 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.User]: The User object, or None if conversion fails.
"""
try:
if isinstance(ctx, bridge.BridgeApplicationContext):
return # TODO: Implement this
else:
return await commands.UserConverter().convert(ctx, str(user_id))
except (
discord.HTTPException,
discord.NotFound,
discord.Forbidden,
commands.BadArgument,
):
return None
2024-03-28 19:53:14 +00:00
2024-07-07 14:31:51 +00:00
@staticmethod
async def convert_to_text_channel(
ctx: commands.Context | bridge.Context, channel_id: int
2024-07-07 14:31:51 +00:00
) -> Optional[discord.TextChannel]:
2024-07-07 14:36:48 +00:00
"""
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.
"""
2024-07-07 14:31:51 +00:00
converter = TextChannelConverter()
try:
if isinstance(ctx, bridge.BridgeApplicationContext):
return # TODO: Implement this
else:
return await converter.convert(ctx, str(channel_id))
2024-07-07 14:31:51 +00:00
except (
discord.HTTPException,
discord.NotFound,
discord.Forbidden,
commands.BadArgument,
):
return None
@staticmethod
async def convert_to_member(
ctx: commands.Context, user_id: int
) -> Optional[discord.Member]:
2024-07-07 14:36:48 +00:00
"""
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.
"""
2024-07-07 14:31:51 +00:00
converter = commands.MemberConverter()
try:
member = await converter.convert(ctx, str(user_id))
except (
discord.HTTPException,
discord.NotFound,
discord.Forbidden,
commands.BadArgument,
):
return None
return member
2024-03-23 10:45:48 +00:00
@staticmethod
2024-07-07 14:36:48 +00:00
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.
"""
2024-03-23 10:45:48 +00:00
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
2024-03-23 10:45:48 +00:00
return channel
2024-03-28 14:32:12 +00:00
@staticmethod
2024-07-07 14:36:48 +00:00
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.
"""
2024-03-28 14:32:12 +00:00
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
return member