diff --git a/config_example.json b/config_example.json index 9f8ebe8..cfa538b 100644 --- a/config_example.json +++ b/config_example.json @@ -47,14 +47,16 @@ }, "commands_admin": { "reboot": "Restart the bot", - "message": "Send a message", + "message": "Send a message", + "label": "Set user's nickname", "warnings": "Check user's warnings", "application": "Check user's application", "applications": "Retrieve all applications as a JSON" }, "commands_group_admin": { "reboot": "Restart the bot", - "message": "Send a message", + "message": "Send a message", + "label": "Set user's nickname", "warnings": "Check user's warnings", "application": "Check user's application", "applications": "Retrieve all applications as a JSON" diff --git a/holochecker.py b/holochecker.py index b97befd..7cc93a8 100644 --- a/holochecker.py +++ b/holochecker.py @@ -13,6 +13,7 @@ pid = getpid() # Importing from modules.commands.application import * from modules.commands.applications import * +from modules.commands.label import * from modules.commands.message import * from modules.commands.reapply import * from modules.commands.reboot import * diff --git a/modules/commands/label.py b/modules/commands/label.py new file mode 100644 index 0000000..4ad5093 --- /dev/null +++ b/modules/commands/label.py @@ -0,0 +1,20 @@ +from app import app +from pyrogram import filters +from modules.utils import should_quote, find_user + +@app.on_message(~ filters.scheduled & filters.private & filters.command(["label"], prefixes=["/"])) +async def cmd_label(app, msg): + + if len(msg.command) < 3: + await msg.reply_text("Invalid syntax:\n`/label USER NICKNAME`") + return + + target = await find_user(app, msg.command[1]) + + if target is not None: + + nickname = " ".join(msg.command[2:]) + await msg.reply_text(f"Setting **{target.id}**'s label to **{nickname}**...", quote=should_quote(msg)) + + else: + await msg.reply_text(f"User not found") \ No newline at end of file diff --git a/modules/utils.py b/modules/utils.py index 685ad2c..81c1165 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -1,6 +1,8 @@ from typing import Any, Union from pyrogram.enums.chat_type import ChatType from pyrogram.types import User +from pyrogram.client import Client +from pyrogram.errors import bad_request_400 from ujson import JSONDecodeError as JSONDecodeError from ujson import loads, dumps @@ -148,4 +150,20 @@ def killProc(pid): p.kill() def should_quote(msg): - return True if msg.chat.type is not ChatType.PRIVATE else False \ No newline at end of file + return True if msg.chat.type is not ChatType.PRIVATE else False + +async def find_user(app: Client, query: Union[str, int]): + try: + result = await app.get_users(int(query)) + if result == [] or result == None: + raise TypeError + except (TypeError, ValueError): + try: + result = await app.get_users(query) + except bad_request_400.UsernameNotOccupied: + return None + except bad_request_400.UsernameInvalid: + return None + except bad_request_400.PeerIdInvalid: + return None + return result \ No newline at end of file