async def chats(c: Alita, m: Message): exmsg = await m.reply_text("`Exporting Chatlist...`") await c.send_message( MESSAGE_DUMP, f"#CHATLIST\n\n**User:** {mention_markdown(m.from_user.first_name, m.from_user.id)}", ) all_chats = userdb.get_all_chats() or [] chatfile = "List of chats.\n\nChat name | Chat ID | Members count\n" P = 1 for chat in all_chats: try: chat_info = await c.get_chat(chat.chat_id) chat_members = chat_info.members_count try: invitelink = chat_info.invite_link except KeyError: invitelink = "No Link!" chatfile += "{}. {} | {} | {} | {}\n".format( P, chat.chat_name, chat.chat_id, chat_members, invitelink) P += 1 except errors.ChatAdminRequired: pass except errors.ChannelPrivate: userdb.rem_chat(chat.chat_id) except Exception as ef: await m.reply_text(f"**Error:**\n{ef}") with io.BytesIO(str.encode(chatfile)) as output: output.name = "chatlist.txt" await m.reply_document( document=output, caption="Here is the list of chats in my Database.") await exmsg.delete() return
async def get_admins(self): """Cache all admins from chats in Redis DB.""" LOGGER.info("Begin caching admins...") begin = time() all_chats = userdb.get_all_chats() or [] # Get list of all chats LOGGER.info(f"{len(all_chats)} chats loaded from database.") try: ADMINDICT = await get_key("ADMINDICT") except BaseException as ef: LOGGER.error(f"Unable to get ADMINDICT!\nError: {ef}") ADMINDICT = {} for i in all_chats: chat_id = i.chat_id adminlist = [] try: async for j in self.iter_chat_members( chat_id=chat_id, filter="administrators", ): if j.user.is_deleted: continue adminlist.append( ( j.user.id, f"@{j.user.username}" if j.user.username else j.user.first_name, ), ) adminlist = sorted(adminlist, key=lambda x: x[1]) ADMINDICT[str(i.chat_id)] = adminlist # Remove the last space LOGGER.info( f"Set {len(adminlist)} admins for {i.chat_id}\n- {adminlist}", ) except PeerIdInvalid: pass except RPCError as ef: LOGGER.error(ef) try: await set_key("ADMINDICT", ADMINDICT) end = time() LOGGER.info( ( "Set admin list cache!" f"Time Taken: {round(end - begin, 2)} seconds." ), ) except BaseException as ef: LOGGER.error(f"Could not set ADMINDICT in Redis Cache!\nError: {ef}")
async def get_muted_chats(c: Alita, m: Message, leave: bool = False): chat_id = m.chat.id chats = user_db.get_all_chats() muted_chats, progress = 0, 0 chat_list = [] progress_message = m for chat in chats: if ((100 * chats.index(chat)) / len(chats)) > progress: progress_bar = f"{progress}% completed in getting muted chats." if progress_message: try: bot.editMessageText(progress_bar, chat_id, progress_message.message_id) except Exception: pass else: progress_message = await m.edit_text(progress_bar) progress += 5 cid = chat.chat_id sleep(0.1) try: await c.send_chat_action(cid, "typing") except (BadRequest, Unauthorized): muted_chats += 1 chat_list.append(cid) except Exception: pass try: await progress_message.delete() except Exception: pass if not leave: return muted_chats else: for muted_chat in chat_list: sleep(0.1) try: await c.leave_chat(muted_chat) except Exception: pass user_db.rem_chat(muted_chat) return muted_chats
async def chats(c: Alita, m: Message): exmsg = await m.reply_text("`Exporting Chatlist...`") await c.send_message( MESSAGE_DUMP, f"#CHATLIST\n\n**User:** {(await mention_markdown(m.from_user.first_name, m.from_user.id))}", ) all_chats = userdb.get_all_chats() or [] chatfile = "List of chats.\n\nChat name | Chat ID | Members count\n" P = 1 for chat in all_chats: try: chat_info = await c.get_chat(chat.chat_id) chat_members = chat_info.members_count try: invitelink = chat_info.invite_link except KeyError: invitelink = "No Link!" chatfile += "{}. {} | {} | {} | {}\n".format( P, chat.chat_name, chat.chat_id, chat_members, invitelink, ) P += 1 except ChatAdminRequired: pass except ChannelPrivate: userdb.rem_chat(chat.chat_id) except PeerIdInvalid: LOGGER.warning(f"Group not loaded {chat.chat_id}") except RPCError as ef: LOGGER.error(ef) await m.reply_text(f"**Error:**\n{ef}") with BytesIO(str.encode(chatfile)) as f: f.name = "chatlist.txt" await m.reply_document( document=f, caption=tlang(m, "dev.chats_in_db"), ) await exmsg.delete() return
async def get_invalid_chats(c: Alita, m: Message, remove: bool = False): chats = user_db.get_all_chats() kicked_chats, progress = 0, 0 chat_list = [] progress_message = m for chat in chats: if ((100 * chats.index(chat)) / len(chats)) > progress: progress_bar = f"{progress}% completed in getting invalid chats." if progress_message: try: await m.edit_text(progress_bar) except RPCError: pass else: progress_message = await m.reply_text(progress_bar) progress += 5 cid = chat.chat_id await sleep(0.1) try: await c.get_chat(cid) except (BadRequest, Unauthorized): kicked_chats += 1 chat_list.append(cid) except RPCError: pass try: await progress_message.delete() except RPCError: pass if not remove: return kicked_chats for muted_chat in chat_list: try: await sleep(0.1) user_db.rem_chat(muted_chat) except RPCError: pass return kicked_chats
async def get_admins(self): LOGGER.info("Begin caching admins...") begin = time.time() c = self # Flush Redis data try: flushredis() except Exception as ef: LOGGER.error(ef) # Get bot info me = await self.get_me() all_chats = userdb.get_all_chats() or [] # Get list of all chats LOGGER.info(f"{len(all_chats)} chats loaded.") ADMINDICT = {} for chat in all_chats: adminlist = [] try: async for i in c.iter_chat_members( chat_id=chat.chat_id, filter="administrators" ): adminlist.append(i.user.id) ADMINDICT[str(chat.chat_id)] = adminlist # Remove the last space LOGGER.info( f"Set {len(adminlist)} admins for {chat.chat_id}\n{adminlist}" ) del adminlist # Delete list var except Exception as ef: LOGGER.error(ef) try: set_key("ADMINDICT", ADMINDICT) del ADMINDICT end = time.time() LOGGER.info(f"Set admin list cache!\nTime Taken: {round(end-begin, 2)}s") except Exception as ef: LOGGER.error(f"Could not set ADMINDICT!\n{ef}")