Ejemplo n.º 1
0
def warns(bot: Bot, update: Update, args: List[str]):
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user_id = extract_user(message, args) or update.effective_user.id
    result = sql.get_warns(user_id, chat.id)

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

        if reasons:
            text = "This user has {}/{} warnings, for the following reasons:".format(
                num_warns, limit)
            for reason in reasons:
                text += "\n - {}".format(reason)

            msgs = split_message(text)
            for msg in msgs:
                update.effective_message.reply_text(msg)
        else:
            update.effective_message.reply_text(
                "User has {}/{} warnings, but no reasons for any of them.".
                format(num_warns, limit))
    else:
        update.effective_message.reply_text(
            "This user hasn't got any warnings!")
Ejemplo n.º 2
0
def get_user_common_chats(bot: Bot, update: Update, args: List[str]):
    msg = update.effective_message
    user = extract_user(msg, args)
    if not user:
        msg.reply_text("I share no common chats with the void.")
        return
    common_list = get_user_com_chats(user)
    if not common_list:
        msg.reply_text("No common chats with this user!")
        return
    name = bot.get_chat(user).first_name
    text = f"<b>Common chats with {name}</b>\n"
    for chat in common_list:
        try:
            chat_name = bot.get_chat(chat).title
            sleep(0.3)
            text += f"• <code>{chat_name}</code>\n"
        except BadRequest:
            pass
        except Unauthorized:
            pass
        except RetryAfter as e:
            sleep(e.retry_after)

    if len(text) < 4096:
        msg.reply_text(text, parse_mode="HTML")
    else:
        with open("common_chats.txt", "w") as f:
            f.write(text)
        with open("common_chats.txt", "rb") as f:
            msg.reply_document(f)
        os.remove("common_chats.txt")
Ejemplo n.º 3
0
def unmute(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]

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

    member = chat.get_member(int(user_id))

    if member:
        if is_user_admin(chat, user_id, member=member):
            message.reply_text(
                "This is an admin, what do you expect me to do?")
            return ""

        elif 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 right to speak.")
                return ""
            else:
                bot.restrict_chat_member(
                    chat.id,
                    int(user_id),
                    can_send_messages=True,
                    can_send_media_messages=True,
                    can_send_other_messages=True,
                    can_add_web_page_previews=True,
                )
                message.reply_text("Unmuted!")
                return ("<b>{}:</b>"
                        "\n#UNMUTE"
                        "\n<b>Admin:</b> {}"
                        "\n<b>User:</b> {}".format(
                            html.escape(chat.title),
                            mention_html(user.id, user.first_name),
                            mention_html(member.user.id,
                                         member.user.first_name),
                        ))
    else:
        message.reply_text(
            "This user isn't even in the chat, unmuting them won't make them talk more than they "
            "already do!")

    return ""
Ejemplo n.º 4
0
def slap(bot: Bot, update: Update, args: List[str]):
    msg = update.effective_message  # type: Optional[Message]

    # reply to correct message
    reply_text = (msg.reply_to_message.reply_text
                  if msg.reply_to_message else msg.reply_text)

    # get user who sent message
    if msg.from_user.username:
        curr_user = msg.from_user.first_name.replace("<", "&lt;")
        curr_user = curr_user.replace(">", "&gt;")
    else:
        curr_user = """<a href="tg://user?id={}">{}</a>""".format(
            msg.from_user.id, msg.from_user.first_name)

    user_id = extract_user(update.effective_message, args)
    if user_id:
        slapped_user = bot.get_chat(user_id)
        user1 = curr_user
        if slapped_user.username:
            user2 = slapped_user.first_name.replace("<", "&lt;")
            user2 = user2.replace(">", "&gt;")
        else:
            user2 = """<a href="tg://user?id={}">{}</a>""".format(
                slapped_user.id, slapped_user.first_name)

    # if no target found, bot targets the sender
    else:
        user1 = """<a href="tg://user?id={}">{}</a>""".format(
            bot.id, bot.first_name)
        user2 = curr_user

    temp = random.choice(SLAP_TEMPLATES)
    item = random.choice(ITEMS)
    hit = random.choice(HIT)
    throw = random.choice(THROW)

    repl = temp.format(user1=user1,
                       user2=user2,
                       item=item,
                       hits=hit,
                       throws=throw)

    reply_text(repl, parse_mode=ParseMode.HTML)
Ejemplo n.º 5
0
def mute(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]

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

    if user_id == bot.id:
        message.reply_text("I'm not muting 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 stop an admin from talking!")

        elif member.can_send_messages is None or member.can_send_messages:
            bot.restrict_chat_member(chat.id, user_id, can_send_messages=False)
            message.reply_text("Muted!")
            return ("<b>{}:</b>"
                    "\n#MUTE"
                    "\n<b>Admin:</b> {}"
                    "\n<b>User:</b> {}".format(
                        html.escape(chat.title),
                        mention_html(user.id, user.first_name),
                        mention_html(member.user.id, member.user.first_name),
                    ))

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

    return ""
Ejemplo n.º 6
0
def about_me(bot: Bot, update: Update, args: List[str]):
    message = update.effective_message  # type: Optional[Message]
    user_id = extract_user(message, args)

    if user_id:
        user = 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!")
Ejemplo n.º 7
0
def reset_warns(bot: Bot, update: Update, args: List[str]) -> str:
    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, 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> {} (<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 ""
Ejemplo n.º 8
0
def get_id(bot: Bot, update: Update, args: List[str]):
    user_id = extract_user(update.effective_message, args)
    if user_id:
        if (update.effective_message.reply_to_message
                and update.effective_message.reply_to_message.forward_from):
            user1 = update.effective_message.reply_to_message.from_user
            user2 = update.effective_message.reply_to_message.forward_from
            update.effective_message.reply_text(
                "The original sender, {}, has an ID of `{}`.\nThe forwarder, {}, has an ID of `{}`."
                .format(
                    escape_markdown(user2.first_name),
                    user2.id,
                    escape_markdown(user1.first_name),
                    user1.id,
                ),
                parse_mode=ParseMode.MARKDOWN,
            )
        else:
            user = bot.get_chat(user_id)
            update.effective_message.reply_text(
                "{}'s id is `{}`.".format(escape_markdown(user.first_name),
                                          user.id),
                parse_mode=ParseMode.MARKDOWN,
            )
    else:
        chat = update.effective_chat  # type: Optional[Chat]
        if chat.type == "private":
            update.effective_message.reply_text("Your id is `{}`.".format(
                chat.id),
                                                parse_mode=ParseMode.MARKDOWN)

        else:
            update.effective_message.reply_text(
                "This group's id is `{}`.".format(chat.id),
                parse_mode=ParseMode.MARKDOWN,
            )
Ejemplo n.º 9
0
def ungban(bot: Bot, update: Update, args: List[str]):
    message = update.effective_message  # type: Optional[Message]

    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 = 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):
        message.reply_text("This user is not gbanned!")
        return

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

    message.reply_text(
        "I'll give {} a second chance, globally.".format(
            mention_html(user_chat.id, user_chat.first_name)
        ),
        parse_mode=ParseMode.HTML,
    )

    send_to_list(
        bot,
        SUDO_USERS,
        "{} has ungbanned user {}".format(
            mention_html(banner.id, banner.first_name),
            mention_html(user_chat.id, user_chat.first_name),
        ),
        html=True,
    )

    chats = get_all_chats()
    for chat in chats:
        chat_id = chat.chat_id

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

        try:
            member = bot.get_chat_member(chat_id, user_id)
            if member.status == "kicked":
                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))
                bot.send_message(
                    OWNER_ID, "Could not un-gban due to: {}".format(excp.message)
                )
                return
        except TelegramError:
            pass

    sql.ungban_user(user_id)

    send_to_list(bot, SUDO_USERS, "Un-gban complete!")

    message.reply_text(
        "{} has been un-gbanned.".format(
            mention_html(user_chat.id, user_chat.first_name)
        ),
        parse_mode=ParseMode.HTML,
    )
Ejemplo n.º 10
0
def info(bot: Bot, update: Update, args: List[str]):
    msg = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user_id = extract_user(update.effective_message, args)

    if user_id:
        user = bot.get_chat(user_id)

    elif not msg.reply_to_message and not args:
        user = msg.from_user

    elif not msg.reply_to_message and (
            not args or
        (len(args) >= 1 and not args[0].startswith("@")
         and not args[0].isdigit()
         and not msg.parse_entities([MessageEntity.TEXT_MENTION]))):
        msg.reply_text("I can't extract a user from this.")
        return

    else:
        return

    text = ("<b>User info</b>:"
            "\nID: <code>{}</code>"
            "\nFirst Name: {}".format(user.id, html.escape(user.first_name)))

    if user.last_name:
        text += "\nLast Name: {}".format(html.escape(user.last_name))

    if user.username:
        text += "\nUsername: @{}".format(html.escape(user.username))

    text += "\nPermanent user link: {}".format(
        mention_html(user.id, user.first_name))

    if user.id == OWNER_ID:
        text += "\n\nThis person is my owner."
    else:
        if user.id in BLACKLIST_USERS:
            pass

        elif user.id in SUDO_USERS:
            text += "\n\nThis person is one of my sudo users."

        else:
            if user.id in SUPPORT_USERS:
                text += "\n\nThis person is one of my support users."

            if user.id in WHITELIST_USERS:
                text += ("\n\nThis person has been whitelisted! "
                         "That means I'm not allowed to ban/kick them.")

    for mod in USER_INFO:
        try:
            mod_info = mod.__user_info__(user.id).strip()
        except TypeError:
            mod_info = mod.__user_info__(user.id, chat.id).strip()
        if mod_info:
            text += "\n\n" + mod_info

    update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)