|
1 | | -from . import cogs, models |
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from logging.handlers import TimedRotatingFileHandler |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import discord |
| 8 | +from alembic import command |
| 9 | +from alembic.config import Config |
| 10 | +from dotenv import load_dotenv |
| 11 | +from sqlalchemy import event |
| 12 | +from sqlalchemy.engine.interfaces import DBAPIConnection |
| 13 | +from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine |
| 14 | +from sqlalchemy.pool import ConnectionPoolEntry |
| 15 | + |
2 | 16 | from .bot import DiscordBot |
3 | 17 |
|
4 | | -__all__ = ["DiscordBot", "models", "cogs"] |
| 18 | +# Initialization |
| 19 | +Path("./volume/logs").mkdir(parents=True, exist_ok=True) |
| 20 | +Path("./volume/gallery-dl").mkdir(parents=True, exist_ok=True) |
| 21 | + |
| 22 | +# Logger |
| 23 | +logger = logging.getLogger() |
| 24 | +logger.setLevel("INFO") |
| 25 | +logger.addHandler(logging.StreamHandler()) |
| 26 | +# Time Rotating File Handler |
| 27 | +logHandler = TimedRotatingFileHandler( |
| 28 | + Path("./volume/logs/discord.log"), when="D", backupCount=10, encoding="utf-8" |
| 29 | +) |
| 30 | +logHandler.setFormatter( |
| 31 | + logging.Formatter( |
| 32 | + "%(asctime)s %(levelname)-8s %(name)-15s %(message)s", |
| 33 | + datefmt="%H:%M:%S", |
| 34 | + ) |
| 35 | +) |
| 36 | +logger.addHandler(logHandler) |
| 37 | + |
| 38 | + |
| 39 | +# Load .env and basic sanity checking |
| 40 | +load_dotenv(dotenv_path="./.env") |
| 41 | +for env in ["DISCORD_TOKEN", "PREFIX"]: |
| 42 | + if not os.getenv(env): |
| 43 | + errMsg = f"{env} is not set in both .env and the OS environment. Exiting..." |
| 44 | + logger.error(errMsg) |
| 45 | + raise Exception(errMsg) |
| 46 | + |
| 47 | +logging.info("Running migrations...") |
| 48 | +alembic_cfg = Config("./alembic.ini") |
| 49 | +alembic_cfg.attributes["no_alembic_loggers"] = True |
| 50 | +command.upgrade(alembic_cfg, "head") |
| 51 | + |
| 52 | + |
| 53 | +async def main() -> None: |
| 54 | + """Entrypoint of the bot.""" |
| 55 | + |
| 56 | + logging.info("\nSetting up the bot...") |
| 57 | + |
| 58 | + # Database initialization |
| 59 | + engine = create_async_engine("sqlite+aiosqlite:///volume/db.sqlite3", echo=True) |
| 60 | + |
| 61 | + @event.listens_for(engine.sync_engine, "connect") |
| 62 | + def set_sqlite_pragma( |
| 63 | + dbapi_connection: DBAPIConnection, connection_record: ConnectionPoolEntry |
| 64 | + ) -> None: |
| 65 | + """If using SQLite, enable foreign key constraints.""" |
| 66 | + cursor = dbapi_connection.cursor() |
| 67 | + cursor.execute("PRAGMA foreign_keys=ON") |
| 68 | + cursor.close() |
| 69 | + |
| 70 | + async_session = async_sessionmaker(engine, expire_on_commit=False) |
| 71 | + |
| 72 | + intents = discord.Intents.default() |
| 73 | + intents.members = True |
| 74 | + intents.message_content = True |
| 75 | + |
| 76 | + activity = discord.Game(name="/help") |
| 77 | + |
| 78 | + description = "Discord bot my own use. \nCreated with [discord.py](https://github.com/Rapptz/discord.py)." |
| 79 | + |
| 80 | + bot = DiscordBot( |
| 81 | + os.getenv("PREFIX", ">>"), intents, activity, description, async_session |
| 82 | + ) |
| 83 | + |
| 84 | + await bot.start(os.getenv("DISCORD_TOKEN", "")) |
| 85 | + |
| 86 | + |
| 87 | +asyncio.run(main()) |
0 commit comments