From 44125f4b1916e8a8379b74cf4b053abbd15e4c2b Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 16 Jan 2023 10:51:37 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Update=20to=20v1.2.24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagermaid/__init__.py | 9 +--- pyromod/listen/listen.py | 84 +++++++++++++++++++++++++++++++++++++- pyromod/listen/temp_fix.py | 69 ------------------------------- 3 files changed, 84 insertions(+), 78 deletions(-) delete mode 100644 pyromod/listen/temp_fix.py diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 3bf5c96..ff4f3d7 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -2,7 +2,6 @@ import contextlib from typing import Callable, Awaitable, Set, Dict -import pyrogram.types from coloredlogs import ColoredFormatter from datetime import datetime, timezone from logging import getLogger, StreamHandler, CRITICAL, INFO, basicConfig, DEBUG, Formatter, FileHandler @@ -13,9 +12,7 @@ from pagermaid.scheduler import scheduler import pyromod.listen from pyrogram import Client -from pyromod.listen.temp_fix import temp_fix, read_chat_history - -pgm_version = "1.2.23" +pgm_version = "1.2.24" CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() @@ -66,10 +63,6 @@ bot = Client( proxy=Config.PROXY, app_version=f"PagerMaid {pgm_version}", ) -# temp fix topics group -setattr(pyrogram.types.Message, "old_parse", getattr(pyrogram.types.Message, "_parse")) -setattr(pyrogram.types.Message, "_parse", temp_fix) -pyrogram.Client.read_chat_history = read_chat_history bot.job = scheduler diff --git a/pyromod/listen/listen.py b/pyromod/listen/listen.py index a1db286..ac5a9af 100644 --- a/pyromod/listen/listen.py +++ b/pyromod/listen/listen.py @@ -21,6 +21,7 @@ along with pyromod. If not, see . import asyncio import contextlib import functools +from datetime import datetime from typing import Optional, List, Union import pyrogram @@ -94,6 +95,34 @@ class Client: def conversation(self, chat_id: Union[int, str], once_timeout: int = 60, filters=None): return Conversation(self, chat_id, once_timeout, filters) + @patchable + async def read_chat_history( + self: "pyrogram.Client", + chat_id: Union[int, str], + max_id: int = 0 + ) -> bool: + peer = await self.resolve_peer(chat_id) + if isinstance(peer, pyrogram.raw.types.InputPeerChannel): + with contextlib.suppress(pyrogram.errors.BadRequest): # noqa + topics: pyrogram.raw.types.messages.ForumTopics = await self.invoke( + pyrogram.raw.functions.channels.GetForumTopics( + channel=peer, # noqa + offset_date=0, + offset_id=0, + offset_topic=0, + limit=0 + ) + ) + for i in topics.topics: + await self.invoke( + pyrogram.raw.functions.messages.ReadDiscussion( + peer=peer, + msg_id=i.id, + read_max_id=i.read_inbox_max_id + i.unread_count, + ) + ) + return await self.oldread_chat_history(chat_id, max_id) # noqa + @patch(pyrogram.handlers.message_handler.MessageHandler) class MessageHandler: @@ -200,7 +229,7 @@ class Message(pyrogram.types.Message): message_ids=self.id, revoke=revoke ) - except Exception as e: # noqa + except Exception: # noqa return False @patchable @@ -318,6 +347,59 @@ class Message(pyrogram.types.Message): edit = edit_text + @patchable + @staticmethod + async def _parse( + client: "pyrogram.Client", + message: pyrogram.raw.base.Message, + users: dict, + chats: dict, + is_scheduled: bool = False, + replies: int = 1 + ): + parsed = await pyrogram.types.Message.old_parse(client, message, users, chats, is_scheduled, replies) # noqa + if isinstance(message, pyrogram.raw.types.Message) and message.reply_to \ + and hasattr(message.reply_to, "forum_topic") and message.reply_to.forum_topic \ + and not message.reply_to.reply_to_top_id: + parsed.reply_to_top_message_id = parsed.reply_to_message_id + parsed.reply_to_message_id = None + parsed.reply_to_message = None + # make message.text as message.caption + parsed.text = parsed.text or parsed.caption + return parsed + + @patchable + async def copy( + self, + chat_id: Union[int, str], + caption: str = None, + parse_mode: Optional["pyrogram.enums.ParseMode"] = None, + caption_entities: List["pyrogram.types.MessageEntity"] = None, + disable_notification: bool = None, + reply_to_message_id: int = None, + schedule_date: datetime = None, + protect_content: bool = None, + reply_markup: Union[ + "pyrogram.types.InlineKeyboardMarkup", + "pyrogram.types.ReplyKeyboardMarkup", + "pyrogram.types.ReplyKeyboardRemove", + "pyrogram.types.ForceReply" + ] = object + ) -> Union["pyrogram.types.Message", List["pyrogram.types.Message"]]: + if self.media: + self.text = None + return await self.oldcopy( + chat_id, + caption, + parse_mode, + caption_entities, + disable_notification, + reply_to_message_id, + schedule_date, + protect_content, + reply_markup, + ) # noqa + @patch(pyrogram.dispatcher.Dispatcher) # noqa class Dispatcher(pyrogram.dispatcher.Dispatcher): # noqa diff --git a/pyromod/listen/temp_fix.py b/pyromod/listen/temp_fix.py deleted file mode 100644 index 3266e43..0000000 --- a/pyromod/listen/temp_fix.py +++ /dev/null @@ -1,69 +0,0 @@ -import contextlib -from typing import Union - -import pyrogram -from pyrogram.errors import BadRequest - - -async def temp_fix( - client: "pyrogram.Client", - message: pyrogram.raw.base.Message, - users: dict, - chats: dict, - is_scheduled: bool = False, - replies: int = 1 -): - parsed = await pyrogram.types.Message.old_parse(client, message, users, chats, is_scheduled, replies) # noqa - if isinstance(message, pyrogram.raw.types.Message) and message.reply_to \ - and hasattr(message.reply_to, "forum_topic") and message.reply_to.forum_topic \ - and not message.reply_to.reply_to_top_id: - parsed.reply_to_top_message_id = parsed.reply_to_message_id - parsed.reply_to_message_id = None - parsed.reply_to_message = None - # make message.text as message.caption - parsed.text = parsed.text or parsed.caption - return parsed - - -async def read_chat_history( - self: "pyrogram.Client", - chat_id: Union[int, str], - max_id: int = 0 -) -> bool: - - peer = await self.resolve_peer(chat_id) - - if isinstance(peer, pyrogram.raw.types.InputPeerChannel): - q = pyrogram.raw.functions.channels.ReadHistory( - channel=peer, - max_id=max_id - ) - else: - q = pyrogram.raw.functions.messages.ReadHistory( - peer=peer, - max_id=max_id - ) - - await self.invoke(q) - - if isinstance(peer, pyrogram.raw.types.InputPeerChannel): - with contextlib.suppress(BadRequest): - topics: pyrogram.raw.types.messages.ForumTopics = await self.invoke( - pyrogram.raw.functions.channels.GetForumTopics( - channel=peer, - offset_date=0, - offset_id=0, - offset_topic=0, - limit=0 - ) - ) - for i in topics.topics: - await self.invoke( - pyrogram.raw.functions.messages.ReadDiscussion( - peer=peer, - msg_id=i.id, - read_max_id=i.read_inbox_max_id + i.unread_count, - ) - ) - - return True