Files
__init__.py
bot.py
config.py
moderation.py
requirements.txt
utility.py
Code
6 429
/home/user/UtilityBot/bot.py
import discord
from discord.ext import commands
import logging
import config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger(__name__)
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(
command_prefix=config.COMMAND_PREFIX,
intents=intents,
help_command=None,
)
@bot.event
async def on_ready():
log.info(f"Logged in as {bot.user} (ID: {bot.user.id})")
log.info(f"Connected to {len(bot.guilds)} guild(s)")
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name=f"{config.COMMAND_PREFIX}help",
)
)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.MissingPermissions):
await ctx.send(
embed=discord.Embed(
title="Permission Denied",
description="You don't have permission to use this command.",
color=config.ERROR_COLOR,
)
)
return
if isinstance(error, commands.BotMissingPermissions):
await ctx.send(
embed=discord.Embed(
title="Bot Permission Denied",
description="I don't have the required permissions to do that.",
color=config.ERROR_COLOR,
)
)
return
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(
embed=discord.Embed(
title="Missing Argument",
description=f"Usage: `{ctx.prefix}{ctx.command.qualified_name} {ctx.command.signature}`",
color=config.ERROR_COLOR,
)
)
return
if isinstance(error, commands.BadArgument):
await ctx.send(
embed=discord.Embed(
title="Invalid Argument",
description=str(error),
color=config.ERROR_COLOR,
)
)
return
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(
embed=discord.Embed(
title="Command on Cooldown",
description=f"Try again in **{error.retry_after:.1f}s**.",
color=config.ERROR_COLOR,
)
)
return
log.error(f"Unhandled error in {ctx.command}: {error}")
await ctx.send(
embed=discord.Embed(
title="Unexpected Error",
description="An unexpected error occurred. Please try again later.",
color=config.ERROR_COLOR,
)
)
async def load_extensions():
import os
for filename in os.listdir("cogs"):
if filename.endswith(".py") and not filename.startswith("_"):
try:
await bot.load_extension(f"cogs.{filename[:-3]}")
log.info(f"Loaded cog: cogs/{filename}")
except Exception as e:
log.error(f"Failed to load cog cogs/{filename}: {e}")
async def main():
async with bot:
await load_extensions()
await bot.start(config.TOKEN)
if __name__ == "__main__":
import asyncio
asyncio.run(main())