Files
cogs
utils
.gitignore
bot.py
keep_alive.py
requirements.txt
Code
13 1,445
bot.py
from __future__ import annotations
import logging
import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
from keep_alive import start_keep_alive
from utils.database import Database
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
intents = discord.Intents.all()
class CommunitySupportBot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix="!",
intents=intents,
help_command=None,
)
self.db = Database()
async def setup_hook(self) -> None:
await self.db.initialize()
extensions = (
"cogs.welcome",
"cogs.admin",
"cogs.roles",
"cogs.session",
"cogs.tickets",
)
for extension in extensions:
await self.load_extension(extension)
roles_cog = self.get_cog("RolesCog")
if roles_cog:
await roles_cog.restore_views()
tickets_cog = self.get_cog("TicketsCog")
if tickets_cog:
await tickets_cog.restore_views()
await self.tree.sync()
async def close(self) -> None:
await self.db.close()
await super().close()
bot = CommunitySupportBot()
@bot.event
async def on_ready() -> None:
logging.getLogger("CommunitySupportBot").info(
"Logged in as %s (%s)",
bot.user,
bot.user.id if bot.user else "unknown",
)
@bot.tree.error
async def on_app_command_error(
interaction: discord.Interaction,
error: app_commands.AppCommandError,
) -> None:
if isinstance(error, app_commands.errors.MissingPermissions):
message = "You do not have permission to use this command."
elif isinstance(error, app_commands.errors.CheckFailure):
message = "You do not meet the requirements for this command."
else:
logging.getLogger("CommunitySupportBot").exception(
"Application command failed",
exc_info=error,
)
message = "Something went wrong while running that command."
if interaction.response.is_done():
await interaction.followup.send(message, ephemeral=True)
else:
await interaction.response.send_message(message, ephemeral=True)
token = os.getenv("DISCORD_TOKEN")
if not token:
raise RuntimeError("DISCORD_TOKEN is not set in the environment")
start_keep_alive()
bot.run(token)