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

109 lines
2.8 KiB
Python
Raw Normal View History

2023-06-19 14:20:17 +00:00
import os
2024-06-20 18:57:45 +00:00
import sys
2023-06-19 14:20:17 +00:00
import discord
from discord.ext import commands
2024-06-20 18:57:45 +00:00
from loguru import logger
2024-06-20 17:48:49 +00:00
2024-06-20 10:34:03 +00:00
import Client
2024-06-20 17:48:49 +00:00
import config.parser
2024-06-20 10:34:03 +00:00
import services.config_service
import services.help_service
2024-08-18 09:16:18 +00:00
from db.database import run_migrations
from lib.constants import CONST
from lib.exceptions.LumiExceptions import Blacklisted
2024-08-18 09:16:18 +00:00
from services.blacklist_service import BlacklistUserService
2023-06-19 14:20:17 +00:00
2024-06-20 18:57:45 +00:00
# Remove the default logger configuration
logger.remove()
# Add a new logger configuration with colors and a short datetime format
log_format = (
"<green>{time:YY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
# "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - "
"<level>{message}</level>"
)
logger.add(sys.stdout, format=log_format, colorize=True, level="DEBUG")
2024-02-27 17:23:59 +00:00
2024-03-16 14:05:40 +00:00
2024-04-05 12:05:36 +00:00
async def get_prefix(bot, message):
2024-08-12 08:21:53 +00:00
extras = services.config_service.GuildConfig.get_prefix(message)
return commands.when_mentioned_or(*extras)(bot, message)
2024-03-16 14:05:40 +00:00
2024-06-20 17:48:49 +00:00
2024-06-20 10:34:03 +00:00
client = Client.LumiBot(
owner_ids=CONST.OWNER_IDS,
command_prefix=get_prefix,
2023-07-02 13:50:21 +00:00
intents=discord.Intents.all(),
2024-03-16 20:08:48 +00:00
status=discord.Status.online,
help_command=services.help_service.LumiHelp(),
2023-07-02 13:50:21 +00:00
)
2024-07-08 19:50:07 +00:00
@client.check
async def blacklist_check(ctx):
if BlacklistUserService.is_user_blacklisted(ctx.author.id):
raise Blacklisted
return True
2024-07-08 19:50:07 +00:00
2023-07-02 13:50:21 +00:00
2024-04-05 12:05:36 +00:00
def load_modules():
loaded = set()
2023-07-02 13:50:21 +00:00
2024-04-05 12:05:36 +00:00
# Load event listeners (handlers) and command cogs (modules)
for directory in ["handlers", "modules"]:
directory_path = os.path.join(os.getcwd(), directory)
if not os.path.isdir(directory_path):
continue
2023-07-02 13:50:21 +00:00
items = (
[
d
for d in os.listdir(directory_path)
if os.path.isdir(os.path.join(directory_path, d))
]
if directory == "modules"
else [f[:-3] for f in os.listdir(directory_path) if f.endswith(".py")]
)
2024-03-17 14:18:24 +00:00
2024-04-05 12:05:36 +00:00
for item in items:
if item in loaded:
continue
2024-04-05 12:05:36 +00:00
try:
client.load_extension(f"{directory}.{item}")
loaded.add(item)
logger.debug(f"{item.upper()} loaded.")
2024-04-05 12:05:36 +00:00
except Exception as e:
logger.exception(f"Failed to load {item.upper()}: {e}")
2024-03-17 14:18:24 +00:00
if __name__ == "__main__":
2023-07-02 13:50:21 +00:00
"""
2024-06-15 22:45:24 +00:00
This code is only ran when Lumi.py is the primary module,
2024-04-06 10:23:40 +00:00
so NOT when main is imported from a cog. (sys.modules)
2023-07-02 13:50:21 +00:00
"""
2024-06-20 18:57:45 +00:00
logger.info("LUMI IS BOOTING")
2024-02-27 14:09:59 +00:00
# Run database migrations
run_migrations()
2024-04-05 12:14:31 +00:00
# cache all JSON
[
config.parser.JsonCache.read_json(file[:-5])
for file in os.listdir("config/JSON")
if file.endswith(".json")
]
2024-04-05 12:14:31 +00:00
# load command and listener cogs
2024-03-17 14:18:24 +00:00
load_modules()
2024-02-27 14:09:59 +00:00
if not CONST.TOKEN:
logger.error("token is not set in .env")
exit(1)
client.run(CONST.TOKEN)