From d215e293f8df1b62ce4de61bb9757d4de8cad1ea Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 15 Nov 2022 10:52:29 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Update=20to=20v1.2.15?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support topic group --- pagermaid/__init__.py | 8 +++++++- pagermaid/modules/account.py | 4 ++-- pagermaid/modules/message.py | 40 +++++++++++++++++++++++------------- pagermaid/modules/prune.py | 1 + pagermaid/modules/system.py | 12 +++++++---- pyromod/listen/temp_fix.py | 18 ++++++++++++++++ 6 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 pyromod/listen/temp_fix.py diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index fe75229..3997fee 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -2,6 +2,7 @@ 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 @@ -12,7 +13,9 @@ from pagermaid.scheduler import scheduler import pyromod.listen from pyrogram import Client -pgm_version = "1.2.14" +from pyromod.listen.temp_fix import temp_fix + +pgm_version = "1.2.15" CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() @@ -60,6 +63,9 @@ bot = Client("pagermaid", api_hash=Config.API_HASH, ipv6=Config.IPV6, proxy=Config.PROXY) +# temp fix topics group +setattr(pyrogram.types.Message, "old_parse", getattr(pyrogram.types.Message, "_parse")) +setattr(pyrogram.types.Message, "_parse", temp_fix) bot.job = scheduler diff --git a/pagermaid/modules/account.py b/pagermaid/modules/account.py index 3ed9970..cf495eb 100644 --- a/pagermaid/modules/account.py +++ b/pagermaid/modules/account.py @@ -69,13 +69,13 @@ async def profile(client: Client, message: Message): f"{lang('profile_type')}: {user_type} \n" \ f"[{first_name}](tg://user?id={user.id})" photo = await client.download_media(user.photo.big_file_id) - reply_to = message.reply_to_message + reply_to = message.reply_to_message.id if message.reply_to_message else message.reply_to_top_message_id try: await client.send_photo( message.chat.id, photo, caption=caption, - reply_to_message_id=reply_to.id if reply_to else None + reply_to_message_id=reply_to ) await message.delete() return remove(photo) diff --git a/pagermaid/modules/message.py b/pagermaid/modules/message.py index 134cd96..7463e4c 100644 --- a/pagermaid/modules/message.py +++ b/pagermaid/modules/message.py @@ -1,11 +1,13 @@ """ Pagermaid message plugin. """ - +from pyrogram.enums import ChatType from pyrogram.errors import Forbidden, FloodWait +from pyrogram.raw.functions.messages import ForwardMessages from pagermaid import log from pagermaid.config import Config from pagermaid.listener import listener -from pagermaid.utils import lang, Message +from pagermaid.utils import lang +from pagermaid.enums import Client, Message @listener(is_plugin=False, outgoing=True, command="id", @@ -16,7 +18,7 @@ async def userid(message: Message): text = f"Message ID: `{str(message.id)}" + "`\n\n" text += "**Chat**\nid:`" + str(message.chat.id) + "`\n" msg_from = message.chat - if msg_from.type == "private": + if msg_from.type == ChatType.PRIVATE: try: text += f"first_name: `{msg_from.first_name}" + "`\n" except TypeError: @@ -25,7 +27,7 @@ async def userid(message: Message): text += f"last_name: `{msg_from.last_name}" + "`\n" if msg_from.username: text += f"username: @{msg_from.username}" + "\n" - if msg_from.type in ["supergroup", "channel"]: + if msg_from.type in [ChatType.SUPERGROUP, ChatType.CHANNEL]: text += f"title: `{msg_from.title}" + "`\n" try: if msg_from.username: @@ -129,7 +131,7 @@ async def logging(message: Message): @listener(is_plugin=False, outgoing=True, command="re", description=lang('re_des'), parameters=lang('re_parameters')) -async def re(message: Message): +async def re(bot: Client, message: Message): """ Forwards a message into this group """ if reply := message.reply_to_message: if message.arguments == '': @@ -145,16 +147,26 @@ async def re(message: Message): for _ in range(num): try: if not message.chat.has_protected_content: - await reply.forward(reply.chat.id) + await forward_msg(bot, message.reply_to_message) else: - await reply.copy(reply.chat.id) - except Forbidden: - return - except FloodWait: - return - except ValueError: - return - except Exception as e: # noqa + await reply.copy(reply.chat.id, reply_to_message_id=message.reply_to_top_message_id) + except (Forbidden, FloodWait, Exception): return else: await message.edit(lang('not_reply')) + + +async def forward_msg(bot: Client, message: Message): + message_ids = [message.id] + await bot.invoke( + ForwardMessages( + to_peer=await bot.resolve_peer(message.chat.id), + from_peer=await bot.resolve_peer(message.chat.id), + id=message_ids, + silent=None, + random_id=[bot.rnd_id() for _ in message_ids], + schedule_date=None, + noforwards=None, + top_msg_id=message.reply_to_top_message_id, + ) + ) diff --git a/pagermaid/modules/prune.py b/pagermaid/modules/prune.py index 35e9ff1..f921c99 100644 --- a/pagermaid/modules/prune.py +++ b/pagermaid/modules/prune.py @@ -160,4 +160,5 @@ async def send_prune_notify(bot: Client, message: Message, count_buffer, count): return await bot.send_message( message.chat.id, f"{lang('spn_deleted')} {str(count_buffer)} / {str(count)} {lang('prune_hint2')}", + reply_to_message_id=message.reply_to_top_message_id, ) diff --git a/pagermaid/modules/system.py b/pagermaid/modules/system.py index 0cb3c44..9940f80 100644 --- a/pagermaid/modules/system.py +++ b/pagermaid/modules/system.py @@ -7,6 +7,8 @@ from sys import exit from platform import node from getpass import getuser +from pyrogram.enums import ParseMode + from pagermaid.listener import listener from pagermaid.enums import Message from pagermaid.services import bot @@ -64,9 +66,11 @@ async def restart(message: Message): parameters=lang('eval_parameters')) async def sh_eval(message: Message): """ Run python commands from Telegram. """ + dev_mode = exists(f"data{sep}dev") try: + assert dev_mode cmd = message.text.split(" ", maxsplit=1)[1] - except IndexError: + except (IndexError, AssertionError): return await message.edit(lang('eval_need_dev')) old_stderr = sys.stderr old_stdout = sys.stdout @@ -89,9 +93,9 @@ async def sh_eval(message: Message): evaluation = stdout else: evaluation = "Success" - final_output = f"**>>>** ```{cmd}``` \n```{evaluation}```" + final_output = f"**>>>** `{cmd}` \n`{evaluation}`" if len(final_output) > 4096: - message = await message.edit(f"**>>>** ```{cmd}```") + message = await message.edit(f"**>>>** `{cmd}`", parse_mode=ParseMode.MARKDOWN) await attach_log(evaluation, message.chat.id, "output.log", message.id) else: await message.edit(final_output) @@ -106,7 +110,7 @@ async def send_log(message: Message): return await message.edit(lang("send_log_not_found")) await upload_attachment("pagermaid.log.txt", message.chat.id, - message.reply_to_message_id, + message.reply_to_message_id or message.reply_to_top_message_id, thumb=f"pagermaid{sep}assets{sep}logo.jpg", caption=lang("send_log_caption")) await message.safe_delete() diff --git a/pyromod/listen/temp_fix.py b/pyromod/listen/temp_fix.py new file mode 100644 index 0000000..7e67c9e --- /dev/null +++ b/pyromod/listen/temp_fix.py @@ -0,0 +1,18 @@ +import pyrogram + + +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 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 + return parsed