async def adminlist(c: Mizuhara, m: Message): _ = GetLang(m).strs try: me_id = int(get_key("BOT_ID")) # Get Bot ID from Redis! adminlist = get_key("ADMINDICT")[str( m.chat.id)] # Load ADMINDICT from string adminstr = _("admin.adminlist").format(chat_title=m.chat.title) for i in adminlist: usr = await c.get_users(i) if i == me_id: adminstr += f"- {mention_html(usr.first_name, i)} (Me)\n" else: usr = await c.get_users(i) adminstr += f"- {mention_html(usr.first_name, i)} (`{i}`)\n" await m.reply_text(adminstr) except Exception as ef: if str(ef) == str(m.chat.id): await m.reply_text(_("admin.useadmincache")) else: await m.reply_text( _("admin.somerror").format(SUPPORT_GROUP=SUPPORT_GROUP, ef=ef)) LOGGER.error(ef) return
async def demote_usr(c: Mizuhara, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return from_user = await m.chat.get_member(m.from_user.id) # If user does not have permission to invite other users, return if from_user.can_invite_users or from_user.status == "creator": try: link = await c.export_chat_invite_link(m.chat.id) await m.reply_text(_("admin.invitelink").format(link=link)) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except errors.ChatAdminInviteRequired: await m.reply_text(_("admin.noinviteperm")) except Exception as ef: await m.reply_text(_("admin.useadmincache")) LOGGER.error(ef) return await m.reply_text(_("admin.nouserinviteperm")) return
async def pin_message(c: Mizuhara, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return pin_loud = m.text.split(" ", 1) if m.reply_to_message: try: disable_notification = True if len(pin_loud) >= 2 and pin_loud[1] in [ "alert", "notify", "loud" ]: disable_notification = False pinned_event = await c.pin_chat_message( m.chat.id, m.reply_to_m.message_id, disable_notification=disable_notification, ) await m.reply_text(_("admin.pinnedmsg")) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text(_("admin.useadmincache")) LOGGER.error(ef) else: await m.reply_text(_("admin.nopinmsg")) return
async def initial_works(c: Mizuhara, m: Message): try: if m.migrate_to_chat_id or m.migrate_from_chat_id: if m.migrate_to_chat_id: old_chat = m.chat.id new_chat = m.migrate_to_chat_id elif m.migrate_from_chat_id: old_chat = m.migrate_from_chat_id new_chat = m.chat.id try: await migrate_chat(old_chat, new_chat) except Exception as ef: LOGGER.error(ef) return else: userdb.update_user(m.from_user.id, m.from_user.username, m.chat.id, m.chat.title) if m.reply_to_message: userdb.update_user( m.reply_to_message.from_user.id, m.reply_to_message.from_user.username, m.chat.id, m.chat.title, ) if m.forward_from: userdb.update_user(m.forward_from.id, m.forward_from.username) except AttributeError: pass # Skip attribute errors! return
async def get_note(c: Mizuhara, m: Message): if len(m.text.split()) >= 2: note = m.text.split()[1] else: await m.reply_text("Give me a note tag!") getnotes = db.get_note(m.chat.id, note) if not getnotes: await m.reply_text("This note does not exist!") return if getnotes["type"] == Types.TEXT: teks, button = parse_button(getnotes.get("value")) button = build_keyboard(button) button = InlineKeyboardMarkup(button) if button else None if button: try: await m.reply_text(teks, reply_markup=button) return except Exception as ef: await m.reply_text("An error has accured! Cannot parse note.") LOGGER.error(ef) return else: await m.reply_text(teks) return elif getnotes["type"] in ( Types.STICKER, Types.VOICE, Types.VIDEO_NOTE, Types.CONTACT, Types.ANIMATED_STICKER, ): await GET_FORMAT[getnotes["type"]](m.chat.id, getnotes["file"]) else: if getnotes.get("value"): teks, button = parse_button(getnotes.get("value")) button = build_keyboard(button) button = InlineKeyboardMarkup(button) if button else None else: teks = None button = None if button: try: await m.reply_text(teks, reply_markup=button) return except Exception as ef: await m.reply_text("An error has accured! Cannot parse note.") LOGGER.error(ef) return else: await GET_FORMAT[getnotes["type"]](m.chat.id, getnotes["file"], caption=teks) return
async def stop(self, *args): # Send a message to MESSAGE_DUMP telling that the bot has stopped! await self.send_message( MESSAGE_DUMP, "<i><b>Bot Stopped!</b></i>", ) await super().stop() # Flush Redis data try: flushredis() LOGGER.info("Flushed Redis!") except Exception as ef: LOGGER.error(ef) LOGGER.info("Bot Stopped.\nkthxbye!")
async def demote_usr(c: Mizuhara, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return from_user = await m.chat.get_member(m.from_user.id) # If user does not have permission to demote other users, return if from_user.can_promote_members or from_user.status == "creator": user_id, user_first_name = await extract_user(c, m) try: await m.chat.promote_member( user_id=user_id, can_change_info=False, can_delete_messages=False, can_restrict_members=False, can_invite_users=False, can_pin_messages=False, ) await m.reply_text( _("admin.demoted").format( demoter=mention_html(m.from_user.first_name, m.from_user.id), demoted=mention_html(user_first_name, user_id), chat_title=m.chat.title, )) # ----- Add admin to redis cache! ----- ADMINDICT = get_key("ADMINDICT") # Load ADMINDICT from string adminlist = [] async for i in m.chat.iter_members(filter="administrators"): adminlist.append(i.user.id) ADMINDICT[str(m.chat.id)] = adminlist set_key("ADMINDICT", ADMINDICT) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text(_("admin.useadmincache")) LOGGER.error(ef) return await m.reply_text(_("admin.nodemoteperm")) return
async def unpin_message(c: Mizuhara, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return try: await m.chat.unpin_chat_message(m.chat.id) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text( _("admin.somerror").format(SUPPORT_GROUP=SUPPORT_GROUP, ef=ef)) LOGGER.error(ef) return
async def ban_usr(c: Mizuhara, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return from_user = await m.chat.get_member(m.from_user.id) if from_user.can_restrict_members or from_user.status == "creator": user_id, user_first_name = await extract_user(c, m) try: await c.kick_chat_member(m.chat.id, user_id) await m.reply_text( f"Banned {mention_html(user_first_name, user_id)}") except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text(f"Error: {ef}\n\nReport it in Support Group!") LOGGER.error(ef) return
async def reload_admins(c: Mizuhara, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return ADMINDICT = get_key("ADMINDICT") # Load ADMINDICT from string try: adminlist = [] async for i in m.chat.iter_members(filter="administrators"): adminlist.append(i.user.id) ADMINDICT[str(m.chat.id)] = adminlist set_key("ADMINDICT", ADMINDICT) await m.reply_text(_("admin.reloadedadmins")) LOGGER.info(f"Reloaded admins for {m.chat.title}({m.chat.id})") except Exception as ef: await m.reply_text(_("admin.useadmincache")) LOGGER.error(ef) return
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 errors.PeerIdInvalid: pass 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}")