Bot config and channel init

This commit is contained in:
BreadTube 2025-09-23 04:50:23 +09:00 committed by Corentin
commit 72edbe6599
7 changed files with 499 additions and 109 deletions

21
breadtube_bot/config.py Normal file
View file

@ -0,0 +1,21 @@
from dataclasses import asdict, dataclass
@dataclass
class Config:
bot_channel: str = 'breadtube-bot'
bot_role: str = 'BreadTube'
bot_channel_init_retries: int = 3
def to_str(self) -> str:
return '\n'.join(['config', *[f'{k}={v}' for k, v in asdict(self).items()]])
def from_str(self, text: str):
lines = text.strip().splitlines()
if not lines:
raise RuntimeError('Config cannot load: empty input')
if lines[0] != 'config':
raise RuntimeError('Config cannot load: first line is not "config"')
for line in lines[1:]:
key, value = line.split('=', maxsplit=1)
setattr(self, key, self.__annotations__[key](value))