def blacklist_mode(update, context): chat = update.effective_chat user = update.effective_user msg = update.effective_message args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command can be only used in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if args: if args[0].lower() in ["off", "nothing", "no"]: settypeblacklist = "do nothing" sql.set_blacklist_strength(chat_id, 0, "0") elif args[0].lower() in ["del", "delete"]: settypeblacklist = "delete blacklisted message" sql.set_blacklist_strength(chat_id, 1, "0") elif args[0].lower() == "warn": settypeblacklist = "warn the sender" sql.set_blacklist_strength(chat_id, 2, "0") elif args[0].lower() == "mute": settypeblacklist = "mute the sender" sql.set_blacklist_strength(chat_id, 3, "0") elif args[0].lower() == "kick": settypeblacklist = "kick the sender" sql.set_blacklist_strength(chat_id, 4, "0") elif args[0].lower() == "ban": settypeblacklist = "ban the sender" sql.set_blacklist_strength(chat_id, 5, "0") elif args[0].lower() == "tban": if len(args) == 1: teks = """It looks like you tried to set time value for blacklist but you didn't specified time; Try, `/blacklistmode tban <timevalue>`. Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" restime = extract_time(msg, args[1]) if not restime: teks = """Invalid time value! Example of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" settypeblacklist = "temporarily ban for {}".format(args[1]) sql.set_blacklist_strength(chat_id, 6, str(args[1])) elif args[0].lower() == "tmute": if len(args) == 1: teks = """It looks like you tried to set time value for blacklist but you didn't specified time; try, `/blacklistmode tmute <timevalue>`. Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" restime = extract_time(msg, args[1]) if not restime: teks = """Invalid time value! Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" settypeblacklist = "temporarily mute for {}".format(args[1]) sql.set_blacklist_strength(chat_id, 7, str(args[1])) else: send_message( update.effective_message, "I only understand: off/del/warn/ban/kick/mute/tban/tmute!", ) return "" if conn: text = "Changed blacklist mode: `{}` in *{}*!".format( settypeblacklist, chat_name) else: text = "Changed blacklist mode: `{}`!".format(settypeblacklist) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>\n" "<b>Admin:</b> {}\n" "Changed the blacklist mode. will {}.".format( html.escape(chat.title), mention_html(user.id, html.escape(user.first_name)), settypeblacklist, )) else: getmode, getvalue = sql.get_blacklist_setting(chat.id) if getmode == 0: settypeblacklist = "do nothing" elif getmode == 1: settypeblacklist = "delete" elif getmode == 2: settypeblacklist = "warn" elif getmode == 3: settypeblacklist = "mute" elif getmode == 4: settypeblacklist = "kick" elif getmode == 5: settypeblacklist = "ban" elif getmode == 6: settypeblacklist = "temporarily ban for {}".format(getvalue) elif getmode == 7: settypeblacklist = "temporarily mute for {}".format(getvalue) if conn: text = "Current blacklistmode: *{}* in *{}*.".format( settypeblacklist, chat_name) else: text = "Current blacklistmode: *{}*.".format(settypeblacklist) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) return ""
def allow_connections(update, context) -> str: chat = update.effective_chat args = context.args if chat.type != chat.PRIVATE: if len(args) >= 1: var = args[0] if var == "no": sql.set_allow_connect_to_chat(chat.id, False) send_message( update.effective_message, "Connection has been disabled for this chat", ) elif var == "yes": sql.set_allow_connect_to_chat(chat.id, True) send_message( update.effective_message, "Connection has been enabled for this chat", ) else: send_message( update.effective_message, "Please enter `yes` or `no`!", parse_mode=ParseMode.MARKDOWN, ) else: get_settings = sql.allow_connect_to_chat(chat.id) if get_settings: send_message( update.effective_message, "Connections to this group are *Allowed* for members!", parse_mode=ParseMode.MARKDOWN, ) else: send_message( update.effective_message, "Connection to this group are *Not Allowed* for members!", parse_mode=ParseMode.MARKDOWN, ) else: send_message(update.effective_message, "This command is for group only. Not in PM!")
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 and whitelists if is_user_admin(chat, user.id) or user.id in WHITELIST_USERS: sql.update_flood(chat.id, None) return "" # ignore approved users if is_approved(chat.id, 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, "Beep Boop! Boop Beep!\n{}!".format(execstrings)) return ("<b>{}:</b>" "\n#{}" "\n<b>User:</b> {}" "\nFlooded the group.".format( tag, html.escape(chat.title), mention_html(user.id, html.escape(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))
def unblacklist(update, context): msg = update.effective_message chat = update.effective_chat user = update.effective_user words = msg.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1] to_unblacklist = list({ trigger.strip() for trigger in text.split("\n") if trigger.strip() }) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_blacklist(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: send_message( update.effective_message, "Removed <code>{}</code> from blacklist in <b>{}</b>!". format(html.escape(to_unblacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message(update.effective_message, "This is not a blacklist trigger!") elif successful == len(to_unblacklist): send_message( update.effective_message, "Removed <code>{}</code> from blacklist in <b>{}</b>!".format( successful, html.escape(chat_name)), parse_mode=ParseMode.HTML, ) elif not successful: send_message( update.effective_message, "None of these triggers exist so it can't be removed.", parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Removed <code>{}</code> from blacklist. {} did not exist, " "so were not removed.".format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Tell me which words you would like to remove from blacklist!", )
def set_flood_mode(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: 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 = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if args: if args[0].lower() == "ban": settypeflood = "ban" sql.set_flood_strength(chat_id, 1, "0") elif args[0].lower() == "kick": settypeflood = "kick" sql.set_flood_strength(chat_id, 2, "0") elif args[0].lower() == "mute": settypeflood = "mute" sql.set_flood_strength(chat_id, 3, "0") elif args[0].lower() == "tban": if len(args) == 1: teks = """It looks like you tried to set time value for antiflood but you didn't specified time; Try, `/setfloodmode tban <timevalue>`. Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return settypeflood = "tban for {}".format(args[1]) sql.set_flood_strength(chat_id, 4, str(args[1])) elif args[0].lower() == "tmute": if len(args) == 1: teks = ( update.effective_message, """It looks like you tried to set time value for antiflood but you didn't specified time; Try, `/setfloodmode tmute <timevalue>`. Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""", ) send_message(update.effective_message, teks, parse_mode="markdown") return settypeflood = "tmute for {}".format(args[1]) sql.set_flood_strength(chat_id, 5, str(args[1])) else: send_message(update.effective_message, "I only understand ban/kick/mute/tban/tmute!") return if conn: text = msg.reply_text( "Exceeding consecutive flood limit will result in {} in {}!". format(settypeflood, chat_name)) else: text = msg.reply_text( "Exceeding consecutive flood limit will result in {}!".format( settypeflood)) return ("<b>{}:</b>\n" "<b>Admin:</b> {}\n" "Has changed antiflood mode. User will {}.".format( settypeflood, html.escape(chat.title), mention_html(user.id, html.escape(user.first_name)), )) else: getmode, getvalue = sql.get_flood_setting(chat.id) if getmode == 1: settypeflood = "ban" elif getmode == 2: settypeflood = "kick" elif getmode == 3: settypeflood = "mute" elif getmode == 4: settypeflood = "tban for {}".format(getvalue) elif getmode == 5: settypeflood = "tmute for {}".format(getvalue) if conn: text = msg.reply_text( "Sending more messages than flood limit will result in {} in {}." .format(settypeflood, chat_name)) else: text = msg.reply_text( "Sending more message than flood limit will result in {}.". format(settypeflood)) return ""
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 conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: 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 in ["off", "no", "0"]: sql.set_flood(chat_id, 0) if conn: text = message.reply_text( "Antiflood has been disabled in {}.".format(chat_name)) else: text = message.reply_text("Antiflood has been disabled.") elif val.isdigit(): amount = int(val) if amount <= 0: sql.set_flood(chat_id, 0) if conn: text = message.reply_text( "Antiflood has been disabled in {}.".format(chat_name)) else: 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, html.escape(user.first_name)), )) elif amount <= 3: send_message( update.effective_message, "Antiflood must be either 0 (disabled) or number greater than 3!", ) return "" else: sql.set_flood(chat_id, amount) if conn: text = message.reply_text( "Anti-flood has been set to {} in chat: {}".format( amount, chat_name)) else: text = message.reply_text( "Successfully updated anti-flood limit to {}!".format( amount)) return ("<b>{}:</b>" "\n#SETFLOOD" "\n<b>Admin:</b> {}" "\nSet antiflood to <code>{}</code>.".format( html.escape(chat_name), mention_html(user.id, html.escape(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 add_blackliststicker(update: Update, context: CallbackContext): bot = context.bot msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) bot = context.bot conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1].replace("https://t.me/addstickers/", "") to_blacklist = list({ trigger.strip() for trigger in text.split("\n") if trigger.strip() }) added = 0 for trigger in to_blacklist: try: get = bot.getStickerSet(trigger) sql.add_to_stickers(chat_id, trigger.lower()) added += 1 except BadRequest: send_message( update.effective_message, "Sticker `{}` can not be found!".format(trigger), parse_mode="markdown", ) if added == 0: return if len(to_blacklist) == 1: send_message( update.effective_message, "Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!" .format(html.escape(to_blacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "<code>{}</code> stickers added to blacklist sticker in <b>{}</b>!" .format(added, html.escape(chat_name)), parse_mode=ParseMode.HTML, ) elif msg.reply_to_message: added = 0 trigger = msg.reply_to_message.sticker.set_name if trigger is None: send_message(update.effective_message, "Sticker is invalid!") return try: get = bot.getStickerSet(trigger) sql.add_to_stickers(chat_id, trigger.lower()) added += 1 except BadRequest: send_message( update.effective_message, "Sticker `{}` can not be found!".format(trigger), parse_mode="markdown", ) if added == 0: return send_message( update.effective_message, "Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!" .format(trigger, html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Tell me what stickers you want to add to the blacklist.", )
def blacklist_mode(update: Update, context: CallbackContext): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] bot, args = context.bot, context.args conn = connected(bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if args: if args[0].lower() in ["off", "nothing", "no"]: settypeblacklist = "turn off" sql.set_blacklist_strength(chat_id, 0, "0") elif args[0].lower() in ["del", "delete"]: settypeblacklist = "left, the message will be deleted" sql.set_blacklist_strength(chat_id, 1, "0") elif args[0].lower() == "warn": settypeblacklist = "warned" sql.set_blacklist_strength(chat_id, 2, "0") elif args[0].lower() == "mute": settypeblacklist = "muted" sql.set_blacklist_strength(chat_id, 3, "0") elif args[0].lower() == "kick": settypeblacklist = "kicked" sql.set_blacklist_strength(chat_id, 4, "0") elif args[0].lower() == "ban": settypeblacklist = "banned" sql.set_blacklist_strength(chat_id, 5, "0") elif args[0].lower() == "tban": if len(args) == 1: teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blstickermode tban <timevalue>`. Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return settypeblacklist = "temporary banned for {}".format(args[1]) sql.set_blacklist_strength(chat_id, 6, str(args[1])) elif args[0].lower() == "tmute": if len(args) == 1: teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blstickermode tmute <timevalue>`. Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return settypeblacklist = "temporary muted for {}".format(args[1]) sql.set_blacklist_strength(chat_id, 7, str(args[1])) else: send_message( update.effective_message, "I only understand off/del/warn/ban/kick/mute/tban/tmute!", ) return if conn: text = "Blacklist sticker mode changed, users will be `{}` at *{}*!".format( settypeblacklist, chat_name) else: text = "Blacklist sticker mode changed, users will be `{}`!".format( settypeblacklist) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>\n" "<b>Admin:</b> {}\n" "Changed sticker blacklist mode. users will be {}.".format( html.escape(chat.title), mention_html(user.id, html.escape(user.first_name)), settypeblacklist, )) else: getmode, getvalue = sql.get_blacklist_setting(chat.id) if getmode == 0: settypeblacklist = "not active" elif getmode == 1: settypeblacklist = "delete" elif getmode == 2: settypeblacklist = "warn" elif getmode == 3: settypeblacklist = "mute" elif getmode == 4: settypeblacklist = "kick" elif getmode == 5: settypeblacklist = "ban" elif getmode == 6: settypeblacklist = "temporarily banned for {}".format(getvalue) elif getmode == 7: settypeblacklist = "temporarily muted for {}".format(getvalue) if conn: text = "Blacklist sticker mode is currently set to *{}* in *{}*.".format( settypeblacklist, chat_name) else: text = "Blacklist sticker mode is currently set to *{}*.".format( settypeblacklist) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) return ""
def unblackliststicker(update: Update, context: CallbackContext): bot = context.bot msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) bot = context.bot conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1].replace("https://t.me/addstickers/", "") to_unblacklist = list({ trigger.strip() for trigger in text.split("\n") if trigger.strip() }) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_stickers(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!" .format(html.escape(to_unblacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message(update.effective_message, "This sticker is not on the blacklist...!") elif successful == len(to_unblacklist): send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!". format(successful, html.escape(chat_name)), parse_mode=ParseMode.HTML, ) elif not successful: send_message( update.effective_message, "None of these stickers exist, so they cannot be removed.", parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist. {} did not exist, so it's not deleted." .format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML, ) elif msg.reply_to_message: trigger = msg.reply_to_message.sticker.set_name if trigger is None: send_message(update.effective_message, "Sticker is invalid!") return success = sql.rm_from_stickers(chat_id, trigger.lower()) if success: send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!". format(trigger, chat_name), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "{} not found on blacklisted stickers...!".format(trigger), ) else: send_message( update.effective_message, "Tell me what stickers you want to add to the blacklist.", )