Files
cogs
bot.py
config.py
database.py
requirements.txt
utils.py
views.py
Code
11 1,822
bot.py
import logging
import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
from config import DB_PATH
from database import Database
from utils import error_embed
from views import DealView, PanelView
load_dotenv()
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
log = logging.getLogger("kodari")
COGS = ("cogs.general", "cogs.deals", "cogs.reputation", "cogs.admin")
class KodariBot(commands.Bot):
def __init__(self) -> None:
intents = discord.Intents.default()
intents.message_content = True
super().__init__(command_prefix=commands.when_mentioned, intents=intents, help_command=None)
self.db = Database(DB_PATH)
async def setup_hook(self) -> None:
await self.db.connect()
for extension in COGS:
await self.load_extension(extension)
self.add_view(PanelView())
self.add_view(DealView())
await self.tree.sync()
log.info("Slash commands synced")
async def on_ready(self) -> None:
log.info("Logged in as %s (%s)", self.user, self.user.id)
print(f"Bot is ready! Logged in as {self.user}")
print("KODARI_READY", flush=True)
async def close(self) -> None:
await self.db.close()
await super().close()
bot = KodariBot()
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError) -> None:
if isinstance(error, app_commands.CommandOnCooldown):
message = f"Slow down. Try again in {error.retry_after:.0f} seconds."
elif isinstance(error, app_commands.CheckFailure):
message = "You do not have permission to use this command."
else:
command_name = interaction.command.name if interaction.command else "unknown"
log.error("Unhandled error in /%s", command_name, exc_info=error)
message = "Something went wrong. The error has been logged."
embed = error_embed(message)
if interaction.response.is_done():
await interaction.followup.send(embed=embed, ephemeral=True)
else:
await interaction.response.send_message(embed=embed, ephemeral=True)
if __name__ == "__main__":
token = os.getenv("DISCORD_TOKEN")
if not token:
raise SystemExit("DISCORD_TOKEN is missing. Add it to your .env file.")
bot.run(token)