async def demote_fadmin(self, message): """Demote user to fed admin""" chat_id = message.chat.id if message.chat.type == "private": return await message.reply_text(await self.bot.text( chat_id, "err-chat-groups")) user_id = message.from_user.id to_demote, _ = extract_user_and_text(message) if not to_demote: return await message.reply_text(await self.bot.text( chat_id, "fed-no-demote-user")) if isinstance(to_demote, str): to_demote = (await extract_user(self.bot.client, to_demote)).id fed_data = await self.get_fed_bychat(chat_id) if not fed_data: return await message.reply_text(await self.bot.text( chat_id, "fed-no-fed-chat")) if user_id != fed_data["owner"]: return await message.reply_text(await self.bot.text( chat_id, "fed-owner-only-demote")) if to_demote == fed_data["owner"]: return await message.reply_text(await self.bot.text( chat_id, "fed-already-owner")) if to_demote not in fed_data.get("admins", []): return await message.reply_text(await self.bot.text( chat_id, "fed-user-not-admin")) async with self.lock: await self.feds_db.update_one({"_id": fed_data["_id"]}, {"$pull": { "admins": to_demote }}) await message.reply_text(await self.bot.text(chat_id, "fed-demote-done"))
async def mute(self, message): """ Mute chat member """ chat_id = message.chat.id user_id, res = extract_user_and_text(message) if not user_id: return await message.reply_text(await self.bot.text( chat_id, "no-mute-user")) if user_id in [self.bot.identifier, f"@{self.bot.username}"]: return await message.reply_text(await self.bot.text( chat_id, "self-muting")) if await user_ban_protected(self.bot, chat_id, user_id): return await message.reply_text(await self.bot.text( chat_id, "cant-mute-admin")) if res: timeflag = res.split(None, 1)[0].lower() until = await extract_time(timeflag) if not until: return await message.reply_text(await self.bot.text( chat_id, "invalid-time-flag")) tr_string = "mute-success-time" else: timeflag = None tr_string = "mute-success" until = 0 muted = await self._muter(message, user_id, until) if muted: member = (await self.parse_member(user_id)).first_name await message.reply_text(await self.bot.text(chat_id, tr_string, member, timeflag))
async def demoter(self, message): """Demoter Just owner and promoter can demote admin.""" chat_id = message.chat.id user, _ = extract_user_and_text(message) if not user: return await message.reply_text(await self.bot.text( chat_id, "no-demote-user")) if user == self.bot.identifier: return await message.reply_text(await self.bot.text( chat_id, "error-its-myself")) try: await message.chat.promote_member( user_id=user, can_change_info=False, can_post_messages=False, can_edit_messages=False, can_delete_messages=False, can_restrict_members=False, can_promote_members=False, can_invite_users=False, can_pin_messages=False, ) except ChatAdminRequired: return await message.reply_text(await self.bot.text( chat_id, "demote-error-perm")) await message.reply_text(await self.bot.text(chat_id, "demote-success"))
async def promoter(self, message): """Bot promote member, required Both permission of can_promote""" chat_id = message.chat.id user, _ = extract_user_and_text(message) if not user: return await message.reply_text(await self.bot.text( chat_id, "no-promote-user")) if user == self.bot.identifier: return await message.reply_text(await self.bot.text( chat_id, "error-its-myself")) # bot can't assign higher perms than itself! # bot_perm = await self.bot.client.get_chat_member(chat_id, "me") bot_perm = await message.chat.get_member("me") try: await message.chat.promote_member( user_id=user, can_change_info=bot_perm.can_change_info, can_post_messages=bot_perm.can_post_messages, can_edit_messages=bot_perm.can_edit_messages, can_delete_messages=bot_perm.can_delete_messages, can_restrict_members=bot_perm.can_restrict_members, can_promote_members=bot_perm.can_promote_members, can_invite_users=bot_perm.can_invite_users, can_pin_messages=bot_perm.can_pin_messages, ) except ChatAdminRequired: return await message.reply_text(await self.bot.text( chat_id, "promote-error-perm")) except UserIdInvalid: return await message.reply_text(await self.bot.text( chat_id, "promote-error-invalid")) await message.reply_text(await self.bot.text(chat_id, "promote-success"))
async def unban_member(self, message): """ Unban chat member """ user, _, = extract_user_and_text(message) if user is None: return await message.reply_text(await self.text(message.chat.id, "unban-no-user")) await message.chat.unban_member(user) await message.reply_text(await self.text(message.chat.id, "unban-done"))
async def unban_member(self, message): """ Unban chat member """ user, _, = extract_user_and_text(message) if user is None: return await message.reply_text(await self.bot.text( message.chat.id, "unban-no-user")) try: await message.chat.unban_member(user) except PeerIdInvalid: return await message.reply_text(await self.bot.text( message.chat.id, "err-peer-invalid")) unbanned = await self.parse_member(user) await message.reply_text(await self.bot.text(message.chat.id, "unban-done", unbanned.first_name))
async def ban_member(self, message): """ Ban chat member """ user, _ = extract_user_and_text(message) chat_id = message.chat.id if user is None: return await message.reply_text("Who should i ban?\nis it you?") try: if await user_ban_protected(self, chat_id, user): return await message.reply_text(await self.text( chat_id, "no-ban-user")) except UserNotParticipant: return await message.reply_text(await self.text(chat_id, "err-not-participant")) await message.chat.kick_member(user) await message.reply_text(await self.text(chat_id, "ban-done"))
async def unmute(self, message): """ Unmute chat member """ user, _ = extract_user_and_text(message) if user is None: return await message.reply_text(await self.bot.text( message.chat.id, "no-mute-user")) member = await message.chat.get_member(user) if member.can_send_messages is False: await message.chat.unban_member(user) await message.reply_text(await self.bot.text(message.chat.id, "unmute-done")) else: await message.reply_text(await self.bot.text(message.chat.id, "user-not-muted"))
async def ban_member(self, message): """ Ban chat member """ user, _ = extract_user_and_text(message) chat_id = message.chat.id if user is None: return await message.reply_text(await self.bot.text( chat_id, "no-ban-user")) try: if await user_ban_protected(self.bot, chat_id, user): return await message.reply_text(await self.bot.text( chat_id, "admin-ban")) except UserNotParticipant: return await message.reply_text(await self.bot.text( chat_id, "err-not-participant")) await message.chat.kick_member(user) banned = await self.parse_member(user) await message.reply_text(await self.bot.text(chat_id, "ban-done", banned.first_name))
async def kick_member(self, message): """ Kick chat member """ user, _ = extract_user_and_text(message) chat_id = message.chat.id if user is None: return await message.reply_text(await self.lang(chat_id, "no-kick-user")) try: if await user_ban_protected(self, chat_id, user): return await message.reply_text(await self.lang( chat_id, "admin-kick")) except UserNotParticipant: return await message.reply_text(await self.text(chat_id, "err-not-participant")) await message.chat.kick_member(user) await message.chat.unban_member(user) await message.reply_text(await self.text(chat_id, "kick-done"))
async def unfban_user(self, message): """Unban a user on federation""" chat_id = message.chat.id if message.chat.type == "private": return await message.reply_text(await self.bot.text( chat_id, "err-chat-groups")) banner = message.from_user fed_data = await self.get_fed_bychat(chat_id) if not fed_data: return await message.reply_text(await self.bot.text( chat_id, "fed-no-fed-chat")) if not self.is_fed_admin(fed_data, banner.id): return await message.reply_text(await self.bot.text( chat_id, "fed-admin-only")) to_unban, _ = extract_user_and_text(message) if not to_unban: return await message.reply_text(await self.bot.text( chat_id, "fed-no-ban-user")) user = await extract_user(self.bot.client, to_unban) if str(user.id) not in fed_data.get("banned").keys(): return await message.reply_text(await self.bot.text( chat_id, "fed-user-not-banned")) await self.fban_user(fed_data["_id"], user.id, ParsedChatMember(user).fullname, ban=False) text = await self.bot.text(chat_id, "fed-unban-info", fed_data["name"], banner.mention, user.mention, user.id) await message.reply_text(text) LOGGER.debug(f"Unfedban {user.id} on {fed_data['_id']}") for chats in fed_data["chats"]: await self.bot.client.unban_chat_member(chats, user.id) # send message to federation log flog = fed_data.get("log", None) if flog: await self.bot.client.send_message(flog, text, disable_web_page_preview=True)
async def stats(self, message): """Fetch user info""" chat_id = message.chat.id msg = await message.reply_text("Fetching user info...") user_id, _ = extract_user_and_text(message) if not user_id: user = message.from_user else: user = await extract_user(self.bot.client, user_id) text = f"**{'Bot' if user.is_bot else 'User'} Info**\n" text += f"**ID:** `{user.id}`\n" text += f"**DC ID: **`{user.dc_id if user.dc_id else 'N/A'}`\n" text += f"**First Name: **{user.first_name}\n" if user.last_name: text += f"**Last Name: **{user.last_name}\n" text += f"**Username: **@{user.username}\n" text += f"**Permanent user link: **{user.mention}\n" text += ( "**Number of profile pics: **" f"`{await self.bot.client.get_profile_photos_count(user.id)}`\n") if user.status: text += f"**Last seen: ** `{user.status}`\n" if user.id in self.bot.staff_id: if user.id == self.bot.staff["owner"]: text += "\nThis person is my **owner**!\nI would never do anything against him.\n" elif user.id in self.bot.staff.get("dev", []): text += "\nThis person is one of my **Devs**!\nNearly as powerfull as my owner.\n" else: text += "\nThis person is one of my **Sudo users**!\n" text += "Mostly imune from my restriction and he have a special access to many commands.\n" elif user.is_self: text += "\nI've seen them in every chats... wait it's me!!\nWow you're stalking me? 😂" user_db = await self.users_db.find_one({"_id": user.id}) if user_db: text += f"\nI've seen them on {len(user_db['chats'])} chats." if user.photo: file = await self.bot.client.download_media(user.photo.big_file_id) await self.bot.client.send_photo(chat_id, file, text) os.remove(file) else: await self.bot.client.send_message(chat_id, text) await msg.delete()
async def demote_fadmin(self, message): """Demote user to fed admin""" chat_id = message.chat.id if message.chat.type == "private": return await message.reply_text(await self.bot.text( chat_id, "err-chat-groups")) user_id = message.from_user.id to_demote, _ = extract_user_and_text(message) if not to_demote: return await message.reply_text(await self.bot.text( chat_id, "fed-no-demote-user")) demote_user = await extract_user(self.bot.client, to_demote) to_demote = demote_user.id fed_data = await self.get_fed_bychat(chat_id) if not fed_data: return await message.reply_text(await self.bot.text( chat_id, "fed-no-fed-chat")) if user_id != fed_data["owner"]: return await message.reply_text(await self.bot.text( chat_id, "fed-owner-only-demote")) if to_demote == fed_data["owner"]: return await message.reply_text(await self.bot.text( chat_id, "fed-already-owner")) if to_demote not in fed_data.get("admins", []): return await message.reply_text(await self.bot.text( chat_id, "fed-user-not-admin")) async with self.lock: await self.feds_db.update_one({"_id": fed_data["_id"]}, {"$pull": { "admins": to_demote }}) await message.reply_text(await self.bot.text(chat_id, "fed-demote-done")) if flog := fed_data.get("log", None): await self.bot.client.send_message( flog, "**New Fed Demotion**\n" f"**Fed: **{fed_data['name']}\n" f"**Promoted FedAdmin: {demote_user.mention}**\n" f"**User ID: **{to_demote}", )
async def fed_stats(self, message): """Get user status""" chat_id = message.chat.id user_id, _ = extract_user_and_text(message) if isinstance(user_id, str): user_id = (await extract_user(self.bot.client, user_id)).id if not user_id: user_id = message.from_user.id # <user_Id> <fed_id> if len(message.command) == 2 and message.command[0].isdigit(): fid = message.command[1] data = await self.get_fed(fid) if not data: return await message.reply_text(await self.bot.text( chat_id, "fed-invalid-id")) if message.command[0] in data.get("banned", {}): res = data["banned"][str(message.command[0])] await message.reply_text(await self.bot.text( chat_id, "fed-stat-banned", res["reason"], self.parse_date(res["time"]))) else: return await message.reply_text(await self.bot.text( chat_id, "fed-stat-not-banned")) # <user_Id> else: data = await self.check_fban(user_id) if data: text = await self.bot.text(chat_id, "fed-stat-multi") async for bans in data: text += await self.bot.text( chat_id, "fed-stat-multi-info", bans["name"], bans["_id"], bans["nammed"][str(user_id)]["reason"], ) else: text = await self.bot.text(chat_id, "fed-stat-multi-not-banned") await message.reply_text(text)
async def fed_ban(self, message): """Fed ban a user""" chat_id = message.chat.id if message.chat.type == "private": return await message.reply_text(await self.bot.text( chat_id, "err-chat-groups")) banner = message.from_user fed_data = await self.get_fed_bychat(chat_id) if not fed_data: return await message.reply_text(await self.bot.text( chat_id, "fed-no-fed-chat")) if not self.is_fed_admin(fed_data, banner.id): return await message.reply_text(await self.bot.text( chat_id, "fed-admin-only")) to_ban, reason = extract_user_and_text(message) if not to_ban: return await message.reply_text(await self.bot.text( chat_id, "fed-no-ban-user")) user = await extract_user(self.bot.client, to_ban) user_id = user.id if user_id == self.bot.identifier: return await message.reply_text(await self.bot.text( chat_id, "fed-ban-self")) if self.is_fed_admin(fed_data, user_id): return await message.reply_text(await self.bot.text( chat_id, "fed-ban-owner")) if user_id in self.bot.staff_id or user_id in (777000, 1087968824): return await message.reply_text(await self.bot.text( chat_id, "fed-ban-protected")) if not reason: reason = "No reason given." update = False if str(user_id) in fed_data.get("banned", {}).keys(): update = True banned_user = ParsedChatMember(user) await self.fban_user(fed_data["_id"], user_id, banned_user.fullname, reason, True) if update: text = await self.bot.text( chat_id, "fed-ban-info-update", fed_data["name"], banner.mention, banned_user.mention, user_id, fed_data["banned"][str(user_id)]["reason"], reason, ) else: text = await self.bot.text( chat_id, "fed-ban-info", fed_data["name"], banner.mention, banned_user.mention, user_id, reason, ) await message.reply_text(text, disable_web_page_preview=True) LOGGER.debug(f"New fedban {user_id} on {fed_data['id']}") for chats in fed_data["chats"]: await self.bot.client.kick_chat_member(chats, user_id)