mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-10 18:30:23 +01:00
Use mongodb for twitch tokens
This commit is contained in:
72
src/core/config.py
Normal file
72
src/core/config.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import tomllib
|
||||
|
||||
from pydantic import BaseModel, field_validator
|
||||
from pydantic_settings import BaseSettings
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TwitchConfig(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
class NotificationsConfig(BaseModel):
|
||||
start_stream: str
|
||||
change_category: str | None = None
|
||||
|
||||
class GamesListConfig(BaseModel):
|
||||
channel_id: int
|
||||
message_id: int
|
||||
|
||||
class DiscordConfig(BaseModel):
|
||||
guild_id: int
|
||||
notifications_channel_id: int
|
||||
games_list: GamesListConfig | None = None
|
||||
roles: dict[str, int] | None = None
|
||||
|
||||
class TelegramConfig(BaseModel):
|
||||
notifications_channel_id: int
|
||||
|
||||
class IntegrationsConfig(BaseModel):
|
||||
discord: DiscordConfig | None = None
|
||||
telegram: TelegramConfig | None = None
|
||||
|
||||
class StreamerConfig(BaseModel):
|
||||
twitch: TwitchConfig
|
||||
notifications: NotificationsConfig
|
||||
integrations: IntegrationsConfig
|
||||
|
||||
|
||||
class Config(BaseSettings):
|
||||
DISCORD_BOT_TOKEN: str
|
||||
DISCORD_BOT_ID: str
|
||||
DISCORD_BOT_ACTIVITY: str
|
||||
|
||||
TELEGRAM_BOT_TOKEN: str
|
||||
|
||||
TWITCH_CLIENT_ID: str
|
||||
TWITCH_CLIENT_SECRET: str
|
||||
|
||||
TWITCH_ADMIN_USER_ID: str
|
||||
|
||||
TWITCH_CALLBACK_URL: str
|
||||
TWITCH_CALLBACK_PORT: int = 80
|
||||
|
||||
STREAMERS: list[StreamerConfig] = []
|
||||
|
||||
MONGODB_URI: str
|
||||
|
||||
SECRETS_FILE_PATH: str
|
||||
|
||||
@field_validator("STREAMERS", mode="before")
|
||||
def check_streamers(cls, value):
|
||||
config_dir = Path("/app/configs")
|
||||
streamers = []
|
||||
for toml_file in config_dir.glob("*.toml"):
|
||||
if toml_file.is_file():
|
||||
with open(toml_file, "rb") as f:
|
||||
streamer_config = tomllib.load(f)
|
||||
streamers.append(StreamerConfig(**streamer_config))
|
||||
return streamers if streamers else value
|
||||
|
||||
|
||||
config = Config() # type: ignore
|
||||
33
src/core/mongo.py
Normal file
33
src/core/mongo.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import contextlib
|
||||
|
||||
from mongojet import create_client
|
||||
|
||||
from core.config import config
|
||||
|
||||
|
||||
async def create_mongo_client():
|
||||
return await create_client(config.MONGODB_URI)
|
||||
|
||||
|
||||
class MongoDBSessionManager:
|
||||
def __init__(self):
|
||||
self.client = None
|
||||
|
||||
async def init(self):
|
||||
self.client = await create_mongo_client()
|
||||
|
||||
async def close(self):
|
||||
if self.client is not None:
|
||||
await self.client.close()
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def connect(self):
|
||||
if self.client is None:
|
||||
await self.init()
|
||||
|
||||
assert self.client is not None
|
||||
|
||||
yield self.client
|
||||
|
||||
|
||||
mongo_manager = MongoDBSessionManager()
|
||||
Reference in New Issue
Block a user