Example #1
0
def gbanstat(update: Update, context: CallbackContext):
    chat = update.effective_chat
    args = update.effective_message.text.split(" ")
    args_option = ""

    if len(args) > 1:
        args_option = args[1].lower()

    if len(args) >= 2:
        if args_option != "" and args_option in ["on", "yes"]:
            sql.enable_gbans(update.effective_chat.id)
            update.effective_message.reply_text(
                "I've enabled gbans for {}. This will help protect you "
                "from spammers, unsavoury characters, and "
                "the biggest trolls.".format(chat.title))
        elif args_option != "" and args_option in ["off", "no"]:
            sql.disable_gbans(update.effective_chat.id)
            update.effective_message.reply_text(
                "I've disabled gbans for {}. Global ban wont affect your users "
                "anymore. You'll be less protected from any trolls and spammers "
                "though!".format(chat.title))
    else:
        update.effective_message.reply_text(
            "Give me some arguments to choose a setting! on/off, yes/no!\n\n"
            "Your current setting is: {}\n"
            "When True, any gbans that happen will also happen in your group. "
            "When False, they won't, leaving you at the possible mercy of "
            "spammers.".format(sql.does_chat_gban(update.effective_chat.id)))
Example #2
0
def enforce_gban(update: Update, context: CallbackContext):
    # Not using @restrict handler to avoid spamming - just ignore if cant gban.
    if sql.does_chat_gban(
            update.effective_chat.id) and update.effective_chat.get_member(
                context.bot.id).can_restrict_members:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        msg = update.effective_message  # type: Optional[Message]
        gban_alert = sql.get_gban_alert(chat.id)
        new_members = update.effective_message.new_chat_members
        user_info = context.bot.get_chat_member(chat.id, user.id)

        if user and not is_user_admin(chat, user.id):
            check_and_ban(update, context, user.id)

        if msg.text:
            if user and not is_user_admin(chat, user.id):
                check_and_ban(update, context, user.id)

                if gban_alert:
                    gban_notification(update,
                                      context,
                                      user_info,
                                      should_message=True)

        elif msg.new_chat_members:
            for mem in new_members:
                user_info = context.bot.get_chat_member(chat.id, mem.id)
                welcome_gban(update, context, mem.id)
                if gban_alert:
                    gban_notification(update,
                                      context,
                                      user_info,
                                      should_message=True)

        elif new_members:
            for mem in new_members:
                welcome_gban(update, context, mem.id)
                if gban_alert:
                    gban_notification(update,
                                      context,
                                      user_info,
                                      should_message=True)
Example #3
0
def __chat_settings__(chat_id, user_id):
    return "This chat is enforcing *gbans*: `{}`.".format(
        sql.does_chat_gban(chat_id))
Example #4
0
def gban(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    user = update.effective_user  # type: Optional[User]
    args = message.text.split(" ")
    user_id, reason = extract_user_and_text(message, args)

    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return

    if int(user_id) in SUDO_USERS:
        message.reply_text(
            "You're playing with fire! Sudo war is catastrophic.")
        return

    if int(user_id) in SUPPORT_USERS:
        message.reply_text(
            "I can't global ban a support user! Only my creator can!")
        return

    if int(user_id) in SUPER_ADMINS:
        message.reply_text(
            "This is one of the super admin users appointed by the hierarchy. "
            "Therefore, I can't touch this user!")
        return

    if user_id == context.bot.id:
        message.reply_text("Only my creator can decide the fate!")
        return

    try:
        user_chat = context.bot.get_chat(user_id)
    except BadRequest as excp:
        message.reply_text(excp.message)
        return

    if user_chat.type != 'private':
        message.reply_text("That's not a user!")
        return

    if sql.is_user_gbanned(user_id):
        if not reason:
            msg = "User {} is already globally banned; I'd change the reason, " \
                  "but you haven't given me one...".format(mention_html(user_chat.id, user_chat.first_name
                                                                        or "Deleted Account"))
            message.reply_text(msg, parse_mode=ParseMode.HTML)
            return

        old_reason = sql.update_gban_reason(
            user_id, user_chat.username or user_chat.first_name, reason)
        user_id, new_reason = extract_user_and_text(message, args)

        if old_reason == new_reason:
            same_reason = "User {} has already been globally banned, with the " \
                          "exact same reason.".format(mention_html(user_chat.id, user_chat.first_name or
                                                                   "Deleted Account"))

            message.reply_text(same_reason, parse_mode=ParseMode.HTML)
            return

        if old_reason:
            banner = update.effective_user  # type: Optional[User]
            send_gban = "<b>Emendation of Global Ban</b>" \
                        "\n#GBAN" \
                        "\n<b>Status:</b> <code>Amended</code>" \
                        "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or
                                                                "Deleted Account"))

            if user_chat.last_name:
                send_gban += "\n<b>Surname:</b> {}".format(
                    mention_html(user_chat.id, user_chat.last_name))

            if user_chat.username:
                send_gban += "\n<b>Username:</b> @{}".format(
                    html.escape(user_chat.username))

            if user_chat:
                send_gban += "\n<b>ID:</b> <code>{}</code>".format(
                    user_chat.id)

            if banner.id in SUDO_USERS:
                send_gban += "\n<b>Sudo:</b> {}".format(
                    mention_html(banner.id, banner.first_name))

            if banner.id in SUPPORT_USERS:
                send_gban += "\n<b>Support:</b> {}".format(
                    mention_html(banner.id, banner.first_name))

            if reason:
                send_gban += "\n<b>Previous:</b> {}".format(old_reason)
                send_gban += "\n<b>Amended:</b> {}".format(new_reason)

            context.bot.send_message(chat_id=GBAN_LOG,
                                     text=send_gban,
                                     parse_mode=ParseMode.HTML)
            old_msg = "User {} is already globally banned, for the following reason:\n" \
                      "<code>{}</code>\n" \
                      "I've gone and updated it with your new reason!".format(mention_html(user_chat.id,
                                                                              user_chat.first_name or "Deleted Account"),
                                                                              old_reason)
            message.reply_text(old_msg, parse_mode=ParseMode.HTML)

        else:
            banner = update.effective_user  # type: Optional[User]
            send_gban = "<b>Emendation of Global Ban</b>" \
                        "\n#GBAN" \
                        "\n<b>Status:</b> <code>New reason</code>" \
                        "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or
                                                                "Deleted Account"))

            if user_chat.last_name:
                send_gban += "\n<b>Surname:</b> {}".format(
                    mention_html(user_chat.id, user_chat.last_name))

            if user_chat.username:
                send_gban += "\n<b>Username:</b> @{}".format(
                    html.escape(user_chat.username))

            if user_chat:
                send_gban += "\n<b>ID:</b> <code>{}</code>".format(
                    user_chat.id)

            if banner.id in SUDO_USERS:
                send_gban += "\n<b>Sudo:</b> {}".format(
                    mention_html(banner.id, banner.first_name))

            if banner.id in SUPPORT_USERS:
                send_gban += "\n<b>Support:</b> {}".format(
                    mention_html(banner.id, banner.first_name))

            if banner.id in SUPER_ADMINS:
                send_gban += "\n<b>Super Admin:</b> {}".format(
                    mention_html(banner.id, banner.first_name))

            if reason:
                send_gban += "\n<b>Reason:</b> {}".format(new_reason)

            context.bot.send_message(chat_id=GBAN_LOG,
                                     text=send_gban,
                                     parse_mode=ParseMode.HTML)
            msg = "User {} is already globally banned, but had no reason set; " \
                  "I've gone and updated it!".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))
            message.reply_text(msg, parse_mode=ParseMode.HTML)

        return

    starting = "Initiating global ban for {}...".format(
        mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

    message.reply_text(starting, parse_mode=ParseMode.HTML)

    banner = update.effective_user  # type: Optional[User]
    send_gban = "<b>Global Ban</b>" \
                "\n#GBAN" \
                "\n<b>Status:</b> <code>Enforced</code>" \
                "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

    if user_chat.last_name:
        send_gban += "\n<b>Surname:</b> {}".format(
            mention_html(user_chat.id, user_chat.last_name))

    if user_chat.username:
        send_gban += "\n<b>Username:</b> @{}".format(
            html.escape(user_chat.username))

    if user_chat:
        send_gban += "\n<b>ID:</b> <code>{}</code>".format(user_chat.id)

    if banner.id in SUDO_USERS:
        send_gban += "\n<b>Sudo:</b> {}".format(
            mention_html(banner.id, banner.first_name))

    if banner.id in SUPPORT_USERS:
        send_gban += "\n<b>Support:</b> {}".format(
            mention_html(banner.id, banner.first_name))

    if reason:
        send_gban += "\n<b>Reason:</b> {}".format(reason)

    context.bot.send_message(chat_id=GBAN_LOG,
                             text=send_gban,
                             parse_mode=ParseMode.HTML)
    sql.gban_user(user_id, user_chat.username or user_chat.first_name, reason)

    fban_id = update.effective_chat  # type: Optional[Chat]
    chats = get_users_by_chat(fban_id.id, user_id)
    for chat in chats:
        chat_id = chat.chat

        # Check if this group has disabled gbans
        if not sql.does_chat_gban(chat_id):
            continue

        try:
            context.bot.kick_chat_member(chat_id, user_id)
        except BadRequest as excp:
            if excp.message in GBAN_ERRORS:
                pass
            else:
                message.reply_text("Could not gban due to: {}".format(
                    excp.message))
                sql.ungban_user(user_id)
                return
        except TelegramError:
            pass

    blacklist_t = "User {} has been successfully globally banned".format(
        mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

    context.bot.send_message(chat_id=GBAN_LOG,
                             text=blacklist_t,
                             parse_mode=ParseMode.HTML)

    message.reply_text("User {} has been globally banned!".format(
        mention_html(user_chat.id, user_chat.first_name or "Deleted Account")),
                       parse_mode=ParseMode.HTML)
Example #5
0
def ungban(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    user = update.effective_user  # type: Optional[User]
    args = message.text.split(" ")

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return

    user_chat = context.bot.get_chat(user_id)
    if user_chat.type != 'private':
        message.reply_text("That's not a user!")
        return

    if not sql.is_user_gbanned(user_id):
        msg = "User {} is not globally banned!".format(
            mention_html(user_chat.id, user_chat.first_name
                         or "Deleted Account"))

        message.reply_text(msg, parse_mode=ParseMode.HTML)
        return

    banner = update.effective_user  # type: Optional[User]

    message.reply_text("Removing {} from global ban.".format(
        user_chat.first_name or "Deleted Account"))

    send_gban = "<b>Regression of Global Ban</b>" \
                "\n#GBAN" \
                "\n<b>Status:</b> <code>Ceased</code>" \
                "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

    if user_chat.last_name:
        send_gban += "\n<b>Surname:</b> {}".format(
            mention_html(user_chat.id, user_chat.last_name))

    if user_chat.username:
        send_gban += "\n<b>Username:</b> @{}".format(
            html.escape(user_chat.username))

    if user_chat:
        send_gban += "\n<b>ID:</b> <code>{}</code>".format(user_chat.id)

    if banner.id in SUDO_USERS:
        send_gban += "\n<b>Sudo:</b> {}".format(
            mention_html(banner.id, banner.first_name))

    if banner.id in SUPPORT_USERS:
        send_gban += "\n<b>Support:</b> {}".format(
            mention_html(banner.id, banner.first_name))

    context.bot.send_message(chat_id=GBAN_LOG,
                             text=send_gban,
                             parse_mode=ParseMode.HTML)
    unfban_id = update.effective_chat  # type: Optional[Chat]
    chats = get_users_by_chat(unfban_id.id, user_id)
    for chat in chats:
        chat_id = chat.chat

        # Check if this group has disabled gbans
        if not sql.does_chat_gban(chat_id):
            continue

        try:
            member = context.bot.get_chat_member(chat_id, user_id)
            if member.status == 'kicked':
                context.bot.unban_chat_member(chat_id, user_id)

        except BadRequest as excp:
            if excp.message in UNGBAN_ERRORS:
                pass
            else:
                message.reply_text("Could not un-gban due to: {}".format(
                    excp.message))
                context.bot.send_message(
                    OWNER_ID,
                    "Could not un-gban due to: {}".format(excp.message))
                return
        except TelegramError:
            pass

    sql.ungban_user(user_id)

    blacklist_t = "User {} has been ungbanned.".format(
        mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

    context.bot.send_message(chat_id=GBAN_LOG,
                             text=blacklist_t,
                             parse_mode=ParseMode.HTML)

    message.reply_text("User {} has been pardoned from global ban.".format(
        mention_html(user_chat.id, user_chat.first_name or "Deleted Account")),
                       parse_mode=ParseMode.HTML)