From 8bdbe01da2a77fece4e935b7d06aad94e3e4504b Mon Sep 17 00:00:00 2001 From: Hanssen Date: Fri, 15 Dec 2023 04:35:28 +0800 Subject: [PATCH 1/2] feat: show users with deep link --- BotHandler.py | 31 +++++++++++++++++++------------ UsersHandler.py | 1 + 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/BotHandler.py b/BotHandler.py index 3dc6e767..ed904a55 100644 --- a/BotHandler.py +++ b/BotHandler.py @@ -1262,7 +1262,7 @@ async def query_callback( reply_message_id = int(data_splitted[2]) # Get user - user = self.users_handler.get_user_by_id(telegram_chat_id) + user = await self._user_check_get(update, context) # Exit if banned if user["banned"]: @@ -1682,21 +1682,29 @@ async def bot_command_users( + " " ) + user_id = user_info["user_id"] + is_private = ( + (user_info["user_type"] == "private") + if "user_type" in user_info + else (user_id > 0) + ) # User ID, name, total requests - message += "{0} ({1}) - {2}\n".format( - user_info["user_id"], + message += ( + "{0} ([{1}](tg://user?id={0})) - {2}\n" + if is_private + else "{0} ({1}) - {2}\n" + ).format( + user_id, user_info["user_name"], user_info["requests_total"], ) - # Parse as monospace message = ( self.messages[lang]["users_admin"] .format(message) .replace("\\t", "\t") .replace("\\n", "\n") ) - message = "```\n" + message + "\n```" # Send list of users as markdown await send_reply( @@ -2668,23 +2676,22 @@ async def _user_check_get( self, update: Update, context: ContextTypes.DEFAULT_TYPE ) -> dict: """ - Gets (or creates) user based on update.effective_chat.id and update.message.from_user.full_name + Gets (or creates) user based on update.effective_chat.id and checks if they are banned or not :param update: :param context: :return: user as dictionary """ # Get user (or create a new one) - telegram_user_name = ( - update.message.from_user.full_name if update.message is not None else None - ) telegram_chat_id = update.effective_chat.id user = self.users_handler.get_user_by_id(telegram_chat_id) # Update user name - if telegram_user_name is not None: - user["user_name"] = str(telegram_user_name) - self.users_handler.save_user(user) + if update.effective_chat.effective_name is not None: + user["user_name"] = str(update.effective_chat.effective_name) + + user["user_type"] = update.effective_chat.type + self.users_handler.save_user(user) # Send banned info if user["banned"]: diff --git a/UsersHandler.py b/UsersHandler.py index 7cca319f..4fec5395 100644 --- a/UsersHandler.py +++ b/UsersHandler.py @@ -116,6 +116,7 @@ def _create_user(self, user_id: int) -> dict: user = { "user_id": user_id, "user_name": DEFAULT_USER_NAME, + "user_type": "", "admin": True if user_id in self.config["telegram"]["admin_ids"] else False, "banned": self.config["telegram"]["ban_by_default"], "ban_reason": self.messages[0]["ban_reason_default"].replace("\\n", "\n"), From d73ec6c468c2818e3b7e43185a2ffc41a1138fe3 Mon Sep 17 00:00:00 2001 From: Hanssen Date: Fri, 15 Dec 2023 04:47:35 +0800 Subject: [PATCH 2/2] fix: admins won't be banned by default --- UsersHandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UsersHandler.py b/UsersHandler.py index 4fec5395..74d56a6e 100644 --- a/UsersHandler.py +++ b/UsersHandler.py @@ -118,7 +118,7 @@ def _create_user(self, user_id: int) -> dict: "user_name": DEFAULT_USER_NAME, "user_type": "", "admin": True if user_id in self.config["telegram"]["admin_ids"] else False, - "banned": self.config["telegram"]["ban_by_default"], + "banned": False if user_id in self.config["telegram"]["admin_ids"] else self.config["telegram"]["ban_by_default"], "ban_reason": self.messages[0]["ban_reason_default"].replace("\\n", "\n"), "module": self.config["modules"]["default_module"], "requests_total": 0,