1
0
Fork 0
mirror of https://code.lenaisten.de/Lenaisten/lenaverse-bot.git synced 2024-11-22 23:13:01 +00:00
lenaverse-bot/lenaverse_bot/core/config.py

65 lines
1.5 KiB
Python
Raw Normal View History

2023-11-19 18:01:06 +00:00
import logging
2023-11-19 15:41:19 +00:00
import os
import tomllib
from typing import Self
2023-11-19 16:32:12 +00:00
import discord
2023-11-19 15:41:19 +00:00
from pydantic import BaseModel
2023-11-19 18:01:06 +00:00
_logger = logging.getLogger(__name__)
2023-11-19 15:41:19 +00:00
2023-11-19 16:32:12 +00:00
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]
2023-11-19 18:01:06 +00:00
_logger.debug(f"Guilds: {[guild.name for guild in guilds]}")
2023-11-19 16:32:12 +00:00
assert len(guilds) == 1
channels = [
channel
for channel in guilds[0].channels
if isinstance(channel, discord.TextChannel | discord.ForumChannel)
and channel.name == self.channel
]
2023-11-19 18:01:06 +00:00
_logger.debug(f"channels: {[channel.name for channel in channels]}")
2023-11-19 16:32:12 +00:00
assert len(channels) == 1
threads = [
thread for thread in channels[0].threads if thread.name == self.thread
]
2023-11-19 18:01:06 +00:00
_logger.debug(f"threads: {[thread.name for thread in threads]}")
2023-11-19 16:32:12 +00:00
assert len(threads) == 1
return threads[0]
class Post(BaseModel):
target: PostTarget
users: list[int]
2023-11-19 15:41:19 +00:00
class Config(BaseModel):
discord_token: str
2023-11-19 16:32:12 +00:00
post: Post
2023-11-19 15:41:19 +00:00
@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()