Minor improvements

This commit is contained in:
2023-08-17 15:01:35 +02:00
parent ac36e70b7f
commit b3805d66ef
11 changed files with 108 additions and 29 deletions

15
classes/callbacks.py Normal file
View File

@@ -0,0 +1,15 @@
from dataclasses import dataclass
from pyrogram.types import CallbackQuery
@dataclass
class CallbackLanguage:
language: str
@classmethod
def from_callback(cls, callback: CallbackQuery):
action, language = str(callback.data).split(":")
if action.lower() != "language":
raise ValueError("Callback provided is not a language callback")
return cls(language)

View File

@@ -1,3 +1,4 @@
import logging
from dataclasses import dataclass
from typing import Union
@@ -5,6 +6,8 @@ from bson import ObjectId
from modules.database import col_users
logger = logging.getLogger(__name__)
@dataclass
class PyroUser:
@@ -16,10 +19,24 @@ class PyroUser:
id: int
locale: Union[str, None]
async def update_locale(self, locale: str) -> None:
@classmethod
async def find(cls, id: int, locale: Union[str, None] = None):
db_entry = await col_users.find_one({"id": id})
if db_entry is None:
inserted = await col_users.insert_one({"id": id, "locale": locale})
db_entry = await col_users.find_one({"_id": inserted.inserted_id})
if db_entry is None:
raise RuntimeError("Could not find inserted user entry.")
return cls(**db_entry)
async def update_locale(self, locale: Union[str, None]) -> None:
"""Change user's locale stored in the database
### Args:
* locale (`str`): New locale to be set
* locale (`Union[str, None]`): New locale to be set
"""
logger.debug("%s's locale has been set to %s", self.id, locale)
await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})