Add settings update
This commit is contained in:
@@ -3,10 +3,10 @@ from aiogram.types import (
|
|||||||
CallbackQuery
|
CallbackQuery
|
||||||
)
|
)
|
||||||
|
|
||||||
from bot.factories.open_setting import OpenSettingCallback
|
from bot.factories.open_setting import OpenSettingCallback, SettingChoiceCallback
|
||||||
|
|
||||||
from bot.keyboards.inline.setting import get_setting_kb
|
from bot.keyboards.inline.setting import get_setting_kb
|
||||||
from bot.modules.settings import settings_strings
|
from bot.modules.settings import settings_strings, UserSettings
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
@@ -25,3 +25,20 @@ async def on_settings(
|
|||||||
str(callback_query.from_user.id)
|
str(callback_query.from_user.id)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.callback_query(SettingChoiceCallback.filter())
|
||||||
|
async def on_change_setting(
|
||||||
|
callback_query: CallbackQuery,
|
||||||
|
callback_data: SettingChoiceCallback,
|
||||||
|
bot: Bot
|
||||||
|
):
|
||||||
|
UserSettings(callback_query.from_user.id)[callback_data.s_id] = callback_data.choice
|
||||||
|
await bot.edit_message_text(
|
||||||
|
inline_message_id=callback_query.inline_message_id,
|
||||||
|
text=settings_strings[callback_data.s_id].description,
|
||||||
|
reply_markup=get_setting_kb(
|
||||||
|
callback_data.s_id,
|
||||||
|
str(callback_query.from_user.id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ class OpenSettingCallback(CallbackData, prefix='setting'):
|
|||||||
|
|
||||||
|
|
||||||
class SettingChoiceCallback(CallbackData, prefix='s_choice'):
|
class SettingChoiceCallback(CallbackData, prefix='s_choice'):
|
||||||
|
s_id: str
|
||||||
choice: str
|
choice: str
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ def get_setting_kb(s_id: str, user_id: str) -> InlineKeyboardMarkup:
|
|||||||
'✅ ' if setting.value == choice else ''
|
'✅ ' if setting.value == choice else ''
|
||||||
) + setting.choices[choice],
|
) + setting.choices[choice],
|
||||||
callback_data=SettingChoiceCallback(
|
callback_data=SettingChoiceCallback(
|
||||||
|
s_id=s_id,
|
||||||
choice=choice,
|
choice=choice,
|
||||||
).pack()
|
).pack()
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -34,9 +34,12 @@ settings_strings: dict[str, Setting] = {
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class UserSettings:
|
class UserSettings:
|
||||||
user_id: str
|
user_id: str | int
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
|
if type(self.user_id) is int:
|
||||||
|
self.user_id = str(self.user_id)
|
||||||
|
|
||||||
if db.settings.get(self.user_id) is None:
|
if db.settings.get(self.user_id) is None:
|
||||||
db.settings[self.user_id] = dict(
|
db.settings[self.user_id] = dict(
|
||||||
(setting, list(settings_strings[setting].choices)[0]) for setting in
|
(setting, list(settings_strings[setting].choices)[0]) for setting in
|
||||||
@@ -51,4 +54,6 @@ class UserSettings:
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
db.settings[self.user_id][key] = value
|
h = db.settings[self.user_id]
|
||||||
|
h[key] = value
|
||||||
|
db.settings[self.user_id] = h
|
||||||
|
|||||||
Reference in New Issue
Block a user