Files
cogs
services
.gitignore
bot.py
README.md
requirements.txt
Code
13 852
bot.py
cp .env.example .env
pip install -r requirements.txt
python bot.py
import logging
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from services.database import Database
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("kodari-bot")
TOKEN = os.getenv("DISCORD_TOKEN")
PREFIX = os.getenv("COMMAND_PREFIX", "!")
DATABASE_PATH = os.getenv("DATABASE_PATH", "bot.sqlite3")
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.voice_states = True
class ServerBot(commands.Bot):
def __init__(self) -> None:
super().__init__(
command_prefix=PREFIX,
intents=intents,
help_command=commands.DefaultHelpCommand(no_category="Other commands"),
)
self.db = Database(DATABASE_PATH)
async def setup_hook(self) -> None:
extensions = (
"cogs.moderation",
"cogs.music",
"cogs.mover",
"cogs.leveling",
"cogs.greetings",
"cogs.invites",
)
for extension in extensions:
await self.load_extension(extension)
logger.info("Loaded extension %s", extension)
async def close(self) -> None:
self.db.close()
await super().close()
bot = ServerBot()
@bot.event
async def on_ready() -> None:
if bot.user is not None:
logger.info("Logged in as %s (%s)", bot.user, bot.user.id)
await bot.change_presence(activity=discord.Game(name=f"{PREFIX}help"))
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None:
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f"Try again in {error.retry_after:.1f} seconds.", delete_after=5)
return
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permission to use that command.", delete_after=6)
return
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("I am missing the permissions needed for that command.", delete_after=8)
return
if isinstance(error, commands.NoPrivateMessage):
await ctx.send("That command can only be used in a server.", delete_after=6)
return
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"Missing argument: `{error.param.name}`.", delete_after=6)
return
if isinstance(error, commands.BadArgument):
await ctx.send("I could not understand one of those arguments.", delete_after=6)
return
if isinstance(error, discord.Forbidden):
await ctx.send("Discord refused that action. Check my role position and permissions.", delete_after=8)
return
logger.exception("Unhandled command error in %s", getattr(ctx.command, "qualified_name", "unknown"), exc_info=error)
await ctx.send("Something went wrong while running that command.", delete_after=8)
def main() -> None:
if not TOKEN:
raise RuntimeError("DISCORD_TOKEN is not set. Copy .env.example to .env and add your token.")
bot.run(TOKEN, log_handler=None)
if __name__ == "__main__":
main()