async def _(event): if event.is_private: return user = event.sender # type: Optional[User] chat = event.chat_id # type: Optional[Chat] # ignore admins and owner if await is_register_admin(chat, user.id) or user.id == OWNER_ID: sql.update_flood(chat, None) return should_ban = sql.update_flood(chat, user.id) if not should_ban: return try: getmode, getvalue = sql.get_flood_setting(chat) if getmode == 1: await tbot(EditBannedRequest(chat, user.id, BANNED_RIGHTS)) execstrings = "Banned" elif getmode == 2: await tbot.kick_participant(chat, user.id) execstrings = "Kicked" elif getmode == 3: await tbot(EditBannedRequest(chat, user.id, MUTE_RIGHTS)) execstrings = "Muted" elif getmode == 4: bantime = await extract_time(event, getvalue) NEW_RIGHTS = ChatBannedRights( until_date=bantime, view_messages=True, send_messages=True, send_media=True, send_stickers=True, send_gifs=True, send_games=True, send_inline=True, embed_links=True, ) await tbot(EditBannedRequest(chat, user.id, NEW_RIGHTS)) execstrings = "Banned for {}".format(getvalue) elif getmode == 5: mutetime = await extract_time(event, getvalue) print(mutetime) NEW_RIGHTS = ChatBannedRights(until_date=mutetime, send_messages=True) await tbot(EditBannedRequest(chat, user.id, NEW_RIGHTS)) execstrings = "Muted for {}".format(getvalue) await event.reply("Spammer Detected !\n{}!".format(execstrings)) except Exception: await event.reply( "I can't restrict people here, give me permissions first! Until then, I'll disable anti-flood." ) sql.set_flood(chat, 0)
async def _(event): if event.is_private: return if event.is_group: if not await can_change_info(message=event): return chat_id = event.chat_id args = event.pattern_match.group(1) if not args: await event.reply( ( "Use `/setflood number` to enable anti-flood.\nOr use `/setflood off` to disable antiflood!." ), parse_mode="markdown", ) return if args == "off": sql.set_flood(chat_id, 0) await event.reply("Antiflood has been disabled.") elif args.isdigit(): amount = int(args) if amount <= 0: sql.set_flood(chat_id, 0) await event.reply("Antiflood has been disabled.") elif amount < 3: await event.reply( "Antiflood must be either 0 (disabled) or number greater than 2!", ) return else: sql.set_flood(chat_id, amount) await event.reply( "Successfully updated anti-flood limit to {}!".format(amount) ) else: await event.reply("Invalid argument please use a number or 'off'")
def set_flood(update, context) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if len(args) >= 1: val = args[0].lower() if val == "off" or val == "no" or val == "0": sql.set_flood(chat_id, 0) text = message.reply_text("Antiflood has been disabled.") send_message(update.effective_message, text, parse_mode="markdown") elif val.isdigit(): amount = int(val) if amount <= 0: sql.set_flood(chat_id, 0) text = message.reply_text("Antiflood has been disabled.") return ("<b>{}:</b>" "\n#SETFLOOD" "\n<b>Admin:</b> {}" "\nDisable antiflood.".format( html.escape(chat_name), mention_html(user.id, user.first_name))) if amount < 3: send_message( update.effective_message, "Antiflood must be either 0 (disabled) or number greater than 3!", ) return "" sql.set_flood(chat_id, amount) text = message.reply_text( "Successfully updated anti-flood limit to {}!".format(amount)) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#SETFLOOD" "\n<b>Admin:</b> {}" "\nSet antiflood to <code>{}</code>.".format( html.escape(chat_name), mention_html(user.id, user.first_name), amount, )) else: message.reply_text( "Invalid argument please use a number, 'off' or 'no'") else: message.reply_text( ("Use `/setflood number` to enable anti-flood.\nOr use `/setflood off` to disable antiflood!." ), parse_mode="markdown", ) return ""
def check_flood(update, context) -> str: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] if not user: # ignore channels return "" # ignore admins if is_user_admin(chat, user.id): sql.update_flood(chat.id, None) return "" should_ban = sql.update_flood(chat.id, user.id) if not should_ban: return "" try: getmode, getvalue = sql.get_flood_setting(chat.id) if getmode == 1: chat.kick_member(user.id) execstrings = "Banned" tag = "BANNED" elif getmode == 2: chat.kick_member(user.id) chat.unban_member(user.id) execstrings = "Kicked" tag = "KICKED" elif getmode == 3: context.bot.restrict_chat_member( chat.id, user.id, permissions=ChatPermissions(can_send_messages=False)) execstrings = "Muted" tag = "MUTED" elif getmode == 4: bantime = extract_time(msg, getvalue) chat.kick_member(user.id, until_date=bantime) execstrings = "Banned for {}".format(getvalue) tag = "TBAN" elif getmode == 5: mutetime = extract_time(msg, getvalue) context.bot.restrict_chat_member( chat.id, user.id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False), ) execstrings = "Muted for {}".format(getvalue) tag = "TMUTE" send_message( update.effective_message, "Great, I like to leave flooding to natural disasters but you, " "you were just a disappointment. {}!".format(execstrings), ) return ("<b>{}:</b>" "\n#{}" "\n<b>User:</b> {}" "\nFlooded the group.".format( tag, html.escape(chat.title), mention_html(user.id, user.first_name))) except BadRequest: msg.reply_text( "I can't restrict people here, give me permissions first! Until then, I'll disable anti-flood." ) sql.set_flood(chat.id, 0) return ( "<b>{}:</b>" "\n#INFO" "\nDon't have enough permission to restrict users so automatically disabled anti-flood" .format(chat.title))