1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20:43:12 +00:00
Lumi/main.py

158 lines
4.3 KiB
Python
Raw Normal View History

2023-06-19 14:20:17 +00:00
import os
2024-04-03 16:06:03 +00:00
import traceback
import asyncio
2023-06-19 14:20:17 +00:00
import discord
from dotenv import load_dotenv
from handlers import LoggingHandler, ErrorHandler
2023-06-29 11:35:12 +00:00
from handlers.ReactionHandler import ReactionHandler
2024-04-03 16:06:03 +00:00
from handlers.xp_handler import XPHandler
from lib.embeds.greet import Greet
from services.Client import RacuBot
2024-03-07 19:26:11 +00:00
from services.GuildConfig import GuildConfig
2024-03-16 20:08:48 +00:00
from services.Help import RacuHelp
2023-06-19 14:20:17 +00:00
2024-02-27 17:23:59 +00:00
load_dotenv('.env')
instance = os.getenv("INSTANCE")
2024-03-16 14:05:40 +00:00
def get_prefix(bot, message):
2024-03-19 07:34:29 +00:00
try:
return GuildConfig.get_prefix(message.guild.id)
except AttributeError:
return "."
2024-03-16 14:05:40 +00:00
2024-03-23 10:45:48 +00:00
client = RacuBot(
2024-03-19 16:28:55 +00:00
owner_id=int(os.getenv('OWNER_ID')),
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=RacuHelp()
2023-07-02 13:50:21 +00:00
)
2024-02-28 13:01:20 +00:00
logs = LoggingHandler.setup_logger()
2023-07-02 13:50:21 +00:00
@client.listen()
2023-06-29 11:21:17 +00:00
async def on_message(message):
2024-03-19 07:34:29 +00:00
if (
message.author.bot or
message.guild is None or
instance.lower() != "main"
):
2024-02-28 11:09:04 +00:00
return
2024-02-27 17:23:59 +00:00
2023-07-02 15:33:25 +00:00
try:
_xp = XPHandler(message)
leveled_up = _xp.process()
if leveled_up:
coros = [
asyncio.create_task(_xp.notify()),
asyncio.create_task(_xp.reward())
]
await asyncio.wait(coros)
2023-07-02 15:33:25 +00:00
reaction_handler = ReactionHandler()
2023-07-02 15:33:25 +00:00
await reaction_handler.handle_message(message)
2023-06-29 11:21:17 +00:00
2023-07-02 15:33:25 +00:00
except Exception as error:
2024-04-03 16:06:03 +00:00
logs.error(f"[EventHandler] on_message (check debug log): {error}", exc_info=True)
traceback.print_tb(error.__traceback__)
2023-06-29 11:35:12 +00:00
2023-06-29 11:21:17 +00:00
2024-02-28 13:01:20 +00:00
@client.event
2023-06-29 12:48:52 +00:00
async def on_member_join(member):
2024-03-07 19:26:11 +00:00
config = GuildConfig(member.guild.id)
2023-06-29 12:48:52 +00:00
2024-03-07 19:26:11 +00:00
if (not config.welcome_channel_id
2023-06-29 12:48:52 +00:00
2024-03-07 19:26:11 +00:00
# comment next line if debugging greetings
or instance.lower() != "main"
):
2024-02-27 18:20:21 +00:00
return
embed = Greet.message(member, config.welcome_message)
2024-03-07 19:26:11 +00:00
try:
await member.guild.get_channel(config.welcome_channel_id).send(embed=embed, content=member.mention)
except Exception as e:
logs.info(f"[GreetingHandler] Message not sent in '{member.guild.name}'. Channel ID may be invalid. {e}")
2023-06-29 12:48:52 +00:00
@client.listen()
2024-03-17 12:59:13 +00:00
async def on_command_completion(ctx) -> None:
2023-07-02 15:33:25 +00:00
"""
This code is executed when a slash_command has been successfully executed.
2024-02-27 18:20:21 +00:00
This technically serves as a CommandHandler function
2023-07-02 15:33:25 +00:00
:param ctx:
:return:
"""
full_command_name = ctx.command.qualified_name
split = full_command_name.split(" ")
executed_command = str(split[0])
if ctx.guild is not None:
2024-02-28 13:01:20 +00:00
# logs.info(
2024-02-27 18:20:21 +00:00
# f"Executed {executed_command} command in {ctx.guild.name} (ID: {ctx.guild.id}) "
# f"by {ctx.author} (ID: {ctx.author.id})"
# )
2024-02-28 13:01:20 +00:00
logs.info(f"[CommandHandler] {ctx.author.name} successfully did \"/{executed_command}\". "
2024-03-07 19:26:11 +00:00
f"| guild: {ctx.guild.name} ")
2023-07-02 15:33:25 +00:00
else:
2024-02-28 13:01:20 +00:00
# logs.info(
2024-02-27 18:20:21 +00:00
# f"Executed {executed_command} command by {ctx.author} (ID: {ctx.author.id}) in DMs."
# )
2024-02-28 13:01:20 +00:00
logs.info(f"[CommandHandler] {ctx.author.name} successfully did \"/{executed_command}\". | direct message")
2023-07-02 15:33:25 +00:00
@client.listen()
2024-03-17 12:59:13 +00:00
async def on_command_error(ctx, error) -> None:
await ErrorHandler.on_command_error(ctx, error)
2023-08-08 10:53:06 +00:00
@client.listen()
async def on_application_command_error(ctx, error) -> None:
await ErrorHandler.on_command_error(ctx, error)
2023-07-02 15:43:13 +00:00
2024-02-28 13:01:20 +00:00
@client.event
2024-01-02 13:32:02 +00:00
async def on_error(event: str, *args, **kwargs) -> None:
await ErrorHandler.on_error(event, *args, **kwargs)
2023-07-02 15:33:25 +00:00
2023-07-02 13:50:21 +00:00
2024-03-17 14:18:24 +00:00
def load_modules():
2024-03-18 17:48:12 +00:00
module_list = [d for d in os.listdir("modules") if os.path.isdir(os.path.join("modules", d))]
loaded_modules = set()
2024-03-17 14:18:24 +00:00
2024-03-18 17:48:12 +00:00
for module in module_list:
if module in loaded_modules:
2024-03-29 17:11:36 +00:00
continue # module is already loaded
try:
client.load_extension(f"modules.{module}")
loaded_modules.add(module)
logs.info(f"[MODULE] {module.upper()} loaded.")
except Exception as e:
logs.error(f"[MODULE] Failed to load module {module.upper()}: {e}")
2024-03-17 14:18:24 +00:00
2023-07-02 13:50:21 +00:00
if __name__ == '__main__':
"""
This code is only ran when main.py is the primary module,
thus NOT when main is imported from a cog. (sys.modules)
"""
2024-02-28 13:01:20 +00:00
logs.info("RACU IS BOOTING")
logs.info("\n")
2024-02-27 14:09:59 +00:00
2024-03-17 14:18:24 +00:00
load_modules()
2024-02-27 14:09:59 +00:00
# empty line to separate modules from system info in logs
2024-02-28 13:01:20 +00:00
logs.info("\n")
2024-02-27 17:23:59 +00:00
2024-02-28 13:01:20 +00:00
client.run(os.getenv('TOKEN'))