Ejemplo n.º 1
0
def ban(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]

    banType = 0
    userIds = extract_multiple_users(message, args)
    allTrue = True
    for id in userIds:
        if id is None or not isinstance(id, int) or id == "":
            allTrue = False
    if len(userIds) > 1 and allTrue:
        banType = 1
    if banType == 1:
        if len(userIds) == 0:
            message.reply_text("There was a problem parsing the user IDs")
            return ""
        log = "#BAN MUL\n Admin: " + user.first_name + "\n"
        for id in userIds:
            try:
                integerID = int(id)
                member = chat.get_member(integerID)
                if is_user_ban_protected(chat, integerID, member):
                    message.reply_text("Can't ban " + str(integerID) +
                                       ", user is ban protected.")
                    continue
                if integerID == bot.id:
                    message.reply_text("I wont ban myself... " +
                                       str(integerID) + " is my ID.")
                    continue
                if integerID == 777000 or integerID == 1087968824:
                    message.reply_text(
                        str(integerID) +
                        " is an account reserved for telegram, I cannot ban it"
                    )
                    continue
                try:
                    chat.kick_member(integerID)
                    keyboard = []
                    # bot.send_sticker(update.effective_chat.id, BAN_STICKER)  # ban sticker
                    reply = "{} has been banned!".format(
                        mention_html(member.user.id, member.user.first_name))
                    message.reply_text(reply,
                                       reply_markup=keyboard,
                                       parse_mode=ParseMode.HTML)
                    log += "ID: " + str(member.user.id) + "\n"
                except BadRequest as excp:
                    LOGGER.warning(update)
                    LOGGER.exception(
                        "ERROR banning user %s in chat %s (%s) due to %s",
                        integerID, chat.title, chat.id, excp.message)
                    message.reply_text("Well damn, I can't ban " +
                                       str(integerID))
            except ValueError:
                message.reply_text("Error parsing the ID: " + id +
                                   " is not a valid user ID")
            except BadRequest as excp:
                if excp.message == "User not found":
                    message.reply_text("User " + str(integerID) +
                                       "has not been found")
                    continue
                else:
                    raise
        return log

    else:
        user_id, reason = extract_user_and_text(message, args)
        if not user_id or int(user_id) == 777000 or int(user_id) == 1087968824:
            message.reply_text("You don't seem to be referring to a user.")
            return ""
        try:
            member = chat.get_member(user_id)
        except BadRequest as excp:
            if excp.message == "User not found":
                message.reply_text("404 - user not found")
                return ""
            else:
                raise
        if is_user_ban_protected(chat, user_id, member):
            message.reply_text("Can't do that, user is admin..")
            return ""
        if user_id == bot.id:
            message.reply_text("hahahahahahaha nice try.. nope")
            return ""
        log = "<b>{}:</b>" \
              "\n#BANNED" \
              "\n<b>Admin:</b> {}" \
              "\n<b>User:</b> {} (<code>{}</code>)".format(html.escape(chat.title),
                                                           mention_html(user.id, user.first_name),
                                                           mention_html(member.user.id, member.user.first_name),
                                                           member.user.id)
        if reason:
            log += "\n<b>Reason:</b> {}".format(reason)
        try:
            chat.kick_member(user_id)
            keyboard = []
            bot.send_sticker(update.effective_chat.id,
                             BAN_STICKER)  # ban sticker
            reply = "{} has been banned!".format(
                mention_html(member.user.id, member.user.first_name))
            message.reply_text(reply,
                               reply_markup=keyboard,
                               parse_mode=ParseMode.HTML)
            return log
        except BadRequest as excp:
            if excp.message == "Reply message not found":
                # Do not reply
                message.reply_text('Banned!', quote=False)
                return log
            else:
                LOGGER.warning(update)
                LOGGER.exception(
                    "ERROR banning user %s in chat %s (%s) due to %s", user_id,
                    chat.title, chat.id, excp.message)
                message.reply_text("Well damn, I can't ban that user.")
        return ""
Ejemplo n.º 2
0
def kick(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]

    banType = 0
    userIds = extract_multiple_users(message, args)
    allTrue = True
    for id in userIds:
        if id is None or not isinstance(id, int) or id == "":
            allTrue = False
    if len(userIds) > 1 and allTrue:
        banType = 1

    if banType == 1:
        if len(userIds) == 0:
            message.reply_text("There was a problem parsing the user IDs")
            return ""
        log = "#KICK_MUL\n Admin: " + user.first_name + "\n"
        for id in userIds:
            try:
                integerID = int(id)
                member = chat.get_member(integerID)
                if is_user_ban_protected(chat, integerID, member):
                    message.reply_text("Can't kick " + str(integerID) +
                                       ", user is ban protected.")
                    continue
                if integerID == bot.id:
                    message.reply_text("I wont ban myself... " +
                                       str(integerID) + " is my ID.")
                    continue
                if integerID == 777000 or integerID == 1087968824:
                    message.reply_text(
                        str(integerID) +
                        " is an account reserved for telegram, I cannot kick it"
                    )
                    continue
                res = chat.unban_member(integerID)
                if res:
                    #bot.send_sticker(chat.id, BAN_STICKER)  # ban sticker
                    keyboard = []
                    reply = "{} has been kicked!".format(
                        mention_html(member.user.id, member.user.first_name))
                    message.reply_text(reply,
                                       reply_markup=keyboard,
                                       parse_mode=ParseMode.HTML)
                    log += "ID: " + str(member.user.id) + "\n"
                else:
                    message.reply_text("Well damn, I can't kick that user.")
            except ValueError:
                message.reply_text("Error parsing the ID: " + id +
                                   " is not a valid user ID")
            except BadRequest as excp:
                if excp.message == "User not found":
                    message.reply_text("User " + str(integerID) +
                                       "has not been found")
                    continue
                else:
                    raise
        return log
    else:
        user_id, reason = extract_user_and_text(message, args)
        if not user_id or int(user_id) == 777000 or int(user_id) == 1087968824:
            message.reply_text("You don't seem to be referring to a user.")
            return ""
        try:
            member = chat.get_member(user_id)
        except BadRequest as excp:
            if excp.message == "User not found":
                message.reply_text("404 - user not found")
                return ""
            else:
                raise

        if is_user_ban_protected(chat, user_id):
            message.reply_text(
                "I'm not gonna kick an admin... Though I reckon it'd be pretty funny."
            )
            return ""

        if user_id == bot.id:
            message.reply_text("hahahahahahaha nice try.. nope")
            return ""

        res = chat.unban_member(user_id)  # unban on current user = kick
        if res:
            bot.send_sticker(chat.id, BAN_STICKER)  # ban sticker
            keyboard = []
            reply = "{} has been kicked!".format(
                mention_html(member.user.id, member.user.first_name))
            message.reply_text(reply,
                               reply_markup=keyboard,
                               parse_mode=ParseMode.HTML)

            log = "<b>{}:</b>" \
                  "\n#KICKED" \
                  "\n<b>Admin:</b> {}" \
                  "\n<b>User:</b> {} (<code>{}</code>)".format(html.escape(chat.title),
                                                               mention_html(user.id, user.first_name),
                                                               mention_html(member.user.id, member.user.first_name),
                                                               member.user.id)
            if reason:
                log += "\n<b>Reason:</b> {}".format(reason)

            return log

        else:
            message.reply_text("Well damn, I can't kick that user.")

        return ""