Bot config and channel init
This commit is contained in:
parent
8ca93c1bab
commit
72edbe6599
7 changed files with 499 additions and 109 deletions
21
breadtube_bot/config.py
Normal file
21
breadtube_bot/config.py
Normal 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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue