Files
WheatlyBot/main.py
2025-03-30 17:34:47 +10:00

99 lines
2.7 KiB
Python

import os
import discord
import asyncio
from dotenv import load_dotenv
import signal
from pathlib import Path
from util.console import console, panel, track_iterable as track
from data.init import init_databses
# Load environment variables from .env file
load_dotenv()
TOKEN = os.getenv("BOT_TOKEN")
DEV_GUILD_ID = int(os.getenv("DEV_GUILD_ID"))
REACT_ROLE_CHANNEL_ID = int(os.getenv("REACT_ROLE_CHANNEL_ID"))
# Set up intents for the bot
intents = discord.Intents.default()
intents.message_content = True
# Create the bot instance
bot = discord.Bot(intents=intents)
# Cog directory and Loading handling
COG_PATH = Path(__file__).resolve().parent / "cogs"
cogs_available = {}
loaded_cog_modules = {}
# Add available cogs to the list
for file in COG_PATH.iterdir():
if file.suffix == ".py" and not file.name.startswith("__"):
cog_name = file.stem
cogs_available[cog_name] = file
############################
### COGS LOADING HANDLER ###
############################
async def load_cogs():
# Check if there are any cogs to load
if not cogs_available:
console.log("[yellow]⚙️ No cogs available to load...[/yellow]")
return
# Check if the cogs directory exists
if not COG_PATH.exists():
console.log(f"[red]❌ Cogs directory not found at {COG_PATH}[/red]")
return
console.print(panel(content="🔌 [bold]Loading Cogs[/bold]", style="cyan"))
# Load each cog
for cog in track(cogs_available, description="Loading Cogs..."):
module_path = f"cogs.{cog}"
try:
bot.load_extension(module_path)
loaded_cog_modules[cog] = module_path
console.log(f"[green]✔ Loaded:[/] Cog - {cog}")
except Exception as e:
console.log(f"[red]✖ Failed to load:[/] Cog - {cog}")
console.log(f"[red]{type(e).__name__}: {e}[/]")
# Bot Shutdown handling
async def shutdown():
console.log("[red]🔴 Shutting down...[/red]")
await bot.close()
def handle_signals():
# Handle shutdown signals
for sig in (signal.SIGINT, signal.SIGTERM):
signal.signal(sig, lambda s, f: asyncio.create_task(shutdown()))
# Bot Event Handlers
@bot.event
async def on_ready():
await load_cogs()
await bot.sync_commands()
console.log("[blue]🔁 Synced slash commands[/blue]")
init_databses()
console.log("[blue]💽 Initialized databases[/blue]")
console.rule(f"[bold green]✅ Bot Ready — Logged in as {bot.user}[/]")
console.print(f"ID: {bot.user.id}")
handle_signals()
# Main Loop
if __name__ == "__main__":
async def main():
# Load cogs before the bot starts
#await load_cogs()
# Start the bot
await bot.start(TOKEN)
# Run the main loop
asyncio.run(main())