# -*- coding: utf-8 -*- # Module author: @ftgmodulesbyfl1yd from telethon.errors import UserAdminInvalidError from telethon.tl.functions.channels import EditBannedRequest from telethon.tl.types import ChatBannedRights from .. import loader, utils @loader.tds class WarnsMod(loader.Module): """Система предупреждений.""" strings = {"name": "Warns"} async def client_ready(self, client, db): self.db = db async def warncmd(self, message): """Выдать варн. Используй: .warn <@ или реплай>.""" if message.is_private: return await message.edit("Это не чат!") chat = await message.get_chat() if not chat.admin_rights and not chat.creator: return await message.edit("Я не админ здесь.") else: if not chat.admin_rights.ban_users: return await message.edit("У меня нет нужных прав.") warns = self.db.get("Warns", "warns", {}) args = utils.get_args(message) reply = await message.get_reply_message() chatid = str(message.chat_id) reason = "Необоснованно" if not args and not reply: return await message.edit("Нет аргументов или реплая.") if reply: user = await message.client.get_entity(reply.sender_id) args = utils.get_args_raw(message) if args: reason = args else: user = await message.client.get_entity( args[0] if not args[0].isnumeric() else int(args[0]) ) if args: if len(args) == 1: args = utils.get_args_raw(message) user = await message.client.get_entity( args if not args.isnumeric() else int(args) ) elif len(args) >= 2: reason = utils.get_args_raw(message).split(" ", 1)[1] userid = str(user.id) me = await message.client.get_me() if me.id == user.id: return await message.edit("Ты не можешь себе давать предупреждение!") if chatid not in warns: warns.update({chatid: {"limit": 3, "action": "ban"}}) if userid not in warns[chatid]: warns[chatid].update({userid: []}) if not args and not reply: return await message.edit("Нет аргументов или реплая.") warns[chatid][userid].append(reason) count = len(warns[chatid][userid]) if count == warns[chatid]["limit"]: warns[chatid].pop(userid) self.db.set("Warns", "warns", warns) try: if warns[chatid]["action"] == "kick": await message.client.kick_participant(int(chatid), user.id) elif warns[chatid]["action"] == "ban": await message.client( EditBannedRequest( int(chatid), user.id, ChatBannedRights(until_date=None, view_messages=True), ) ) elif warns[chatid]["action"] == "mute": await message.client( EditBannedRequest( int(chatid), user.id, ChatBannedRights(until_date=True, send_messages=True), ) ) except UserAdminInvalidError: return await message.edit("У меня нет достаточных прав.") else: return await message.edit( f"{user.first_name} получил {count}/{warns[chatid]['limit']} предупреждения, и был ограничен в чате." ) self.db.set("Warns", "warns", warns) await message.edit( f"{user.first_name} получил {count}/{warns[chatid]['limit']} предупреждений." + (f"\nПричина: {reason}." if reason != "Необоснованно" else "") ) async def warnslimitcmd(self, message): # sourcery skip: last-if-guard """Установить лимит предупреждений. Используй: .warnslimit <кол-во:int>.""" if message.is_private: return await message.edit("Это не чат!") warns = self.db.get("Warns", "warns", {}) args = utils.get_args_raw(message) chatid = str(message.chat_id) if chatid not in warns: warns.update({chatid: {"limit": 3}}) if not args: return await message.edit( f"Лимит предупреждений в этом чате: {warns[chatid]['limit']}" ) try: warns[chatid].update({"limit": int(args)}) self.db.set("Warns", "warns", warns) return await message.edit( f"Лимит предупреждений в этом чате был установлен на: {warns[chatid]['limit']}" ) except ValueError: return await message.edit("Значение должно быть числом.") async def warnscmd(self, message): """Посмотреть кол-во варнов. Используй: .warns <@ или реплай> или .""" if message.is_private: return await message.edit("Это не чат!") args = utils.get_args_raw(message) reply = await message.get_reply_message() chatid = str(message.chat_id) warns = self.db.get("Warns", "warns", {}) if not args and not reply: return await message.edit("Нет аргументов или реплая.") if args == "list": users = "" try: for _ in warns[chatid]: if _ not in ["limit", "action"]: user = await message.client.get_entity(int(_)) users += f"• {user.first_name} | [{_}]\n" return await message.edit( f"Список тех, кто получил предупреждения:\n\n{users}" ) except KeyError: return await message.edit( "В этом чате никто не получал предупреждения." ) try: if args: user = await message.client.get_entity( int(args) if args.isnumeric() else args ) else: user = await message.client.get_entity(reply.sender_id) userid = str(user.id) except ValueError: return await message.edit("Не удалось найти этого пользователя.") try: if userid not in warns[chatid]: return await message.edit( "Этот пользователь не получал предупреждения." ) msg = "".join( f"{count}) {_}\n" for count, _ in enumerate(warns[chatid][userid], start=1) ) return await message.edit( f'Предупреждения {user.first_name}:\n\n{msg}' ) except KeyError: return await message.edit( f'У {user.first_name} нет предупреждений.' ) async def swarncmd(self, message): """Изменить режим ограничения. Используй: .swarn .""" if message.is_private: return await message.edit("Это не чат!") args = utils.get_args_raw(message) chatid = str(message.chat_id) warns = self.db.get("Warns", "warns", {}) if chatid not in warns: warns.update({chatid: {"action": "ban"}}) if args: if args == "kick": warns[chatid].update({"action": "kick"}) elif args == "ban": warns[chatid].update({"action": "ban"}) elif args == "mute": warns[chatid].update({"action": "mute"}) elif args == "none": warns[chatid].update({"action": "none"}) else: return await message.edit( "Такого режима нет в списке.\nДоступные режимы: kick/ban/mute/none." ) self.db.set("AntiMention", "action", warns) return await message.edit( f"Теперь при достижения лимита предупреждений будет выполняться действие: {warns[chatid]['action']}." ) else: return await message.edit( f"При достижения лимита предупреждений будет выполняться действие: {warns[chatid]['action']}." ) async def clearwarnscmd(self, message): """Очистить все варны. Используй: .clearwarns <@ или реплай>.""" if message.is_private: return await message.edit("Это не чат!") args = utils.get_args_raw(message) reply = await message.get_reply_message() chatid = str(message.chat_id) warns = self.db.get("Warns", "warns", {}) if not args and not reply: return await message.edit("Нет аргументов или реплая.") try: if args: user = await message.client.get_entity( int(args) if args.isnumeric() else args ) else: user = await message.client.get_entity(reply.sender_id) userid = str(user.id) except ValueError: return await message.edit("Не удалось найти этого пользователя.") try: warns[chatid][userid].pop() if len(warns[chatid][userid]) == 0: warns[chatid].pop(userid) self.db.set("Warns", "warns", warns) return await message.edit( f'У {user.first_name} удалено последнее предупреждение.' ) except KeyError: return await message.edit( f'У {user.first_name} нет предупреждений.' )