Added stubs for classes and cogs, performed a cleanup, added more database indexes
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
from .pycord_event import PycordEvent
|
||||
from .pycord_event_stage import PycordEventStage
|
||||
from .pycord_guild import PycordGuild
|
||||
from .pycord_user import PycordUser
|
||||
|
@@ -2,7 +2,6 @@ import logging
|
||||
from logging import Logger
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import ClientSession
|
||||
from discord import User
|
||||
from libbot.cache.manager import create_cache_client
|
||||
from libbot.pycord.classes import PycordBot as LibPycordBot
|
||||
@@ -12,17 +11,12 @@ from classes import PycordUser
|
||||
logger: Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# from modules.tracking.dhl import update_tracks_dhl
|
||||
|
||||
|
||||
class PycordBot(LibPycordBot):
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self._set_cache_engine()
|
||||
|
||||
self.client_session = ClientSession()
|
||||
|
||||
if self.scheduler is None:
|
||||
return
|
||||
|
||||
@@ -63,8 +57,6 @@ class PycordBot(LibPycordBot):
|
||||
await super().start(*args, **kwargs)
|
||||
|
||||
async def close(self, **kwargs) -> None:
|
||||
await self.client_session.close()
|
||||
|
||||
if self.scheduler is not None:
|
||||
self.scheduler.shutdown()
|
||||
|
||||
|
17
classes/pycord_event.py
Normal file
17
classes/pycord_event.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
from bson import ObjectId
|
||||
|
||||
|
||||
@dataclass
|
||||
class PycordEvent:
|
||||
_id: ObjectId
|
||||
id: int
|
||||
guild_id: int
|
||||
created: datetime
|
||||
creator_id: int
|
||||
starts: datetime
|
||||
ends: datetime
|
||||
stage_ids: List[int]
|
18
classes/pycord_event_stage.py
Normal file
18
classes/pycord_event_stage.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
from bson import ObjectId
|
||||
|
||||
|
||||
@dataclass
|
||||
class PycordEventStage:
|
||||
_id: ObjectId
|
||||
id: int
|
||||
event_id: int
|
||||
guild_id: int
|
||||
sequence: int
|
||||
created: datetime
|
||||
creator_id: int
|
||||
text: str | None
|
||||
media: List[str]
|
14
cogs/config.py
Normal file
14
cogs/config.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from discord.ext.commands import Cog
|
||||
|
||||
from classes.pycord_bot import PycordBot
|
||||
|
||||
|
||||
class Config(Cog):
|
||||
"""Cog with guild configuration commands."""
|
||||
|
||||
def __init__(self, bot: PycordBot):
|
||||
self.bot: PycordBot = bot
|
||||
|
||||
|
||||
def setup(bot: PycordBot) -> None:
|
||||
bot.add_cog(Config(bot))
|
14
cogs/event.py
Normal file
14
cogs/event.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from discord.ext.commands import Cog
|
||||
|
||||
from classes.pycord_bot import PycordBot
|
||||
|
||||
|
||||
class Event(Cog):
|
||||
"""Cog with event management commands."""
|
||||
|
||||
def __init__(self, bot: PycordBot):
|
||||
self.bot: PycordBot = bot
|
||||
|
||||
|
||||
def setup(bot: PycordBot) -> None:
|
||||
bot.add_cog(Event(bot))
|
14
cogs/stage.py
Normal file
14
cogs/stage.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from discord.ext.commands import Cog
|
||||
|
||||
from classes.pycord_bot import PycordBot
|
||||
|
||||
|
||||
class Stage(Cog):
|
||||
"""Cog with event stage management commands."""
|
||||
|
||||
def __init__(self, bot: PycordBot):
|
||||
self.bot: PycordBot = bot
|
||||
|
||||
|
||||
def setup(bot: PycordBot) -> None:
|
||||
bot.add_cog(Stage(bot))
|
30
main.py
30
main.py
@@ -1,16 +1,16 @@
|
||||
import asyncio
|
||||
import contextlib
|
||||
import logging.config
|
||||
from argparse import ArgumentParser
|
||||
from logging import Logger
|
||||
from os import getpid, makedirs
|
||||
from os import makedirs
|
||||
from pathlib import Path
|
||||
from sys import exit
|
||||
|
||||
# Import required for uvicorn
|
||||
from discord import LoginFailure
|
||||
from libbot.utils import config_get
|
||||
|
||||
from classes.pycord_bot import PycordBot
|
||||
from modules.logging_utils import get_logging_config, get_logger
|
||||
from modules.logging_utils import get_logger, get_logging_config
|
||||
from modules.migrator import migrate_database
|
||||
from modules.scheduler import scheduler
|
||||
|
||||
@@ -21,9 +21,7 @@ logging.config.dictConfig(get_logging_config())
|
||||
logger: Logger = get_logger(__name__)
|
||||
|
||||
# Declare the parser that retrieves the command line arguments
|
||||
parser: ArgumentParser = ArgumentParser(
|
||||
prog="QuizBot"
|
||||
)
|
||||
parser: ArgumentParser = ArgumentParser(prog="QuizBot")
|
||||
|
||||
# Add a switch argument --migrate to be parsed...
|
||||
parser.add_argument("--migrate", action="store_true")
|
||||
@@ -41,7 +39,7 @@ with contextlib.suppress(ImportError):
|
||||
uvloop.install()
|
||||
|
||||
|
||||
async def main():
|
||||
def main():
|
||||
# Perform migration if command line argument was provided
|
||||
if args.migrate:
|
||||
migrate_database()
|
||||
@@ -57,16 +55,22 @@ async def main():
|
||||
# downgrade_database()
|
||||
# exit()
|
||||
|
||||
bot = PycordBot(scheduler=scheduler)
|
||||
bot: PycordBot = PycordBot(scheduler=scheduler)
|
||||
|
||||
bot.load_extension("cogs")
|
||||
|
||||
try:
|
||||
await bot.start(config_get("bot_token", "bot"))
|
||||
bot.run(config_get("bot_token", "bot"))
|
||||
except LoginFailure as exc:
|
||||
logger.error("Provided bot token is invalid: %s", exc)
|
||||
except KeyboardInterrupt:
|
||||
logger.warning("Forcefully shutting down with PID %s...", getpid())
|
||||
await bot.close()
|
||||
logger.info("KeyboardInterrupt received: Shutting down gracefully.")
|
||||
except Exception as exc:
|
||||
logger.error("An unexpected error has occurred: %s", exc, exc_info=exc)
|
||||
exit(1)
|
||||
finally:
|
||||
exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.create_task(main())
|
||||
main()
|
||||
|
@@ -23,8 +23,14 @@ db_client = AsyncClient(con_string)
|
||||
db: AsyncDatabase = db_client.get_database(name=db_config["name"])
|
||||
|
||||
col_users: AsyncCollection = db.get_collection("users")
|
||||
col_wallets: AsyncCollection = db.get_collection("wallets")
|
||||
col_guilds: AsyncCollection = db.get_collection("guilds")
|
||||
col_events: AsyncCollection = db.get_collection("events")
|
||||
col_stages: AsyncCollection = db.get_collection("stages")
|
||||
|
||||
# Update indexes
|
||||
db.dispatch.get_collection("users").create_index("id", unique=True)
|
||||
db.dispatch.get_collection("wallets").create_index(["owner_id", "guild_id"], unique=False)
|
||||
db.dispatch.get_collection("guilds").create_index("id", unique=True)
|
||||
db.dispatch.get_collection("events").create_index("id", unique=True)
|
||||
db.dispatch.get_collection("events").create_index("guild_id", unique=False)
|
||||
db.dispatch.get_collection("stages").create_index("id", unique=True)
|
||||
db.dispatch.get_collection("stages").create_index(["event_id", "guild_id"], unique=False)
|
||||
|
Reference in New Issue
Block a user