Added database and /language
This commit is contained in:
parent
b1f7e649b9
commit
95dbd9cec8
40
classes/pyroclient.py
Normal file
40
classes/pyroclient.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from libbot.pyrogram.classes import PyroClient
|
||||||
|
from pyrogram.types import User
|
||||||
|
|
||||||
|
from classes.pyrouser import PyroUser
|
||||||
|
from modules.database import col_users
|
||||||
|
|
||||||
|
|
||||||
|
class PyroClient(PyroClient):
|
||||||
|
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`: PyroUser object
|
||||||
|
"""
|
||||||
|
if (
|
||||||
|
col_users.find_one({"id": user.id if isinstance(user, User) else user})
|
||||||
|
is None
|
||||||
|
):
|
||||||
|
col_users.insert_one(
|
||||||
|
{
|
||||||
|
"id": user.id if isinstance(user, User) else user,
|
||||||
|
"locale": user.language_code if isinstance(user, User) else None,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
db_record = col_users.find_one(
|
||||||
|
{"id": user.id if isinstance(user, User) else user}
|
||||||
|
)
|
||||||
|
|
||||||
|
if db_record is None:
|
||||||
|
raise TypeError(
|
||||||
|
f"User with ID {user.id if isinstance(user, User) else user} was not found in the database"
|
||||||
|
)
|
||||||
|
|
||||||
|
return PyroUser(**db_record)
|
23
classes/pyrouser.py
Normal file
23
classes/pyrouser.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from attrs import define
|
||||||
|
from bson import ObjectId
|
||||||
|
|
||||||
|
from modules.database import col_users
|
||||||
|
|
||||||
|
|
||||||
|
@define
|
||||||
|
class PyroUser:
|
||||||
|
"""Dataclass of DB entry of a user"""
|
||||||
|
|
||||||
|
_id: ObjectId
|
||||||
|
id: int
|
||||||
|
locale: Union[str, None]
|
||||||
|
|
||||||
|
async def update_locale(self, locale: str) -> None:
|
||||||
|
"""Change user's locale stored in the database
|
||||||
|
|
||||||
|
### Args:
|
||||||
|
* locale (`str`): New locale to be set
|
||||||
|
"""
|
||||||
|
col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
|
@ -9,6 +9,13 @@
|
|||||||
"max_concurrent_transmissions": 1,
|
"max_concurrent_transmissions": 1,
|
||||||
"scoped_commands": true
|
"scoped_commands": true
|
||||||
},
|
},
|
||||||
|
"database": {
|
||||||
|
"user": null,
|
||||||
|
"password": null,
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 27017,
|
||||||
|
"name": "pyrobot"
|
||||||
|
},
|
||||||
"reports": {
|
"reports": {
|
||||||
"chat_id": "owner"
|
"chat_id": "owner"
|
||||||
},
|
},
|
||||||
|
32
modules/database.py
Normal file
32
modules/database.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
"""Module that provides all database columns"""
|
||||||
|
|
||||||
|
from pymongo import MongoClient
|
||||||
|
from ujson import loads
|
||||||
|
|
||||||
|
with open("config.json", "r", encoding="utf-8") as f:
|
||||||
|
db_config = loads(f.read())["database"]
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
if db_config["user"] is not None and db_config["password"] is not None:
|
||||||
|
con_string = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
|
||||||
|
db_config["user"],
|
||||||
|
db_config["password"],
|
||||||
|
db_config["host"],
|
||||||
|
db_config["port"],
|
||||||
|
db_config["name"],
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
con_string = "mongodb://{0}:{1}/{2}".format(
|
||||||
|
db_config["host"], db_config["port"], db_config["name"]
|
||||||
|
)
|
||||||
|
|
||||||
|
db_client = MongoClient(con_string)
|
||||||
|
db = db_client.get_database(name=db_config["name"])
|
||||||
|
|
||||||
|
collections = db.list_collection_names()
|
||||||
|
|
||||||
|
for collection in ["users"]:
|
||||||
|
if collection not in collections:
|
||||||
|
db.create_collection(collection)
|
||||||
|
|
||||||
|
col_users = db.get_collection("users")
|
@ -2,7 +2,7 @@ from pyrogram import filters
|
|||||||
from pyrogram.client import Client
|
from pyrogram.client import Client
|
||||||
from pyrogram.types import CallbackQuery
|
from pyrogram.types import CallbackQuery
|
||||||
|
|
||||||
from libbot.pyrogram.classes import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_callback_query(filters.regex("nothing")) # type: ignore
|
@Client.on_callback_query(filters.regex("nothing")) # type: ignore
|
||||||
|
@ -2,7 +2,7 @@ from pyrogram import filters
|
|||||||
from pyrogram.client import Client
|
from pyrogram.client import Client
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
|
|
||||||
from libbot.pyrogram.classes import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(
|
@Client.on_message(
|
||||||
|
@ -2,7 +2,7 @@ from pyrogram import filters
|
|||||||
from pyrogram.client import Client
|
from pyrogram.client import Client
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
|
|
||||||
from libbot.pyrogram.classes import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(filters.text & filters.private) # type: ignore
|
@Client.on_message(filters.text & filters.private) # type: ignore
|
||||||
|
@ -5,7 +5,7 @@ from pyrogram.types import (
|
|||||||
InputTextMessageContent,
|
InputTextMessageContent,
|
||||||
)
|
)
|
||||||
|
|
||||||
from libbot.pyrogram.classes import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_inline_query() # type: ignore
|
@Client.on_inline_query() # type: ignore
|
||||||
|
42
plugins/language.py
Normal file
42
plugins/language.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from pykeyboard import InlineButton, InlineKeyboard
|
||||||
|
from pyrogram import filters
|
||||||
|
from pyrogram.client import Client
|
||||||
|
from pyrogram.types import CallbackQuery, Message
|
||||||
|
|
||||||
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
|
@Client.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)
|
||||||
|
keyboard = InlineKeyboard(row_width=2)
|
||||||
|
buttons = []
|
||||||
|
|
||||||
|
for locale, data in app.in_every_locale("metadata").items():
|
||||||
|
buttons.append(
|
||||||
|
InlineButton(f"{data['flag']} {data['name']}", f"language:{locale}")
|
||||||
|
)
|
||||||
|
|
||||||
|
keyboard.add(*buttons)
|
||||||
|
|
||||||
|
await message.reply_text(
|
||||||
|
app._("locale_choice", "messages", locale=user.locale),
|
||||||
|
reply_markup=keyboard,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@Client.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]
|
||||||
|
|
||||||
|
await user.update_locale(language)
|
||||||
|
|
||||||
|
await callback.answer(
|
||||||
|
app._("locale_set", "callbacks", locale=language).format(
|
||||||
|
locale=app._("name", "metadata", locale=language)
|
||||||
|
),
|
||||||
|
show_alert=True,
|
||||||
|
)
|
@ -2,7 +2,7 @@ from pyrogram import filters
|
|||||||
from pyrogram.client import Client
|
from pyrogram.client import Client
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
|
|
||||||
from libbot.pyrogram.classes import PyroClient
|
from classes.pyroclient import PyroClient
|
||||||
|
|
||||||
|
|
||||||
@Client.on_message(
|
@Client.on_message(
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
aiofiles~=23.1.0
|
aiofiles~=23.1.0
|
||||||
apscheduler~=3.10.1
|
apscheduler~=3.10.1
|
||||||
|
attrs~=23.1.0
|
||||||
black~=23.7.0
|
black~=23.7.0
|
||||||
convopyro==0.5
|
convopyro==0.5
|
||||||
|
pykeyboard==0.1.5
|
||||||
|
pymongo==4.4.1
|
||||||
pyrogram==2.0.106
|
pyrogram==2.0.106
|
||||||
tgcrypto==1.2.5
|
tgcrypto==1.2.5
|
||||||
ujson==5.8.0
|
ujson==5.8.0
|
||||||
|
Reference in New Issue
Block a user