Initial commit with code
This commit is contained in:
30
modules/database.py
Normal file
30
modules/database.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Module that provides all database collections"""
|
||||
|
||||
from typing import Any, Mapping
|
||||
|
||||
from async_pymongo import AsyncClient, AsyncCollection, AsyncDatabase
|
||||
from libbot.utils import config_get
|
||||
|
||||
db_config: Mapping[str, Any] = config_get("database")
|
||||
|
||||
if db_config["user"] is not None and db_config["password"] is not None:
|
||||
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
|
||||
db_config["user"],
|
||||
db_config["password"],
|
||||
db_config["host"],
|
||||
db_config["port"],
|
||||
db_config["name"],
|
||||
)
|
||||
else:
|
||||
con_string = "mongodb://{0}:{1}/{2}".format(db_config["host"], db_config["port"], db_config["name"])
|
||||
|
||||
# Async declarations
|
||||
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")
|
||||
|
||||
# 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)
|
35
modules/logging_utils.py
Normal file
35
modules/logging_utils.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import logging
|
||||
from logging import Logger
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
|
||||
from libbot.utils import config_get
|
||||
|
||||
|
||||
def get_logging_config() -> Dict[str, Any]:
|
||||
return {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"handlers": {
|
||||
"file": {
|
||||
"class": "logging.handlers.RotatingFileHandler",
|
||||
"filename": str(Path("logs/latest.log")),
|
||||
"maxBytes": 500000,
|
||||
"backupCount": 10,
|
||||
"formatter": "simple",
|
||||
},
|
||||
"console": {"class": "logging.StreamHandler", "formatter": "systemd"},
|
||||
},
|
||||
"formatters": {
|
||||
"simple": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"},
|
||||
"systemd": {"format": "%(name)s - %(levelname)s - %(message)s"},
|
||||
},
|
||||
"root": {
|
||||
"level": "DEBUG" if config_get("debug") else "INFO",
|
||||
"handlers": ["file", "console"],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def get_logger(name: str) -> Logger:
|
||||
return logging.getLogger(name)
|
22
modules/migrator.py
Normal file
22
modules/migrator.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from typing import Any, Mapping
|
||||
|
||||
from libbot.utils import config_get
|
||||
from mongodb_migrations.cli import MigrationManager
|
||||
from mongodb_migrations.config import Configuration
|
||||
|
||||
|
||||
def migrate_database() -> None:
|
||||
"""Apply migrations from folder `migrations/` to the database"""
|
||||
db_config: Mapping[str, Any] = config_get("database")
|
||||
|
||||
manager_config = Configuration(
|
||||
{
|
||||
"mongo_host": db_config["host"],
|
||||
"mongo_port": db_config["port"],
|
||||
"mongo_database": db_config["name"],
|
||||
"mongo_username": db_config["user"],
|
||||
"mongo_password": db_config["password"],
|
||||
}
|
||||
)
|
||||
manager = MigrationManager(manager_config)
|
||||
manager.run()
|
3
modules/scheduler.py
Normal file
3
modules/scheduler.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
|
||||
scheduler = AsyncIOScheduler()
|
6
modules/utils.py
Normal file
6
modules/utils.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def hex_to_int(hex_color: str) -> int:
|
||||
return int(hex_color.lstrip("#"), 16)
|
||||
|
||||
|
||||
def int_to_hex(integer_color: int) -> str:
|
||||
return "#" + format(integer_color, "06x")
|
Reference in New Issue
Block a user