Minor improvements

This commit is contained in:
Profitroll 2023-08-17 15:01:35 +02:00
parent ac36e70b7f
commit b3805d66ef
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
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}})

View File

@ -22,6 +22,13 @@
"disabled_plugins": [],
"commands": {
"start": {
"scopes": [
{
"name": "BotCommandScopeDefault"
}
]
},
"remove_commands": {
"scopes": [
{
"name": "BotCommandScopeChat",

View File

@ -1,5 +1,23 @@
{
"metadata": {
"flag": "🇬🇧",
"name": "English",
"codes": [
"en",
"en-US",
"en-GB"
]
},
"commands": {
"start": "Start using the bot"
"start": "Start using the bot",
"language": "Change bot's language",
"remove_commands": "Unregister all commands"
},
"messages": {
"start": "Welcome! I'm your bot!",
"locale_choice": "Alright. Please choose the language using keyboard below."
},
"callback": {
"locale_set": "Your language now is: {locale}"
}
}

View File

@ -1,5 +1,22 @@
{
"metadata": {
"flag": "🇺🇦",
"name": "Українська",
"codes": [
"uk",
"uk-UA"
]
},
"commands": {
"start": "Почати користуватись ботом"
"start": "Почати користуватись ботом",
"language": "Змінити мову бота",
"remove_commands": "Видалити всі команди"
},
"messages": {
"start": "Привіт! Я твій бот!",
"locale_choice": "Гаразд. Будь ласка, оберіть мову за допомогою клавіатури нижче."
},
"callback": {
"locale_set": "Встановлено мову: {locale}"
}
}

View File

@ -1,10 +1,11 @@
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.types import CallbackQuery
from classes.pyroclient import PyroClient
@Client.on_callback_query(filters.regex("nothing")) # type: ignore
async def callback_nothing(app: PyroClient, clb: CallbackQuery):
await clb.answer(text="Nothing here...")
@PyroClient.on_callback_query(filters.regex("nothing")) # type: ignore
async def callback_nothing(app: PyroClient, callback: CallbackQuery):
await callback.answer(
text=app._("nothing", "callbacks", locale=callback.from_user.language_code)
)

View File

@ -1,12 +1,13 @@
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.types import Message
from classes.pyroclient import PyroClient
@Client.on_message(
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) # type: ignore
)
async def command_start(app: PyroClient, msg: Message):
await msg.reply_text("Welcome! I'm your bot!")
async def command_start(app: PyroClient, message: Message):
await message.reply_text(
app._("start", "messages", locale=message.from_user.language_code)
)

View File

@ -1,13 +1,12 @@
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.types import Message
from classes.pyroclient import PyroClient
@Client.on_message(
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["remove_commands"], prefixes=["/"]) # type: ignore
)
async def command_remove_commands(app: PyroClient, msg: Message):
await msg.reply_text("Okay.")
async def command_remove_commands(app: PyroClient, message: Message):
await message.reply_text("Okay.")
await app.remove_commands(command_sets=await app.collect_commands())

View File

@ -1,10 +1,9 @@
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.types import Message
from classes.pyroclient import PyroClient
@Client.on_message(filters.text & filters.private) # type: ignore
@PyroClient.on_message(filters.text & filters.private) # type: ignore
async def handler_echo(app: PyroClient, message: Message):
await message.reply(message.text[::-1])

View File

@ -1,4 +1,3 @@
from pyrogram.client import Client
from pyrogram.types import (
InlineQuery,
InlineQueryResultArticle,
@ -8,7 +7,7 @@ from pyrogram.types import (
from classes.pyroclient import PyroClient
@Client.on_inline_query() # type: ignore
@PyroClient.on_inline_query() # type: ignore
async def inline(app: PyroClient, inline_query: InlineQuery):
await inline_query.answer(
results=[

View File

@ -1,16 +1,20 @@
from pykeyboard import InlineButton, InlineKeyboard
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.types import CallbackQuery, Message
from classes.callbacks import CallbackLanguage
from classes.pyroclient import PyroClient
from classes.pyrouser import PyroUser
@Client.on_message(
@PyroClient.on_message(
~filters.scheduled & filters.private & filters.command(["language"], prefixes=["/"]) # type: ignore
)
async def command_language(app: PyroClient, message: Message):
user = await app.find_user(message.from_user)
user = await PyroUser.find(
message.from_user.id, locale=message.from_user.language_code
)
keyboard = InlineKeyboard(row_width=2)
buttons = []
@ -27,16 +31,18 @@ async def command_language(app: PyroClient, message: Message):
)
@Client.on_callback_query(filters.regex(r"language:[\s\S]*")) # type: ignore
@PyroClient.on_callback_query(filters.regex(r"language:[\s\S]*")) # type: ignore
async def callback_language(app: PyroClient, callback: CallbackQuery):
user = await app.find_user(callback.from_user)
language = str(callback.data).split(":")[1]
user = await PyroUser.find(
callback.from_user.id, locale=callback.from_user.language_code
)
parsed = CallbackLanguage.from_callback(callback)
await user.update_locale(language)
await user.update_locale(parsed.language)
await callback.answer(
app._("locale_set", "callbacks", locale=language).format(
locale=app._("name", "metadata", locale=language)
app._("locale_set", "callbacks", locale=parsed.language).format(
locale=app._("name", "metadata", locale=parsed.language)
),
show_alert=True,
)