aboutsummaryrefslogtreecommitdiff
path: root/handlers/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'handlers/config.py')
-rw-r--r--handlers/config.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/handlers/config.py b/handlers/config.py
new file mode 100644
index 0000000..7eaf9eb
--- /dev/null
+++ b/handlers/config.py
@@ -0,0 +1,77 @@
1from ast import literal_eval
2from copy import deepcopy
3from logging import info
4from typing import Any, get_args
5
6from aiogram import types as t
7from pydantic import BaseModel, ValidationError
8
9from shared.database import Message
10from shared.instances import config, dp, session
11from shared.settings import Config
12from utils import filters as f
13
14
15@dp.message_handler(f.user.is_admin, commands=["void"])
16async def void_command(msg: t.Message) -> None:
17 if msg.get_args() == "Я знаю что делаю":
18 with session.begin() as s:
19 s.query(Message).filter(Message.chat_id == msg.chat.id).delete()
20 await msg.answer("Лоботомия проведена успешно")
21 else:
22 await msg.answer(
23 "Напишите <code>/void Я знаю что делаю</code>",
24 parse_mode=t.ParseMode.HTML,
25 )
26
27
28@dp.message_handler(f.message.is_chat, f.user.is_admin, commands=["config"])
29async def settings_command(msg: t.Message) -> None:
30 def get_fields(config: BaseModel, level: int = 1) -> str:
31 text = ""
32 for name in config.__fields__:
33 value = getattr(config, name)
34 if isinstance(value, BaseModel):
35 text += f"\n{' '*level*4}<code>{name}.</code>"
36 text += get_fields(value, level + 1)
37 else:
38 text += f"\n{' '*level*4}<code>{name}</code> = {value!r}"
39 return text
40
41 args = msg.get_args().split()
42 chat_config = deepcopy(config.get_config(msg.chat.id))
43 try:
44 if len(args) == 0:
45 text = f"<code>/config</code>{get_fields(chat_config)}\n\n"
46 await msg.reply(text, parse_mode=t.ParseMode.HTML)
47 else:
48 conf = chat_config
49 *path, field = args[0].split(".")
50 for f in path:
51 conf = getattr(conf, f)
52 if not isinstance(conf, BaseModel):
53 raise KeyError()
54
55 if len(args) == 2:
56 if isinstance(getattr(conf, field), BaseModel):
57 raise KeyError()
58 value = args[1]
59 setattr(conf, field, literal_eval(value))
60
61 config.set_config(msg.chat.id, Config.parse_obj(chat_config.dict()))
62 config.save("data/config.json")
63
64 await msg.reply(
65 f"<code>/config {args[0]}</code> = {getattr(conf, field)!r}",
66 parse_mode=t.ParseMode.HTML,
67 )
68 else:
69 field_info = conf.__fields__[field].field_info
70 await msg.reply(
71 f"<code>/config {args[0]}</code> = {getattr(conf, field)!r}\n{field_info.description}",
72 parse_mode=t.ParseMode.HTML,
73 )
74 except (ValidationError, ValueError, SyntaxError):
75 await msg.reply("Неверное значение")
76 except (KeyError, AttributeError):
77 await msg.reply("Параметр не найден")