Big and tasty update to v2.0
This commit is contained in:
28
classes/callbacks.py
Normal file
28
classes/callbacks.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
from pyrogram.types import CallbackQuery
|
||||
|
||||
|
||||
@dataclass
|
||||
class CallbackLanguage:
|
||||
language: str
|
||||
|
||||
@classmethod
|
||||
def from_callback(cls, callback: CallbackQuery):
|
||||
"""Parse callback query and extract language data from it.
|
||||
|
||||
### Args:
|
||||
* callback (`CallbackQuery`): Callback query got from user interaction.
|
||||
|
||||
### Raises:
|
||||
* `ValueError`: Raised when callback provided is not a language one.
|
||||
|
||||
### Returns:
|
||||
* `CallbackLanguage`: Parsed callback query.
|
||||
"""
|
||||
action, language = str(callback.data).split(":")
|
||||
|
||||
if action.lower() != "language":
|
||||
raise ValueError("Callback provided is not a language callback")
|
||||
|
||||
return cls(language)
|
24
classes/pyroclient.py
Normal file
24
classes/pyroclient.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from typing import Union
|
||||
|
||||
from libbot.pyrogram.classes import PyroClient as LibPyroClient
|
||||
from pyrogram.types import User
|
||||
|
||||
from classes.pyrouser import PyroUser
|
||||
|
||||
|
||||
class PyroClient(LibPyroClient):
|
||||
async def find_user(self, user: Union[int, User]) -> PyroUser:
|
||||
"""Find User by it's ID or User object.
|
||||
|
||||
### Args:
|
||||
* user (`Union[int, User]`): ID or User object to extract ID from.
|
||||
|
||||
### Returns:
|
||||
* `PyroUser`: User in database representation.
|
||||
"""
|
||||
|
||||
return (
|
||||
await PyroUser.find(user)
|
||||
if isinstance(user, int)
|
||||
else await PyroUser.find(user.id, locale=user.language_code)
|
||||
)
|
77
classes/pyrouser.py
Normal file
77
classes/pyrouser.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
from modules.database import cursor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class PyroUser:
|
||||
"""Dataclass of DB entry of a user"""
|
||||
|
||||
__slots__ = ("id", "card", "locale")
|
||||
|
||||
id: int
|
||||
card: Union[str, None]
|
||||
locale: Union[str, None]
|
||||
|
||||
@classmethod
|
||||
async def find(
|
||||
cls, id: int, card: Union[str, None] = None, locale: Union[str, None] = None
|
||||
):
|
||||
"""Find user in database and create new record if user does not exist.
|
||||
|
||||
### Args:
|
||||
* id (`int`): User's Telegram ID
|
||||
* card (`Union[str, None]`, *optional*): User's card number. Defaults to `None`.
|
||||
* locale (`Union[str, None]`, *optional*): User's locale. Defaults to `None`.
|
||||
|
||||
### Raises:
|
||||
* `RuntimeError`: Raised when user entry after insertion could not be found.
|
||||
|
||||
### Returns:
|
||||
* `PyroUser`: User with its database data.
|
||||
"""
|
||||
db_entry = cursor.execute(
|
||||
"SELECT id, card, locale FROM users WHERE id = ?", (id,)
|
||||
).fetchone()
|
||||
|
||||
if db_entry is None:
|
||||
cursor.execute("INSERT INTO users VALUES (?, ?, ?)", (id, card, locale))
|
||||
cursor.connection.commit()
|
||||
db_entry = cursor.execute(
|
||||
"SELECT id, card, locale FROM users WHERE id = ?", (id,)
|
||||
).fetchone()
|
||||
|
||||
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 (`Union[str, None]`): New locale to be set.
|
||||
"""
|
||||
logger.debug("%s's locale has been set to %s", self.id, locale)
|
||||
cursor.execute(
|
||||
"UPDATE users SET locale = ? WHERE id = ?",
|
||||
(locale, self.id),
|
||||
)
|
||||
cursor.connection.commit()
|
||||
|
||||
async def update_card(self, card: Union[str, None]) -> None:
|
||||
"""Change user's card stored in the database.
|
||||
|
||||
### Args:
|
||||
* card (`Union[str, None]`): New card to be set.
|
||||
"""
|
||||
logger.debug("%s's card has been set to %s", self.id, card)
|
||||
cursor.execute(
|
||||
"UPDATE users SET card = ? WHERE id = ?",
|
||||
(card, self.id),
|
||||
)
|
||||
cursor.connection.commit()
|
Reference in New Issue
Block a user