async def get_command_from_target(self, client: Client,
                                   msg: Message) -> None:
     if re.match(r'^\/(del(f)?|b|undo|print)$', msg.text):
         if msg.text == '/b':
             for_id = await self.checker.query_forward_from(
                 msg.chat.id, msg.reply_to_message.message_id)
             await self.add_black_list(for_id, self.owner_group_id)
             # To enable delete message, please add `delete other messages' privilege to bot
             call_delete_msg(
                 30, client.delete_messages, msg.chat.id,
                 (msg.message_id, msg.reply_to_message.message_id))
         elif msg.text == '/undo':
             group_id = msg.reply_to_message.message_id if msg.reply_to_message else None
             if group_id:
                 try:
                     if await self.redis.srem(
                             f'{self.redis_prefix}for_admin', group_id):
                         await self.checker.remove_admin(group_id)
                     await client.send_message(
                         self.owner_group_id,
                         f'Remove `{group_id}` from blacklist', 'markdown')
                 except ValueError:
                     await client.send_message(
                         self.owner_group_id,
                         f'`{group_id}` not in blacklist', 'markdown')
         elif msg.text == '/print' and msg.reply_to_message is not None:
             print(msg.reply_to_message)
         else:
             call_delete_msg(20, client.delete_messages, msg.chat.id,
                             msg.message_id)
             if get_forward_id(msg.reply_to_message):
                 await self.del_message_by_id(client, msg,
                                              self.owner_group_id,
                                              msg.text[-1] == 'f')
 async def _run(self) -> None:
     while self.get_status():
         task = asyncio.create_task(self.get())
         while True:
             result, _pending = await asyncio.wait([task], timeout=1)
             if len(result):
                 request = result.pop().result()
                 break
             if not self.get_status():
                 task.cancel()
                 return
         try:
             r = await request.msg.forward(request.target_id, True)
             await self.checker.insert_log(r.chat.id, r.message_id,
                                           request.msg.chat.id,
                                           request.msg.message_id,
                                           get_msg_from(request.msg),
                                           get_forward_id(request.msg, -1)
                                           )  # type: ignore
             if request.log.need_log:
                 self.logger.info(request.log.fmt_log,
                                  *request.log.fmt_args)
         except pyrogram.errors.exceptions.bad_request_400.MessageIdInvalid:
             pass
         except:
             if request.msg and request.target_id != self.configure.blacklist:
                 print(repr(request.msg))
             # self.put(target_id, chat_id, msg_id, request.log, msg_raw)
             logger.exception('Got other exceptions in forward thread')
         await asyncio.sleep(.5)
 async def process_private(self, _client: Client, msg: Message) -> None:
     if self.custom_switch:
         obj = getattr(msg, self.get_file_type(msg), None)
         if obj:
             await msg.reply('```{}```\n{}'.format(
                 str(obj), 'Resolution: `{}`'.format(
                     msg.photo.file_size /
                     (msg.photo.width * msg.photo.height) *
                     1000) if msg.photo else ''),
                             parse_mode='markdown')
     if self.echo_switch:
         await msg.reply('forward_from = `{}`'.format(
             get_forward_id(msg, -1)),
                         parse_mode='markdown')
         if self.detail_msg_switch: print(msg)
     if msg.text is None: return
     r = re.match(r'^Add (-?\d+) to blacklist$', msg.text)
     if r is None: return
     await self.add_black_list(int(r.group(1)), msg.chat.id)
    async def del_message_by_id(self,
                                client: Client,
                                msg: Message,
                                send_message_to: Optional[Union[int,
                                                                str]] = None,
                                forward_control: bool = True) -> None:
        if forward_control and self.configure.blacklist == '':
            logger.error('Request forward but blacklist channel not specified')
            return
        id_from_reply = get_forward_id(msg.reply_to_message)
        q = await self.checker.query('''SELECT * FROM "msg_detail" 
        WHERE ("from_chat" = $1 OR "from_user" = $2 OR "from_forward" = $3) 
        AND "to_chat" != $4''', (id_from_reply, id_from_reply, id_from_reply,
                                 self.configure.blacklist))  # type: ignore

        if send_message_to:
            _msg = await client.send_message(send_message_to,
                                             f'Find {len(q)} message(s)')

            for x in q:
                try:
                    await client.delete_messages(x['to_chat'], x['to_msg'])
                except:
                    pass
            await self.checker.execute('''DELETE FROM "msg_detail" 
            WHERE ("from_chat" = $1 OR "from_user" = $2 OR "from_forward" = $3) 
            AND "to_chat" != $4''', (id_from_reply, id_from_reply,
                                     id_from_reply, self.configure.blacklist)
                                       )  # type: ignore
            await _msg.edit(
                f'Delete all message from `{id_from_reply}` completed.',
                'markdown')
        else:
            for x in q:
                try:
                    await client.delete_messages(x['to_chat'], x['to_msg'])
                except:
                    pass
            await msg.reply(
                f'Delete all message from `{id_from_reply}` completed.', False,
                'markdown')
Beispiel #5
0
    async def del_message_by_id(
            self,
            client: Client,
            msg: Message,
            send_message_to: int | str | None = None) -> None:
        id_from_reply = get_forward_id(msg.reply_to_message)
        q = await self.checker.query(
            '''SELECT "to_chat", "to_msg" FROM "msg_detail"
         WHERE ("from_chat" = $1 OR "from_user" = $2 OR "forward_from" = $3)''',
            id_from_reply,
            id_from_reply,
            id_from_reply,
        )

        if send_message_to:
            _msg = await client.send_message(send_message_to,
                                             f'Find {len(q)} message(s)')

            for x in q:
                try:
                    await client.delete_messages(x['to_chat'], x['to_msg'])
                except pyrogram.errors.RPCError:
                    pass
            await self.checker.execute(
                '''DELETE FROM "msg_detail" 
                 WHERE ("from_chat" = $1 OR "from_user" = $2 OR "forward_from" = $3)''',
                id_from_reply, id_from_reply, id_from_reply)
            await _msg.edit(
                f'Delete all message from `{id_from_reply}` completed.',
                'markdown')
        else:
            for x in q:
                try:
                    await client.delete_messages(x['to_chat'], x['to_msg'])
                except pyrogram.errors.RPCError:
                    pass
            await msg.reply(
                f'Delete all message from `{id_from_reply}` completed.', False,
                'markdown')