lenaverse-bot/lenaverse_bot/core/config.py

64 lines
1.5 KiB
Python

import logging
import os
import tomllib
from typing import Self
import discord
from pydantic import BaseModel
_logger = logging.getLogger(__name__)
class PostTarget(BaseModel):
server: str
channel: str
thread: str
def get(self, client: discord.Client) -> discord.Thread:
"""
Zielkanal für Posts finden
"""
guilds = [guild for guild in client.guilds if guild.name == self.server]
_logger.debug(f"Guilds: {[guild.name for guild in guilds]}")
assert len(guilds) == 1
channels = [
channel
for channel in guilds[0].channels
if isinstance(channel, discord.TextChannel | discord.ForumChannel)
and channel.name == self.channel
]
_logger.debug(f"channels: {[channel.name for channel in channels]}")
assert len(channels) == 1
threads = [
thread for thread in channels[0].threads if thread.name == self.thread
]
_logger.debug(f"threads: {[thread.name for thread in threads]}")
assert len(threads) == 1
return threads[0]
class Post(BaseModel):
target: PostTarget
users: list[int]
class Config(BaseModel):
discord_token: str
post: Post
@classmethod
def get(cls) -> Self:
cfg_path = os.getenv(
key="CONFIG_PATH",
default="/usr/local/etc/lenaverse-bot/lenaverse-bot.toml",
)
with open(cfg_path, "rb") as cfg_file:
return cls.model_validate(tomllib.load(cfg_file))
CONFIG = Config.get()