Files
cogs
.gitignore
bot.py
config.py
database.py
README.md
requirements.txt
Code
10 607
bot.py
import logging
import discord
from discord import app_commands
from discord.ext import commands
from config import Settings
from database import Database
class KodariBot(commands.Bot):
def __init__(self, settings: Settings) -> None:
intents = discord.Intents.default()
intents.guilds = True
intents.members = True
intents.message_content = True
intents.messages = True
super().__init__(
command_prefix=commands.when_mentioned,
intents=intents,
help_command=None,
)
self.settings = settings
self.db = Database(settings.database_path)
async def setup_hook(self) -> None:
self.db.initialize()
for extension in ("cogs.general", "cogs.moderation", "cogs.community"):
await self.load_extension(extension)
if self.settings.test_guild_id:
guild = discord.Object(id=self.settings.test_guild_id)
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
logging.info("Synchronized commands to test guild %s", guild.id)
else:
await self.tree.sync()
logging.info("Synchronized global application commands")
async def on_ready(self) -> None:
if self.user:
logging.info("Logged in as %s (%s)", self.user, self.user.id)
async def handle_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.BotMissingPermissions):
message = "I am missing the Discord permissions required for that action."
elif isinstance(error, app_commands.errors.CommandOnCooldown):
message = f"Try again in {error.retry_after:.1f} seconds."
else:
logging.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)
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
settings = Settings.from_environment()
bot = KodariBot(settings)
bot.tree.on_error = handle_app_command_error
bot.run(settings.token, log_handler=None)
if __name__ == "__main__":
main()