This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
EmojiCaptchaBot/plugins/language.py

85 lines
2.6 KiB
Python
Raw Normal View History

2023-08-11 16:04:21 +03:00
import logging
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
2023-08-23 12:12:26 +03:00
from modules.utils import is_permitted
2023-08-11 16:04:21 +03:00
logger = logging.getLogger(__name__)
@PyroClient.on_message(
~filters.scheduled & filters.group & filters.command(["language"], prefixes=["/"]) # type: ignore
)
async def command_language(app: PyroClient, message: Message):
2023-08-17 17:37:42 +03:00
group = await app.find_group(message.chat.id)
2023-08-11 16:04:21 +03:00
locale = group.select_locale(app, message.from_user)
2023-08-23 12:12:26 +03:00
if not (await is_permitted(app, group, message=message)):
2023-08-11 16:04:21 +03:00
await message.reply_text(
app._("permission_denied", "messages", locale=locale), quote=True
)
return
keyboard = InlineKeyboard(row_width=2)
2023-08-23 12:12:26 +03:00
buttons = [
InlineButton(f"{data['flag']} {data['name']}", f"language:{language}")
for language, data in app.in_every_locale("metadata").items()
if data["selectable"]
]
2023-08-11 16:04:21 +03:00
buttons.append(
InlineButton(
f"🤖 {app._('locale_default', 'buttons', locale=locale)}", "language:default"
2023-08-11 16:04:21 +03:00
)
)
keyboard.add(*buttons)
await message.reply_text(
app._("locale_choice", "messages", locale=locale),
reply_markup=keyboard,
)
@Client.on_callback_query(filters.regex(r"language:[\s\S]*")) # type: ignore
2023-08-11 16:04:21 +03:00
async def callback_language(app: PyroClient, callback: CallbackQuery):
2023-08-17 17:37:42 +03:00
group = await app.find_group(callback.message.chat.id)
2023-08-11 16:04:21 +03:00
locale = group.select_locale(app, callback.message.from_user)
2023-08-23 12:12:26 +03:00
if not (await is_permitted(app, group, callback=callback)):
2023-08-11 16:04:21 +03:00
await callback.answer(
app._("wrong_user", "callbacks", locale=locale), show_alert=True
)
return
parsed = CallbackLanguage.from_callback(callback)
logger.info(
"Locale of group %s has been set to %s (auto: %s)",
group.id,
parsed.language,
group.locale_auto,
)
if parsed.language == "default":
await group.set_locale(None)
await callback.answer(
app._("locale_set", "callbacks", locale=app.default_locale).format(
locale=app._("locale_default", "buttons", locale=locale)
),
show_alert=True,
)
return
await group.set_locale(parsed.language)
await callback.answer(
app._("locale_set", "callbacks", locale=parsed.language).format(
locale=app._("name", "metadata", locale=parsed.language)
),
show_alert=True,
)