Пример #1
0
def warns(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    user_id = extract_user(message,
                           message.text.split(" ")) or update.effective_user.id
    member = chat.get_member(user_id)
    result = sql.get_warns(user_id, chat.id)
    num = 1

    if result and result[0] != 0:
        num_warns, reasons = result
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)

        if reasons:
            text = "User {} has {}/{} warnings, for the following reasons:".format(
                mention_html(member.user.id, member.user.first_name),
                num_warns, limit)
            for reason in reasons:
                text += "\n {}. {}".format(num, reason)
                num += 1

            msgs = split_message(text)
            for msg in msgs:
                update.effective_message.reply_text(msg,
                                                    parse_mode=ParseMode.HTML)

    else:
        update.effective_message.reply_text(
            "User {} hasn't got any warnings!".format(
                mention_html(member.user.id, member.user.first_name)),
            parse_mode=ParseMode.HTML)
Пример #2
0
def remove_warns(update: Update, context: CallbackContext) -> str:
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    args = message.text.split(" ")

    user_id = extract_user(message, args)

    if user_id:
        sql.remove_warn(user_id, chat.id)
        warned = chat.get_member(user_id).user
        rm_warnuser = chat.get_member(user_id)

        message.reply_text("Admin {} removed last warn for {}.".format(
            mention_html(user.id, user.first_name),
            mention_html(rm_warnuser.user.id, rm_warnuser.user.first_name)),
                           parse_mode=ParseMode.HTML)
        return "<b>{}:</b>" \
               "\n#UNWARN" \
               "\n<b>• Admin:</b> {}" \
               "\n<b>• User:</b> {}" \
               "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title),
                                                       mention_html(user.id, user.first_name),
                                                       mention_html(warned.id, warned.first_name),
                                                       warned.id)
    else:
        message.reply_text("No user has been designated!")
    return ""
Пример #3
0
def demote(update: Update, context: CallbackContext) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    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_member = chat.get_member(user_id)
    if user_member.status == 'creator':
        message.reply_text("This person CREATED the chat, how would I demote them?")
        return ""

    if not user_member.status == 'administrator':
        message.reply_text("Can't demote what wasn't promoted!")
        return ""

    if user_id == context.bot.id:
        message.reply_text("I can't demote myself! Get an admin to do it for me.")
        return ""

    try:
        context.bot.promoteChatMember(int(chat.id), int(user_id),
                              can_change_info=False,
                              can_post_messages=False,
                              can_edit_messages=False,
                              can_delete_messages=False,
                              can_invite_users=False,
                              can_restrict_members=False,
                              can_pin_messages=False,
                              can_promote_members=False)
        message.reply_text("Successfully demoted!")
        return "<b>{}:</b>" \
               "\n#DEMOTED" \
               "\n<b>• Admin:</b> {}" \
               "\n<b>• User:</b> {}".format(html.escape(chat.title),
                                          mention_html(user.id, user.first_name),
                                          mention_html(user_member.user.id, user_member.user.first_name))

    except BadRequest:
        message.reply_text("Could not demote. I might not be admin, or the admin status was appointed by another "
                           "user, so I can't act upon them!")
        return ""
Пример #4
0
def nomedia(update: Update, context: CallbackContext) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    args = message.text.split(" ")

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(
            "You'll need to either give me a username to restrict, or reply to someone to be restricted."
        )
        return ""

    if user_id == context.bot.id:
        message.reply_text("I'm not restricting myself!")
        return ""

    member = chat.get_member(int(user_id))

    if member:
        if is_user_admin(chat, user_id, member=member):
            message.reply_text("Afraid I can't restrict admins!")

        elif member.can_send_messages is None or member.can_send_messages:
            context.bot.restrict_chat_member(chat.id, user_id,
                                             NOMEDIA_PERMISSIONS)

            reply = "{} is restricted from sending media!".format(
                mention_html(member.user.id, member.user.first_name))
            message.reply_text(reply, parse_mode=ParseMode.HTML)
            return "<b>{}:</b>" \
                   "\n#RESTRICTED" \
                   "\n<b>• Admin:</b> {}" \
                   "\n<b>• User:</b> {}" \
                   "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title),
                                              mention_html(user.id, user.first_name),
                                              mention_html(member.user.id, member.user.first_name), user_id)

        else:
            message.reply_text("This user is already restricted!")
    else:
        message.reply_text("This user isn't in the chat!")

    return ""
Пример #5
0
def promote(update: Update, context: CallbackContext) -> str:
    chat_id = update.effective_chat.id
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    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_member = chat.get_member(user_id)
    if user_member.status == 'administrator' or user_member.status == 'creator':
        message.reply_text("How am I meant to promote someone that's already an admin?")
        return ""

    if user_id == context.bot.id:
        message.reply_text("I can't promote myself! Get an admin to do it for me.")
        return ""

    # set same perms as bot - bot can't assign higher perms than itself!
    bot_member = chat.get_member(context.bot.id)

    context.bot.promoteChatMember(chat_id, user_id,
                          can_change_info=bot_member.can_change_info,
                          can_post_messages=bot_member.can_post_messages,
                          can_edit_messages=bot_member.can_edit_messages,
                          can_delete_messages=bot_member.can_delete_messages,
                          can_invite_users=bot_member.can_invite_users,
                          can_restrict_members=bot_member.can_restrict_members,
                          can_pin_messages=bot_member.can_pin_messages,
                          can_promote_members=bot_member.can_promote_members)

    message.reply_text("Successfully promoted!")
    return "<b>{}:</b>" \
           "\n#PROMOTED" \
           "\n<b>• Admin:</b> {}" \
           "\n<b>• User:</b> {}".format(html.escape(chat.title),
                                      mention_html(user.id, user.first_name),
                                      mention_html(user_member.user.id, user_member.user.first_name))
Пример #6
0
def media(update: Update, context: CallbackContext) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    args = message.text.split(" ")

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(
            "You'll need to either give me a username to unrestrict, or reply to someone to be unrestricted."
        )
        return ""

    member = chat.get_member(int(user_id))

    if member.status != 'kicked' and member.status != 'left':
        if member.can_send_messages and member.can_send_media_messages \
                and member.can_send_other_messages and member.can_add_web_page_previews:
            message.reply_text(
                "This user already has the rights to send anything.")
        else:
            context.bot.restrict_chat_member(chat.id, int(user_id),
                                             MEDIA_PERMISSIONS)

            reply = "Yep, {} can send media again!".format(
                mention_html(member.user.id, member.user.first_name))
            message.reply_text(reply, parse_mode=ParseMode.HTML)
            return "<b>{}:</b>" \
                   "\n#UNRESTRICTED" \
                   "\n<b>• Admin:</b> {}" \
                   "\n<b>• User:</b> {}" \
                   "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title),
                                                           mention_html(user.id, user.first_name),
                                                           mention_html(member.user.id, member.user.first_name), user_id)
    else:
        message.reply_text(
            "This user isn't even in the chat, unrestricting them won't make them send anything than they "
            "already do!")

    return ""
Пример #7
0
def about_me(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    args = message.text.split(" ")
    user_id = extract_user(message, args)

    if user_id:
        user = context.bot.get_chat(user_id)
    else:
        user = message.from_user

    info = sql.get_user_me_info(user.id)

    if info:
        update.effective_message.reply_text("*{}*:\n{}".format(
            user.first_name, escape_markdown(info)),
                                            parse_mode=ParseMode.MARKDOWN)
    elif message.reply_to_message:
        username = message.reply_to_message.from_user.first_name
        update.effective_message.reply_text(
            username + " hasn't set an info message about themselves  yet!")
    else:
        update.effective_message.reply_text(
            "You haven't set an info message about yourself yet!")
Пример #8
0
def reset_warns(update: Update, context: CallbackContext) -> str:
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    args = message.text.split(" ")

    user_id = extract_user(message, args)

    if user_id:
        sql.reset_warns(user_id, chat.id)
        message.reply_text("Warnings have been reset!")
        warned = chat.get_member(user_id).user
        return "<b>{}:</b>" \
               "\n#RESETWARNS" \
               "\n<b>• Admin:</b> {}" \
               "\n<b>• User:</b> {}" \
               "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title),
                                                       mention_html(user.id, user.first_name),
                                                       mention_html(warned.id, warned.first_name),
                                                       warned.id)
    else:
        message.reply_text("No user has been designated!")
    return ""
Пример #9
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)