Minor improvements
This commit is contained in:
parent
ac36e70b7f
commit
b3805d66ef
15
classes/callbacks.py
Normal file
15
classes/callbacks.py
Normal 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)
|
@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
@ -5,6 +6,8 @@ from bson import ObjectId
|
|||||||
|
|
||||||
from modules.database import col_users
|
from modules.database import col_users
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PyroUser:
|
class PyroUser:
|
||||||
@ -16,10 +19,24 @@ class PyroUser:
|
|||||||
id: int
|
id: int
|
||||||
locale: Union[str, None]
|
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
|
"""Change user's locale stored in the database
|
||||||
|
|
||||||
### Args:
|
### 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}})
|
await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
|
||||||
|
@ -22,6 +22,13 @@
|
|||||||
"disabled_plugins": [],
|
"disabled_plugins": [],
|
||||||
"commands": {
|
"commands": {
|
||||||
"start": {
|
"start": {
|
||||||
|
"scopes": [
|
||||||
|
{
|
||||||
|
"name": "BotCommandScopeDefault"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"remove_commands": {
|
||||||
"scopes": [
|
"scopes": [
|
||||||
{
|
{
|
||||||
"name": "BotCommandScopeChat",
|
"name": "BotCommandScopeChat",
|
||||||
|
@ -1,5 +1,23 @@
|
|||||||
{
|
{
|
||||||
|
"metadata": {
|
||||||
|
"flag": "🇬🇧",
|
||||||
|
"name": "English",
|
||||||
|
"codes": [
|
||||||
|
"en",
|
||||||
|
"en-US",
|
||||||
|
"en-GB"
|
||||||
|
]
|
||||||
|
},
|
||||||
"commands": {
|
"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}"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,22 @@
|
|||||||
{
|
{
|
||||||
|
"metadata": {
|
||||||
|
"flag": "🇺🇦",
|
||||||
|
"name": "Українська",
|
||||||
|
"codes": [
|
||||||
|
"uk",
|
||||||
|
"uk-UA"
|
||||||
|
]
|
||||||
|
},
|
||||||
"commands": {
|
"commands": {
|
||||||
"start": "Почати користуватись ботом"
|
"start": "Почати користуватись ботом",
|
||||||
|
"language": "Змінити мову бота",
|
||||||
|
"remove_commands": "Видалити всі команди"
|
||||||
|
},
|
||||||
|
"messages": {
|
||||||
|
"start": "Привіт! Я твій бот!",
|
||||||
|
"locale_choice": "Гаразд. Будь ласка, оберіть мову за допомогою клавіатури нижче."
|
||||||
|
},
|
||||||
|
"callback": {
|
||||||
|
"locale_set": "Встановлено мову: {locale}"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,11 @@
|
|||||||
from pyrogram import filters
|
from pyrogram import filters
|
||||||
from pyrogram.client import Client
|
|
||||||
from pyrogram.types import CallbackQuery
|
from pyrogram.types import CallbackQuery
|
||||||
|
|
||||||
from classes.pyroclient import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_callback_query(filters.regex("nothing")) # type: ignore
|
@PyroClient.on_callback_query(filters.regex("nothing")) # type: ignore
|
||||||
async def callback_nothing(app: PyroClient, clb: CallbackQuery):
|
async def callback_nothing(app: PyroClient, callback: CallbackQuery):
|
||||||
await clb.answer(text="Nothing here...")
|
await callback.answer(
|
||||||
|
text=app._("nothing", "callbacks", locale=callback.from_user.language_code)
|
||||||
|
)
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
from pyrogram import filters
|
from pyrogram import filters
|
||||||
from pyrogram.client import Client
|
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
|
|
||||||
from classes.pyroclient import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(
|
@PyroClient.on_message(
|
||||||
~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) # type: ignore
|
~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) # type: ignore
|
||||||
)
|
)
|
||||||
async def command_start(app: PyroClient, msg: Message):
|
async def command_start(app: PyroClient, message: Message):
|
||||||
await msg.reply_text("Welcome! I'm your bot!")
|
await message.reply_text(
|
||||||
|
app._("start", "messages", locale=message.from_user.language_code)
|
||||||
|
)
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
from pyrogram import filters
|
from pyrogram import filters
|
||||||
from pyrogram.client import Client
|
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
|
|
||||||
from classes.pyroclient import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(
|
@PyroClient.on_message(
|
||||||
~filters.scheduled & filters.private & filters.command(["remove_commands"], prefixes=["/"]) # type: ignore
|
~filters.scheduled & filters.private & filters.command(["remove_commands"], prefixes=["/"]) # type: ignore
|
||||||
)
|
)
|
||||||
async def command_remove_commands(app: PyroClient, msg: Message):
|
async def command_remove_commands(app: PyroClient, message: Message):
|
||||||
await msg.reply_text("Okay.")
|
await message.reply_text("Okay.")
|
||||||
await app.remove_commands(command_sets=await app.collect_commands())
|
await app.remove_commands(command_sets=await app.collect_commands())
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
from pyrogram import filters
|
from pyrogram import filters
|
||||||
from pyrogram.client import Client
|
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
|
|
||||||
from classes.pyroclient import PyroClient
|
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):
|
async def handler_echo(app: PyroClient, message: Message):
|
||||||
await message.reply(message.text[::-1])
|
await message.reply(message.text[::-1])
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from pyrogram.client import Client
|
|
||||||
from pyrogram.types import (
|
from pyrogram.types import (
|
||||||
InlineQuery,
|
InlineQuery,
|
||||||
InlineQueryResultArticle,
|
InlineQueryResultArticle,
|
||||||
@ -8,7 +7,7 @@ from pyrogram.types import (
|
|||||||
from classes.pyroclient import PyroClient
|
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):
|
async def inline(app: PyroClient, inline_query: InlineQuery):
|
||||||
await inline_query.answer(
|
await inline_query.answer(
|
||||||
results=[
|
results=[
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
from pykeyboard import InlineButton, InlineKeyboard
|
from pykeyboard import InlineButton, InlineKeyboard
|
||||||
from pyrogram import filters
|
from pyrogram import filters
|
||||||
from pyrogram.client import Client
|
|
||||||
from pyrogram.types import CallbackQuery, Message
|
from pyrogram.types import CallbackQuery, Message
|
||||||
|
|
||||||
|
from classes.callbacks import CallbackLanguage
|
||||||
from classes.pyroclient import PyroClient
|
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
|
~filters.scheduled & filters.private & filters.command(["language"], prefixes=["/"]) # type: ignore
|
||||||
)
|
)
|
||||||
async def command_language(app: PyroClient, message: Message):
|
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)
|
keyboard = InlineKeyboard(row_width=2)
|
||||||
buttons = []
|
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):
|
async def callback_language(app: PyroClient, callback: CallbackQuery):
|
||||||
user = await app.find_user(callback.from_user)
|
user = await PyroUser.find(
|
||||||
language = str(callback.data).split(":")[1]
|
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(
|
await callback.answer(
|
||||||
app._("locale_set", "callbacks", locale=language).format(
|
app._("locale_set", "callbacks", locale=parsed.language).format(
|
||||||
locale=app._("name", "metadata", locale=language)
|
locale=app._("name", "metadata", locale=parsed.language)
|
||||||
),
|
),
|
||||||
show_alert=True,
|
show_alert=True,
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user