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
|
2024-08-12 07:35:37 +00:00
|
|
|
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-07-16 19:53:42 +00:00
|
|
|
from lib.constants import CONST
|
2024-07-08 19:50:07 +00:00
|
|
|
from services.blacklist_service import BlacklistUserService
|
2024-08-14 10:30:52 +00:00
|
|
|
from lib.exceptions.LumiExceptions import Blacklisted
|
2024-07-19 20:14:28 +00:00
|
|
|
from db.database import run_migrations
|
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)
|
2024-08-12 07:35:37 +00:00
|
|
|
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(
|
2024-07-16 19:53:42 +00:00
|
|
|
owner_ids=CONST.OWNER_IDS,
|
2024-03-16 19:44:46 +00:00
|
|
|
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,
|
2024-07-16 19:53:42 +00:00
|
|
|
help_command=services.help_service.LumiHelp(),
|
2023-07-02 13:50:21 +00:00
|
|
|
)
|
|
|
|
|
2024-07-16 19:53:42 +00:00
|
|
|
|
2024-07-08 19:50:07 +00:00
|
|
|
@client.check
|
|
|
|
async def blacklist_check(ctx):
|
2024-08-14 10:30:52 +00:00
|
|
|
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
|
|
|
|
2024-07-16 19:53:42 +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-03-17 18:49:12 +00:00
|
|
|
|
2024-04-05 12:05:36 +00:00
|
|
|
try:
|
|
|
|
client.load_extension(f"{directory}.{item}")
|
|
|
|
loaded.add(item)
|
2024-07-16 19:53:42 +00:00
|
|
|
logger.debug(f"{item.upper()} loaded.")
|
2024-03-17 18:49:12 +00:00
|
|
|
|
2024-04-05 12:05:36 +00:00
|
|
|
except Exception as e:
|
2024-07-16 19:53:42 +00:00
|
|
|
logger.exception(f"Failed to load {item.upper()}: {e}")
|
2024-03-17 14:18:24 +00:00
|
|
|
|
|
|
|
|
2024-07-16 19:53:42 +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
|
|
|
|
2024-07-19 20:14:28 +00:00
|
|
|
# Run database migrations
|
|
|
|
run_migrations()
|
|
|
|
|
2024-04-05 12:14:31 +00:00
|
|
|
# cache all JSON
|
2024-07-16 19:53:42 +00:00
|
|
|
[
|
|
|
|
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
|
|
|
|
2024-07-16 19:53:42 +00:00
|
|
|
if not CONST.TOKEN:
|
2024-07-17 12:49:10 +00:00
|
|
|
logger.error("token is not set in .env")
|
2024-07-16 19:53:42 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
client.run(CONST.TOKEN)
|