Files
wise
.gitignore
bot.py
README.md
requirements.txt
Code
16 1,027
bot.py
import logging
import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
from wise.config import Settings
from wise.database import Database
load_dotenv()
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(name)s | %(message)s")
log = logging.getLogger("wise")
class WiseBot(commands.Bot):
def __init__(self, settings: Settings):
intents = discord.Intents.all()
super().__init__(command_prefix="!", intents=intents, help_command=None)
self.settings = settings
self.db = Database(settings.database)
async def setup_hook(self):
await self.db.initialize()
extensions = (
"wise.cogs.core",
"wise.cogs.moderation",
"wise.cogs.spy",
"wise.cogs.fun",
"wise.cogs.economy",
"wise.cogs.tickets",
"wise.cogs.automation",
)
for extension in extensions:
await self.load_extension(extension)
if self.settings.guild_id:
guild = discord.Object(id=self.settings.guild_id)
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
log.info("Synced commands to guild %s", self.settings.guild_id)
else:
await self.tree.sync()
log.info("Synced global commands")
async def on_ready(self):
log.info("γ W.I.S.E. γ online as %s", self.user)
async def on_app_command_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.CommandOnCooldown):
message = f"β¦ This mission is on cooldown. Try again in `{error.retry_after:.1f}s`."
elif isinstance(error, app_commands.MissingPermissions):
message = "π΅οΈ Your clearance level is insufficient for that mission."
elif isinstance(error, app_commands.CheckFailure):
message = "π΅οΈ This command can only be used in a server."
else:
log.exception("Unhandled slash command error", exc_info=error)
message = "π W.I.S.E. encountered an operational hiccup. Please try again."
if interaction.response.is_done():
await interaction.followup.send(message, ephemeral=True)
else:
await interaction.response.send_message(message, ephemeral=True)
async def close(self):
await self.db.close()
await super().close()
def main():
token = os.getenv("DISCORD_TOKEN")
if not token:
raise RuntimeError("DISCORD_TOKEN is missing from .env")
bot = WiseBot(Settings.from_environment())
bot.run(token, log_handler=None)
if __name__ == "__main__":
main()