Files
.gitignore
admin.py
bot.py
config.py
database.py
economy.py
features.py
leveling.py
README.md
requirements.txt
utils.py
Code
11 1,000
bot.py
import logging
import discord
from discord import app_commands
from discord.ext import commands
from admin import AdminCog
from config import load_settings
from database import Database
from economy import EconomyCog
from features import FeatureCog, VerificationCommands, VerifyView
from leveling import LevelingCog
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(name)s | %(message)s")
logger = logging.getLogger("kodari")
class KodariBot(commands.Bot):
def __init__(self, database: Database, settings):
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.reactions = True
super().__init__(command_prefix=settings.prefixes, intents=intents, help_command=None)
self.database = database
self.settings = settings
async def setup_hook(self) -> None:
await self.add_cog(AdminCog(self.database))
await self.add_cog(EconomyCog(self.database))
await self.add_cog(LevelingCog(self, self.database))
await self.add_cog(FeatureCog(self, self.database))
await self.add_cog(VerificationCommands(self, self.database))
self.add_view(VerifyView(self.database))
guilds = list(self.guilds)
for guild in guilds:
self.tree.copy_global_to(guild=guild)
# Remove previously registered global commands so commands are not duplicated.
self.tree.clear_commands(guild=None)
await self.tree.sync()
for guild in guilds:
try:
synced = await self.tree.sync(guild=guild)
logger.info("Synced %d commands to %s (%s)", len(synced), guild.name, guild.id)
except discord.Forbidden:
logger.warning("Could not sync commands to %s (%s): missing access", guild.name, guild.id)
settings = load_settings()
bot = KodariBot(Database(settings.database_path), settings)
@bot.event
async def on_ready() -> None:
if bot.user:
logger.info("Logged in as %s (%s)", bot.user, bot.user.id)
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError) -> None:
if isinstance(error, app_commands.MissingPermissions):
message = "You do not have permission to use this command."
elif isinstance(error, app_commands.NoPrivateMessage):
message = "This command can only be used in a server."
else:
logger.error("Slash 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)
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None:
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, (commands.MissingPermissions, commands.NoPrivateMessage)):
message = "You do not have permission to use this command." if isinstance(error, commands.MissingPermissions) else "This command can only be used in a server."
elif isinstance(error, commands.MissingRequiredArgument):
message = f"Missing argument: `{error.param.name}`."
elif isinstance(error, commands.BadArgument):
message = "One or more arguments are invalid."
else:
logger.error("Prefix command failed", exc_info=error)
message = "Something went wrong while running that command."
await ctx.send(message)
try:
bot.run(settings.token)
finally:
bot.database.close()