PYRSS-Bot/src/bot.py
2023-12-13 23:22:33 +00:00

54 lines
1.5 KiB
Python

"""
The discord bot for the application.
"""
import logging
from pathlib import Path
from discord import Intents
from discord.ext import commands
from sqlalchemy import insert
from db import DatabaseManager, AuditModel
log = logging.getLogger(__name__)
class DiscordBot(commands.Bot):
def __init__(self, BASE_DIR: Path):
super().__init__(command_prefix="-", intents=Intents.all())
self.BASE_DIR = BASE_DIR
async def sync_app_commands(self):
"""
Sync application commands between discord and the bot.
"""
await self.wait_until_ready()
await self.tree.sync()
log.info("Application commands successfully synced")
async def on_ready(self):
"""
Execute init operations that require the bot to be ready.
Ideally should not be manually called, this is handled by discord.py
"""
await self.sync_app_commands()
async def load_extensions(self):
"""
Load any extensions found in the extensions dictionary.
"""
for path in (self.BASE_DIR / "src/extensions").iterdir():
if path.suffix == ".py":
await self.load_extension(f"extensions.{path.stem}")
async def audit(self, message: str, user_id: int):
async with DatabaseManager() as database:
message = f"Requesting latest article"
query = insert(AuditModel).values(discord_user_id=user_id, message=message)
await database.session.execute(query)