Chitanda-Bot / main.py
E5K7's picture
Exclude functions.py from cog loading (it's a helper module)
f3747b5
import os
import pathlib
import discord
from discord.ext import commands
import json
import asyncio
import traceback
from keep_alive import keep_alive
try:
from dotenv import load_dotenv
load_dotenv()
except Exception:
pass
with open('config.json') as f:
config = json.load(f)
TOKEN = os.getenv("DISCORD_TOKEN")
PREFIX = config.get("prefix", "!")
intents = discord.Intents.all()
intents.message_content = True
bot = commands.Bot(
command_prefix=PREFIX,
intents=intents,
case_insensitive=True,
strip_after_prefix = True,
)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} slash command(s)")
except Exception as e:
print(f"Failed to sync commands: {e}")
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
error = getattr(error, 'original', error)
error_msg = ''.join(traceback.format_exception(type(error), error, error.__traceback__))
if len(error_msg) > 1000:
error_msg = error_msg[-1000:]
error_msg = "..." + error_msg
embed = discord.Embed(
title="❌ Error Log",
description=f"An error occurred while executing the command.",
color=0xff0000
)
embed.add_field(
name="Error Details",
value=f"```py\n{error_msg}\n```",
inline=False
)
embed.set_footer(text=f"Command: {ctx.command.name if ctx.command else 'Unknown'}")
try:
await ctx.send(embed=embed)
except:
await ctx.send(f"```py\n{error_msg}\n```")
async def load_cogs():
cogs_dir = pathlib.Path(__file__).parent / "cogs"
for file in cogs_dir.glob("*.py"):
if file.stem.startswith("_") or file.stem == "functions":
continue
ext = f"cogs.{file.stem}"
try:
await bot.load_extension(ext)
print(f"Loaded extension: {ext}")
except Exception as e:
print(f"Failed to load {ext}: {e}")
async def main():
keep_alive()
try:
await bot.load_extension('jishaku')
print("Loaded extension: jishaku")
except Exception as e:
print(f"Failed to load jishaku: {e}")
await load_cogs()
await bot.start(TOKEN)
if __name__ == "__main__":
asyncio.run(main())