async def _init(): async for chat in LYDIA_CHATS.find({'active': True}): ACTIVE_CHATS[chat['_id']] = (chat['session_id'], chat['session_exp']) if CUSTOM_REPLY_CHANNEL: async for message in userge.iter_history(chat_id=CUSTOM_REPLY_CHANNEL, limit=300): CUSTOM_REPLIES.append(message)
async def _init(): async for chat in LYDIA_CHATS.find({"active": True}): ACTIVE_CHATS[chat["_id"]] = (chat["session_id"], chat["session_exp"]) if CUSTOM_REPLY_CHANNEL: async for message in userge.iter_history(chat_id=CUSTOM_REPLY_CHANNEL, limit=300): CUSTOM_REPLIES_IDS.append(message.message_id)
async def load_playlist(self, cache=False): filename = f"playlist{self.chat_id}.json" if os.path.isfile(filename) and not cache: data = json.load(open(filename)) self.playlist = await userge.get_messages(self.chat_id, data["message_ids"]) else: async for message in userge.iter_history(self.chat_id): if message.audio: self.playlist.append(message)
async def quotecmd(message: Message): """quotecmd""" quote_list = [] replied = message.reply_to_message if replied and "l" in message.flags and not message.filtered_input_str: args = "" limit = message.flags.get("l", 1) if limit.isdigit(): limit = int(limit) else: return await message.err("give valid no. of message to quote", del_in=3) num = min(limit, 24) async for msg in userge.iter_history( message.chat.id, limit=num, offset_id=replied.message_id, reverse=True ): if msg.message_id != message.message_id: quote_list.append(msg.message_id) else: args = message.input_str quote_list.append(replied.message_id) asyncio.get_event_loop().create_task(message.delete()) async with userge.conversation("QuotLyBot") as conv: try: if quote_list and not args: await userge.forward_messages("QuotLyBot", message.chat.id, quote_list) else: if not args: await message.err("input not found!") return await conv.send_message(args) except YouBlockedUser: await message.edit("first **unblock** @QuotLyBot") return quote = await conv.get_response(mark_read=True) if not (quote.sticker or quote.document): await message.err("something went wrong!") else: message_id = replied.message_id if replied else None if quote.sticker: await userge.send_sticker( chat_id=message.chat.id, sticker=quote.sticker.file_id, file_ref=quote.sticker.file_ref, reply_to_message_id=message_id, ) else: await userge.send_document( chat_id=message.chat.id, document=quote.document.file_id, file_ref=quote.document.file_ref, reply_to_message_id=message_id, )
async def purgeme_(message: Message): """purge given no. of your messages""" await message.edit("`purging ...`") if not (message.input_str and message.input_str.isdigit()): return await message.err("Provide a valid number of message to delete", del_in=3) start_t = datetime.datetime.now() number = min(int(message.input_str), 100) mid = message.message_id msg_list = [] # https://t.me/pyrogramchat/266224 # search_messages takes some minutes to index new messages # so using iter_history to get messages newer than 5 mins. old_msg = (start_t - datetime.timedelta(minutes=5)).timestamp() async for msg in userge.search_messages(message.chat.id, "", limit=number, from_user="******"): msg_list.append(msg.message_id) async for new_msg in userge.iter_history(message.chat.id, offset_id=mid, offset=0): if new_msg.from_user.is_self: msg_list.append(new_msg.message_id) if old_msg > new_msg.date or (msg_list and (msg_list[-1] > new_msg.message_id)): break # https://stackoverflow.com/questions/39734485/python-combining-two-lists-and-removing-duplicates-in-a-functional-programming del_list = list(set(msg_list)) if mid in del_list: del_list.remove(mid) del_list.reverse() del_list_ = del_list[:number] await userge.delete_messages(message.chat.id, message_ids=del_list_) end_t = datetime.datetime.now() time_taken_s = (end_t - start_t).seconds out = f"<u>purged</u> {len(del_list_)} messages in {time_taken_s} seconds." await message.edit(out, del_in=3)
async def word_count(message: Message): """Finds most words used""" words = custom() await message.edit("```Processed 0 messages...```") total = 0 async for msg in userge.iter_history(message.chat.id, 1000): total += 1 if total % 200 == 0: await message.edit(f"```Processed {total} messages...```") await asyncio.sleep(0.5) if msg.text: for word in msg.text.split(): words[word.lower()] += 1 if msg.caption: for word in msg.caption.split(): words[word.lower()] += 1 freq = sorted(words, key=words.get, reverse=True) out = "`Word Counter of last 1000 messages.`\n" for i in range(25): out += f"{i + 1}. **{words[freq[i]]}**: `{freq[i]}`\n" await message.edit(out)
async def purgeme_(message: Message): """purge given no. of your messages""" await message.edit("`purging ...`") if not (message.input_str and message.input_str.isdigit()): return await message.err("Provide a valid number of message to delete", del_in=3) start_t = time.time() number = min(int(message.input_str), 100) msg_list = [] new_msg = [] old_msg = (datetime.datetime.now() - datetime.timedelta(minutes=5)).timestamp() async for msgg in userge.iter_history(message.chat.id, offset_id=message.message_id, offset=0): if msgg.from_user.is_self: new_msg.append(msgg.message_id) if old_msg > msgg.date: break async for msg in userge.search_messages(message.chat.id, "", limit=number, from_user="******"): msg_list.append(msg.message_id) if new_msg: for mids in new_msg: if mids > msg.message_id: msg_list.append(mids) await userge.delete_messages(message.chat.id, message_ids=msg_list) end_t = time.time() out = ( f"<u>purged</u> {len(msg_list)} messages in {time_formatter(end_t - start_t)}." ) await message.edit(out, del_in=3)
async def quotecmd(message: Message): """quotecmd""" reply = message.reply_to_message quote_list = [] self_mid = False args = "" if reply: if "l" in message.flags: limit = message.flags.get("l", 1) if not limit.isdigit(): await message.err("give valid no. of message to quote", del_in=5) return num_ = min(int(limit), 24) async for msg in userge.iter_history(message.chat.id, limit=num_, offset_id=reply.message_id, reverse=True): if msg.message_id != message.message_id: quote_list.append(msg.message_id) if message.filtered_input_str: self_mid = True await message.edit(message.filtered_input_str) else: quote_list.append(reply.message_id) if message.input_str: self_mid = True await message.edit(message.input_str) else: args = message.input_str quote_list.append( message.message_id) if self_mid else await message.delete() if not args and len(quote_list) == 0: await message.err("Reply to a message or provide an input to quote !", del_in=5) return try: async with userge.conversation("QuotLyBot", timeout=100) as conv: try: if quote_list: await userge.forward_messages("QuotLyBot", message.chat.id, quote_list) if self_mid: await message.delete() elif args: await conv.send_message(args) except YouBlockedUser: await message.edit("first **unblock** @QuotLyBot") return quote = await conv.get_response(mark_read=True) if not (quote.sticker or quote.document): await message.err("something went wrong!") return message_id = reply.message_id if reply else None if quote.sticker: await userge.send_sticker( chat_id=message.chat.id, sticker=quote.sticker.file_id, reply_to_message_id=message_id, ) else: await userge.send_document( chat_id=message.chat.id, document=quote.document.file_id, reply_to_message_id=message_id, ) except StopConversation: await message.err( "@QuotLyBot Didn't respond in time\n:( please try again later...", del_in=5)