From fade059e80d77c09922b479f01d4b487137dfa15 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Wed, 25 May 2022 19:26:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Update=20to=20v1.0.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagermaid/__init__.py | 12 ++++++--- pagermaid/listener.py | 2 -- pagermaid/modules/account.py | 32 ++--------------------- pagermaid/single_utils.py | 21 +++++++-------- pyromod/listen/__init__.py | 2 +- pyromod/listen/listen.py | 25 ++++++++++++++++++ utils/gensession.py | 50 ++++++++++++++++++++++++++++++++++++ utils/install.sh | 2 +- 8 files changed, 98 insertions(+), 48 deletions(-) create mode 100644 utils/gensession.py diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 14f8388..825ceb9 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -9,7 +9,7 @@ import pyromod.listen from pyrogram import Client import sys -pgm_version = "1.0.1" +pgm_version = "1.0.2" CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() @@ -17,9 +17,8 @@ working_dir = getcwd() read_context = {} help_messages = {} all_permissions = [] -scheduler = AsyncIOScheduler() +scheduler = AsyncIOScheduler(timezone="Asia/ShangHai") if not scheduler.running: - scheduler.configure(timezone="Asia/ShangHai") scheduler.start() logs = getLogger(__name__) logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s" @@ -40,7 +39,12 @@ elif not Config.API_HASH: sys.exit(1) start_time = datetime.utcnow() -bot = Client("pagermaid", api_id=Config.API_ID, api_hash=Config.API_HASH, ipv6=Config.IPV6, proxy=Config.PROXY) +bot = Client("pagermaid", + session_string=Config.STRING_SESSION, + api_id=Config.API_ID, + api_hash=Config.API_HASH, + ipv6=Config.IPV6, + proxy=Config.PROXY) async def log(message): diff --git a/pagermaid/listener.py b/pagermaid/listener.py index 88c3875..d3a5da0 100644 --- a/pagermaid/listener.py +++ b/pagermaid/listener.py @@ -38,8 +38,6 @@ def listener(**args): is_plugin = args.get('is_plugin', True) incoming = args.get('incoming', False) outgoing = args.get('outgoing', True) - owners_only = args.get("owners_only", False) - admins_only = args.get("admins_only", False) groups_only = args.get("groups_only", False) privates_only = args.get("privates_only", False) diff --git a/pagermaid/modules/account.py b/pagermaid/modules/account.py index 309f7b5..bceee9b 100644 --- a/pagermaid/modules/account.py +++ b/pagermaid/modules/account.py @@ -87,27 +87,12 @@ async def profile(client: Client, message: Message): parameters="(username/uid/reply)") async def block_user(client: Client, message: Message): """ Block a user. """ - current_chat = message.chat if len(message.parameter) > 1: await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") return if not Config.SILENT: await message.edit(lang('block_process')) - user = None - # Priority: reply > argument > current_chat - if message.reply_to_message: # Reply to a user - user = message.reply_to_message.from_user - if user: - user = user.id - if not user and len(message.parameter) == 1: # Argument provided - (raw_user,) = message.parameter - if raw_user.isnumeric(): - user = int(raw_user) - elif message.entities is not None: - if message.entities[0].type == "text_mention": - user = message.entities[0].user.id - if not user and current_chat.type == "private": # Current chat - user = current_chat.id + user = message.obtain_user() if not user: return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") try: @@ -129,20 +114,7 @@ async def unblock_user(client: Client, message: Message): return if not Config.SILENT: await message.edit(lang('unblock_process')) - user = None - if message.reply_to_message: # Reply to a user - user = message.reply_to_message.from_user - if user: - user = user.id - if not user and len(message.parameter) == 1: # Argument provided - (raw_user,) = message.parameter - if raw_user.isnumeric(): - user = int(raw_user) - elif message.entities is not None: - if message.entities[0].type == "text_mention": - user = message.entities[0].user.id - if not user and message.chat.type == "private": # Current chat - user = message.chat.id + user = message.obtain_user() if not user: return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}") try: diff --git a/pagermaid/single_utils.py b/pagermaid/single_utils.py index e9def3b..7e71521 100644 --- a/pagermaid/single_utils.py +++ b/pagermaid/single_utils.py @@ -1,6 +1,6 @@ from os import sep, remove, mkdir from os.path import exists -from typing import List +from typing import List, Optional from pyrogram.types import Message from sqlitedict import SqliteDict @@ -29,12 +29,13 @@ class Message(Message): # noqa arguments: str parameter: List - async def safe_delete(self, revoke: bool = True): - try: - return await self._client.delete_messages( - chat_id=self.chat.id, - message_ids=self.id, - revoke=revoke - ) - except Exception as e: # noqa - return False + def obtain_message(self) -> Optional[str]: + """ Obtains a message from either the reply message or command arguments. """ + return + + def obtain_user(self) -> Optional[int]: + """ Obtains a user from either the reply message or command arguments. """ + return + + async def safe_delete(self, revoke: bool = True) -> None: + return diff --git a/pyromod/listen/__init__.py b/pyromod/listen/__init__.py index 150b549..b916072 100644 --- a/pyromod/listen/__init__.py +++ b/pyromod/listen/__init__.py @@ -18,4 +18,4 @@ You should have received a copy of the GNU General Public License along with pyromod. If not, see . """ -from .listen import Client, MessageHandler, Chat, User \ No newline at end of file +from .listen import Client, MessageHandler, Chat, User diff --git a/pyromod/listen/listen.py b/pyromod/listen/listen.py index e460f36..3548b79 100644 --- a/pyromod/listen/listen.py +++ b/pyromod/listen/listen.py @@ -160,6 +160,31 @@ class Message(pyrogram.types.Message): except Exception as e: # noqa return False + @patchable + def obtain_message(self) -> Optional[str]: + """ Obtains a message from either the reply message or command arguments. """ + return self.arguments if self.arguments else (self.reply_to_message.text if self.reply_to_message else None) + + @patchable + def obtain_user(self) -> Optional[int]: + """ Obtains a user from either the reply message or command arguments. """ + user = None + # Priority: reply > argument > current_chat + if self.reply_to_message: # Reply to a user + user = self.reply_to_message.from_user + if user: + user = user.id + if not user and len(self.parameter) == 1: # Argument provided + (raw_user,) = self.parameter + if raw_user.isnumeric(): + user = int(raw_user) + elif self.entities is not None: + if self.entities[0].type == pyrogram.enums.MessageEntityType.TEXT_MENTION: + user = self.entities[0].user.id + if not user and self.chat.type == pyrogram.enums.ChatType.PRIVATE: # Current chat + user = self.chat.id + return user + @patchable async def edit_text( self, diff --git a/utils/gensession.py b/utils/gensession.py new file mode 100644 index 0000000..1196ff6 --- /dev/null +++ b/utils/gensession.py @@ -0,0 +1,50 @@ +import asyncio +import os +from sys import executable, exit + +try: + from pyrogram.errors import ApiIdInvalid, PhoneNumberInvalid + from pyrogram import Client + print("Found an existing installation of Pyrogram...\nSuccessfully Imported.") +except ImportError: + print("Installing Pyrogram...") + os.system(f"{executable} -m pip install pyrogram") + print("Done. Installed and imported pyrogram.") + from pyrogram.errors import ApiIdInvalid, PhoneNumberInvalid + from pyrogram import Client + + +async def main(): + API_ID = 0 + try: + API_ID = int(input("Please enter your API ID: ")) + except ValueError: + print("APP ID must be an integer.\nQuitting...") + exit(0) + except Exception as e: + raise e + + API_HASH = input("Please enter your API HASH: ") + try: + async with Client("pagermaid", API_ID, API_HASH) as bot: + print("Generating a user session...") + await bot.send_message( + "me", + f"**PagerMaid** `String SESSION`:\n\n`{await bot.export_session_string()}`", + ) + print("Your SESSION has been generated. Check your telegram saved messages!") + exit(0) + except ApiIdInvalid: + print("Your API ID/API HASH combination is invalid. Kindly recheck.\nQuitting...") + exit(0) + except ValueError: + print("API HASH must not be empty!\nQuitting...") + exit(0) + except PhoneNumberInvalid: + print("The phone number is invalid!\nQuitting...") + exit(0) + + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/utils/install.sh b/utils/install.sh index 2ac188d..624e29e 100644 --- a/utils/install.sh +++ b/utils/install.sh @@ -252,7 +252,7 @@ debian_require_install() { download_repo() { echo "下载 repository 中 . . ." rm -rf /var/lib/pagermaid >>/dev/null 2>&1 - git clone https://gitlab.com/Xtao-Labs/pagermaid-modify.git /var/lib/pagermaid >>/dev/null 2>&1 + git clone https://github.com/TeamPGM/PagerMaid-Pyro.git /var/lib/pagermaid >>/dev/null 2>&1 cd /var/lib/pagermaid >>/dev/null 2>&1 echo "Hello World!" >/var/lib/pagermaid/public.lock }