Beispiel #1
0
def set_rules(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_message

    if conn := connected(bot, update, chat, user.id):
        chat_id = conn
Beispiel #2
0
def list_notes(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    if conn := connected(bot, update, chat, user.id, need_admin=False):
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
        msg = tld(chat.id, "note_in_chat")
Beispiel #3
0
def mute(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message

    if conn := connected(bot, update, chat, user.id):
        chatD = dispatcher.bot.getChat(conn)
Beispiel #4
0
def list_handlers(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user

    if conn := connected(bot, update, chat, user.id, need_admin=False):
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
Beispiel #5
0
def stop_filter(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    args = update.effective_message.text.split(None, 1)

    if conn := connected(bot, update, chat, user.id):
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
Beispiel #6
0
def blacklist(bot: Bot, update: Update, args: List[str]):
    msg = update.effective_message
    chat = update.effective_chat
    user = update.effective_user

    if conn := connected(bot, update, chat, user.id, need_admin=False):
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
Beispiel #7
0
def get_rules(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_message
    from_pm = False

    if conn := connected(bot, update, chat, user.id):
        chat_id = conn
        from_pm = True
Beispiel #8
0
def add_blacklist(bot: Bot, update: Update):
    msg = update.effective_message
    chat = update.effective_chat
    user = update.effective_user
    words = msg.text.split(None, 1)

    if conn := connected(bot, update, chat, user.id):
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
Beispiel #9
0
def filters(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_message
    args = msg.text.split(
        None,
        1)  # use python's maxsplit to separate Cmd, keyword, and reply_text

    if conn := connected(bot, update, chat, user.id):
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
Beispiel #10
0
def invite(bot: Bot, update: Update):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chatP = dispatcher.bot.getChat(conn)
    else:
        chatP = update.effective_chat
        if chat.type == "private":
            exit(1)

    if chatP.username:
        update.effective_message.reply_text(chatP.username)
    elif chatP.type == chatP.SUPERGROUP or chatP.type == chatP.CHANNEL:
        bot_member = chatP.get_member(bot.id)
        if bot_member.can_invite_users:
            invitelink = chatP.invite_link
            # print(invitelink)
            if not invitelink:
                invitelink = bot.exportChatInviteLink(chatP.id)

            update.effective_message.reply_text(invitelink)
        else:
            update.effective_message.reply_text(
                tld(
                    chat.id,
                    "I don't have access to the invite link, try changing my permissions!"
                ))
    else:
        update.effective_message.reply_text(
            tld(
                chat.id,
                "Sorry, but I can give invite links only for supergroups and channels!"
            ))
Beispiel #11
0
def blacklist(bot: Bot, update: Update, args: List[str]):
    msg = update.effective_message
    chat = update.effective_chat
    user = update.effective_user

    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if chat.type == "private":
            return
        else:
            chat_id = update.effective_chat.id
            chat_name = chat.title

    filter_list = tld(chat.id, "blacklist_active_list").format(chat_name)

    all_blacklisted = sql.get_chat_blacklist(chat_id)

    if len(args) > 0 and args[0].lower() == 'copy':
        for trigger in all_blacklisted:
            filter_list += "<code>{}</code>\n".format(html.escape(trigger))
    else:
        for trigger in all_blacklisted:
            filter_list += " • <code>{}</code>\n".format(html.escape(trigger))

    split_text = split_message(filter_list)
    for text in split_text:
        if filter_list == tld(chat.id, "blacklist_active_list").format(
                chat_name):  #We need to translate
            msg.reply_text(tld(chat.id, "blacklist_no_list").format(chat_name),
                           parse_mode=ParseMode.HTML)
            return
        msg.reply_text(text, parse_mode=ParseMode.HTML)
Beispiel #12
0
def list_locks(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user

    # Connection check
    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_name = chat.title
    else:
        if update.effective_message.chat.type == "private":
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "Anda bisa lakukan command ini pada grup, bukan pada PM"))
            return ""
        chat = update.effective_chat
        chat_name = update.effective_message.chat.title

    res = build_lock_message(chat.id)
    if conn:
        res = res.replace(tl(update.effective_message, 'obrolan ini'),
                          '*{}*'.format(chat_name))

    send_message(update.effective_message, res, parse_mode=ParseMode.MARKDOWN)
Beispiel #13
0
def invite(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chatP = dispatcher.bot.getChat(conn)
    else:
        chatP = update.effective_chat
        if chat.type == "private":
            return

    if chatP.username:
        update.effective_message.reply_text(chatP.username)
    elif chatP.type == chatP.SUPERGROUP or chatP.type == chatP.CHANNEL:
        bot_member = chatP.get_member(bot.id)
        if bot_member.can_invite_users:
            invitelink = chatP.invite_link
            #print(invitelink)
            if not invitelink:
                invitelink = bot.exportChatInviteLink(chatP.id)

            update.effective_message.reply_text(invitelink)
        else:
            update.effective_message.reply_text(
                tld(chat.id, "admin_err_no_perm_invitelink"))
    else:
        update.effective_message.reply_text(
            tld(chat.id, "admin_err_chat_no_invitelink"))
Beispiel #14
0
def private_rules(update, context):
    args = context.args
    chat = update.effective_chat
    user = update.effective_user
    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":
            chat_name = chat.title
        else:
            chat_name = chat.title

    if len(args) >= 1:
        if args[0] in ("yes", "on", "ya"):
            sql.private_rules(str(chat_id), True)
            send_message(update.effective_message, tl(update.effective_message,
                                                      "Private Rules di *aktifkan*, pesan peraturan akan di kirim di PM."),
                         parse_mode="markdown")
        elif args[0] in ("no", "off"):
            sql.private_rules(str(chat_id), False)
            send_message(update.effective_message, tl(update.effective_message,
                                                      "Private Rules di *non-aktifkan*, pesan peraturan akan di kirim di grup."),
                         parse_mode="markdown")
        else:
            send_message(update.effective_message,
                         tl(update.effective_message, "Argumen tidak dikenal - harap gunakan 'yes', atau 'no'."))
    else:
        is_private = sql.get_private_rules(chat_id)
        send_message(update.effective_message,
                     tl(update.effective_message, "Pengaturan Private Rules di {}: *{}*").format(chat_name,
                                                                                                 "Enabled" if is_private else "Disabled"),
                     parse_mode="markdown")
Beispiel #15
0
def mute(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message

    conn = connected(bot, update, chat, user.id)
    if conn:
        chatD = dispatcher.bot.getChat(conn)
    else:
        if chat.type == "private":
            return ""
        else:
            chatD = chat

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(tld(chat.id, "mute_invalid"))
        return ""

    if user_id == bot.id:
        message.reply_text(tld(chat.id, "mute_not_myself"))
        return ""

    member = chatD.get_member(int(user_id))

    if member:

        if user_id in SUDO_USERS:
            message.reply_text(tld(chat.id, "mute_not_sudo"))

        elif is_user_admin(chatD, user_id, member=member):
            message.reply_text(tld(chat.id, "mute_not_m_admin"))

        elif member.can_send_messages is None or member.can_send_messages:
            bot.restrict_chat_member(chatD.id,
                                     user_id,
                                     can_send_messages=False)
            keyboard = []
            reply = tld(chat.id, "mute_success").format(
                mention_html(member.user.id, member.user.first_name),
                chatD.title)
            message.reply_text(reply,
                               reply_markup=keyboard,
                               parse_mode=ParseMode.HTML)
            return "<b>{}:</b>" \
                   "\n#MUTE" \
                   "\n<b>Admin:</b> {}" \
                   "\n<b>User:</b> {}".format(html.escape(chatD.title),
                                              mention_html(
                                                  user.id, user.first_name),
                                              mention_html(member.user.id, member.user.first_name))

        else:
            message.reply_text(
                tld(chat.id, "mute_already_mute").format(chatD.title))
    else:
        message.reply_text(
            tld(chat.id, "mute_not_in_chat").format(chatD.title))

    return ""
Beispiel #16
0
def adminlist(bot, update):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chatP = dispatcher.bot.getChat(conn)
    else:
        chatP = update.effective_chat
        if chat.type == "private":
            exit(1)

    administrators = chatP.get_administrators()

    text = tld(chat.id, "Admins in") + " *{}*:".format(
        chatP.title or tld(chat.id, "this chat"))
    for admin in administrators:
        user = admin.user
        status = admin.status
        if status == "creator":
            name = user.first_name + (user.last_name or "") + tld(
                chat.id, " (Creator)")
        else:
            name = user.first_name + (user.last_name or "")
        text += f"\n• `{name}`"

    update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
Beispiel #17
0
def list_handlers(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user

    conn = connected(context.bot, update, chat, user.id, need_admin=False)
    if conn is not False:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
        filter_list = "*Filter di {}:*\n"
    else:
        chat_id = update.effective_chat.id
        if chat.type == "private":
            chat_name = tl(update.effective_message, "filter lokal")
            filter_list = tl(update.effective_message, "*filter lokal:*\n")
        else:
            chat_name = chat.title
            filter_list = tl(update.effective_message, "*Filter di {}*:\n")

    all_handlers = sql.get_chat_triggers(chat_id)

    if not all_handlers:
        send_message(update.effective_message,
                     tl(update.effective_message, "Tidak ada filter di {}!").format(chat_name))
        return

    for keyword in all_handlers:
        entry = " - {}\n".format(escape_markdown(keyword))
        if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH:
            send_message(update.effective_message, filter_list.format(chat_name),
                         parse_mode=telegram.ParseMode.MARKDOWN)
            filter_list = entry
        else:
            filter_list += entry

    send_message(update.effective_message, filter_list.format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN)
Beispiel #18
0
def stop_filter(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user
    args = update.effective_message.text.split(None, 1)

    conn = connected(context.bot, update, chat, user.id)
    if conn is not False:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        chat_id = update.effective_chat.id
        if chat.type == "private":
            chat_name = tl(update.effective_message, "catatan lokal")
        else:
            chat_name = chat.title

    if len(args) < 2:
        send_message(update.effective_message, tl(update.effective_message, "Apa yang harus saya hentikan?"))
        return

    chat_filters = sql.get_chat_triggers(chat_id)

    if not chat_filters:
        send_message(update.effective_message, tl(update.effective_message, "Tidak ada filter aktif di sini!"))
        return

    for keyword in chat_filters:
        if keyword == args[1].lower():
            sql.remove_filter(chat_id, args[1].lower())
            send_message(update.effective_message,
                         tl(update.effective_message, "Ya, saya akan berhenti menjawabnya di *{}*.").format(chat_name),
                         parse_mode=telegram.ParseMode.MARKDOWN)
            raise DispatcherHandlerStop

    send_message(update.effective_message, "Itu bukan filter aktif - jalankan /filter untuk semua filter aktif.")
Beispiel #19
0
def permapin(update, context):
    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=False)
    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,
                tl(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

    text, data_type, content, buttons = get_message_type(message)
    tombol = build_keyboard_alternate(buttons)
    try:
        message.delete()
    except BadRequest:
        pass
    if str(data_type) in ('Types.BUTTON_TEXT', 'Types.TEXT'):
        try:
            sendingmsg = context.bot.send_message(
                chat_id,
                text,
                parse_mode="markdown",
                disable_web_page_preview=True,
                reply_markup=InlineKeyboardMarkup(tombol))
        except BadRequest:
            context.bot.send_message(
                chat_id,
                tl(
                    update.effective_message,
                    "Wrong markdown text!\nIf you don't know what markdown is, please type `/markdownhelp` in PM."
                ),
                parse_mode="markdown")
            return
    else:
        sendingmsg = ENUM_FUNC_MAP[str(data_type)](
            chat_id,
            content,
            caption=text,
            parse_mode="markdown",
            disable_web_page_preview=True,
            reply_markup=InlineKeyboardMarkup(tombol))
    try:
        context.bot.pinChatMessage(chat_id, sendingmsg.message_id)
    except BadRequest:
        send_message(
            update.effective_message,
            tl(update.effective_message,
               "I don't have access to pin message!"))
Beispiel #20
0
def set_rules(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_message

    conn = connected(bot, update, chat, user.id)
    if conn: chat_id = conn
    else:
        if chat.type == 'private':
            msg.reply_text(tld(chat.id, 'common_cmd_group_only'))
            return
        chat_id = chat.id

    raw_text = msg.text
    args = raw_text.split(None,
                          1)  # use python's maxsplit to separate cmd and args
    if len(args) == 2:
        txt = args[1]
        offset = len(txt) - len(
            raw_text)  # set correct offset relative to command
        markdown_rules = markdown_parser(txt,
                                         entities=msg.parse_entities(),
                                         offset=offset)

        sql.set_rules(chat_id, markdown_rules)
        msg.reply_text(tld(chat.id, "rules_success"))
Beispiel #21
0
def set_warn_limit(update, context) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "Anda bisa lakukan command ini pada grup, bukan pada PM"))
            return ""
        chat = update.effective_chat
        chat_name = update.effective_message.chat.title

    if args:
        if args[0].isdigit():
            if int(args[0]) < 3:
                send_message(
                    update.effective_message,
                    tl(update.effective_message,
                       "Batas peringatan minimum adalah 3!"))
            else:
                sql.set_warn_limit(chat.id, int(args[0]))
                if conn:
                    text = tl(
                        update.effective_message,
                        "Diperbarui batas untuk diperingatkan {} pada *{}*"
                    ).format(args[0], chat_name)
                else:
                    text = tl(
                        update.effective_message,
                        "Diperbarui batas untuk diperingatkan {}").format(
                            args[0])
                send_message(update.effective_message,
                             text,
                             parse_mode="markdown")
                return "<b>{}:</b>" \
                       "\n#SET_WARN_LIMIT" \
                       "\n<b>Admin:</b> {}" \
                       "\nSet the warn limit to <code>{}</code>".format(html.escape(chat.title),
                                                                        mention_html(user.id, user.first_name), args[0])
        else:
            send_message(update.effective_message,
                         tl(update.effective_message, "Beri aku angkanya!"))
    else:
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)
        if conn:
            text = tl(update.effective_message,
                      "Batas peringatan saat ini adalah {} pada *{}*").format(
                          limit, chat_name)
        else:
            text = tl(update.effective_message,
                      "Batas peringatan saat ini adalah {}").format(limit)
        send_message(update.effective_message, text, parse_mode="markdown")
    return ""
Beispiel #22
0
def add_blacklist(bot: Bot, update: Update):
    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)

    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":
            exit(1)
        else:
            chat_name = chat.title

    if len(words) > 1:
        text = words[1]
        to_blacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip()))
        for trigger in to_blacklist:
            sql.add_to_blacklist(chat_id, trigger.lower())

        if len(to_blacklist) == 1:
            msg.reply_text(tld(chat.id, "Added <code>{}</code> to the blacklist in <b>{}</b>!").format(
                html.escape(to_blacklist[0]), chat_name),
                           parse_mode=ParseMode.HTML)

        else:
            msg.reply_text(tld(chat.id,
                               "Added <code>{}</code> to the blacklist in <b>{}</b>!").format(len(to_blacklist)),
                           chat_name, parse_mode=ParseMode.HTML)

    else:
        msg.reply_text(tld(chat.id, "Tell me what words you would like to add to the blacklist."))
Beispiel #23
0
def nomedia(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message

    conn = connected(bot, update, chat, user.id)
    if conn:
        chatD = dispatcher.bot.getChat(conn)
    else:
        if chat.type == "private":
            return ""
        else:
            chatD = chat

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(tld(chat.id, "restrict_invalid"))
        return ""

    if user_id == bot.id:
        message.reply_text(tld(chat.id, "restrict_is_bot"))
        return ""

    member = chatD.get_member(int(user_id))

    if member:
        if is_user_admin(chatD, user_id, member=member):
            message.reply_text(tld(chat.id, "restrict_is_admin"))

        elif member.can_send_messages is None or member.can_send_messages:
            bot.restrict_chat_member(chatD.id,
                                     user_id,
                                     can_send_messages=True,
                                     can_send_media_messages=False,
                                     can_send_other_messages=False,
                                     can_add_web_page_previews=False)
            keyboard = []
            reply = tld(chat.id, "restrict_success").format(
                mention_html(member.user.id, member.user.first_name),
                chatD.title)
            message.reply_text(reply,
                               reply_markup=keyboard,
                               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(chatD.title),
                                                           mention_html(
                                                               user.id, user.first_name),
                                                           mention_html(member.user.id, member.user.first_name), user_id)

        else:
            message.reply_text(tld(chat.id, "restrict_already_restricted"))
    else:
        message.reply_text(
            tld(chat.id, "mute_not_in_chat").format(chatD.title))

    return ""
Beispiel #24
0
def adminlist(update, context):
    chat = update.effective_chat
    user = update.effective_user

    conn = connected(context.bot, update, chat, user.id, need_admin=False)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_id = conn
    else:
        if update.effective_message.chat.type == "private":
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "You can do this command in groups, not PM"))
            return ""
        chat = update.effective_chat
        chat_id = update.effective_chat.id

    administrators = context.bot.getChatAdministrators(chat_id)
    text = tl(update.effective_message, "Admin in *{}*:").format(
        update.effective_chat.title
        or tl(update.effective_message, "chat ini"))
    for admin in administrators:
        user = admin.user
        status = admin.status
        if user.first_name == '':
            name = tl(update.effective_message, "☠ Deleted Account")
        else:
            name = "{}".format(
                mention_markdown(
                    user.id, user.first_name + " " + (user.last_name or "")))
        # if user.username:
        #    name = escape_markdown("@" + user.username)
        if status == "creator":
            text += "\n 👑 Creator:"
            text += "\n` • `{} \n\n 🔱 Admins:".format(name)
    for admin in administrators:
        user = admin.user
        status = admin.status
        if user.first_name == '':
            name = tl(update.effective_message, "☠ Deleted Account")
        else:
            name = "{}".format(
                mention_markdown(
                    user.id, user.first_name + " " + (user.last_name or "")))
        # if user.username:
        #    name = escape_markdown("@" + user.username)
        if status == "administrator":
            text += "\n` • `{}".format(name)

    try:
        send_message(update.effective_message,
                     text,
                     parse_mode=ParseMode.MARKDOWN)
    except BadRequest:
        send_message(update.effective_message,
                     text,
                     parse_mode=ParseMode.MARKDOWN,
                     quote=False)
Beispiel #25
0
def demote(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat
    message = update.effective_message
    user = update.effective_user
    conn = connected(bot, update, chat, user.id)
    if conn:
        chatD = dispatcher.bot.getChat(conn)
    else:
        chatD = update.effective_chat
        if chat.type == "private":
            return ""

    if not chatD.get_member(bot.id).can_promote_members:
        update.effective_message.reply_text(tld(chat.id, "admin_err_no_perm"))
        return ""

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(tld(chat.id, "common_err_no_user"))
        return ""

    user_member = chatD.get_member(user_id)
    if user_member.status == 'creator':
        message.reply_text(tld(chat.id, "admin_err_demote_creator"))
        return ""

    if not user_member.status == 'administrator':
        message.reply_text(tld(chat.id, "admin_err_demote_noadmin"))
        return ""

    if user_id == bot.id:
        message.reply_text(tld(chat.id, "admin_err_self_demote"))
        return ""

    try:
        bot.promoteChatMember(int(chatD.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(tld(chat.id, "admin_demote_success").format(
            mention_html(user.id, user.first_name),
            mention_html(user_member.user.id, user_member.user.first_name),
            html.escape(chatD.title)),
                           parse_mode=ParseMode.HTML)
        return f"<b>{html.escape(chatD.title)}:</b>" \
                "\n#DEMOTED" \
               f"\n<b>Admin:</b> {mention_html(user.id, user.first_name)}" \
               f"\n<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}"

    except BadRequest:
        message.reply_text(tld(chat.id, "admin_err_cant_demote"))
        return ""
Beispiel #26
0
def promote(bot: Bot, update: Update, args: List[str]) -> str:
    message = update.effective_message  # type: Optional[Message]
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    conn = connected(bot, update, chat, user.id)
    if conn:
        chatD = dispatcher.bot.getChat(conn)
    else:
        chatD = update.effective_chat
        if chat.type == "private":
            exit(1)

    if not chatD.get_member(bot.id).can_promote_members:
        update.effective_message.reply_text(
            "I can't promote/demote people here! "
            "Make sure I'm admin and can appoint new admins.")

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

    user_member = chatD.get_member(user_id)
    if user_member.status == 'administrator' or user_member.status == 'creator':
        message.reply_text(
            tld(chat.id,
                "How am I meant to promote someone who's already an admin?"))
        return ""

    if user_id == bot.id:
        message.reply_text(
            tld(chat.id,
                "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 = chatD.get_member(bot.id)

    bot.promoteChatMember(
        chatD.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(tld(chat.id,
                           f"Successfully promoted in *{chatD.title}*!"),
                       parse_mode=ParseMode.MARKDOWN)
    return f"<b>{html.escape(chatD.title)}:</b>" \
           "\n#PROMOTED" \
           f"\n<b>• Admin:</b> {mention_html(user.id, user.first_name)}" \
           f"\n<b>• User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}"
Beispiel #27
0
def unblacklist(bot: Bot, update: Update):
    msg = update.effective_message
    chat = update.effective_chat
    user = update.effective_user
    words = msg.text.split(None, 1)

    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]
        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:
                msg.reply_text(tld(chat.id, "blacklist_del").format(
                    html.escape(to_unblacklist[0]), chat_name),
                               parse_mode=ParseMode.HTML)
            else:
                msg.reply_text(tld(chat.id, "blacklist_err_not_trigger"))

        elif successful == len(to_unblacklist):
            msg.reply_text(tld(chat.id, "blacklist_multi_del").format(
                successful, chat_name),
                           parse_mode=ParseMode.HTML)

        elif not successful:
            msg.reply_text(tld(chat.id,
                               "blacklist_err_multidel_no_trigger").format(
                                   successful,
                                   len(to_unblacklist) - successful),
                           parse_mode=ParseMode.HTML)

        else:
            msg.reply_text(tld(
                chat.id, "blacklist_err_multidel_some_no_trigger").format(
                    successful, chat_name,
                    len(to_unblacklist) - successful),
                           parse_mode=ParseMode.HTML)
    else:
        msg.reply_text(tld(chat.id, "blacklist_err_del_no_args"))
Beispiel #28
0
def private_note(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    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":
            chat_name = chat.title
        else:
            chat_name = chat.title

    if len(args) >= 1:
        if args[0] in ("yes", "on", "ya"):
            if len(args) >= 2:
                if args[1] == "del":
                    sql.private_note(str(chat_id), True, True)
                    send_message(update.effective_message, (tld(
                        chat.id,
                        "Private Note is *activated*, when a user retrieves a note, a note message will be sent to the PM and the user's message will be immediately deleted."
                    )),
                                 parse_mode="markdown")
                else:
                    sql.private_note(str(chat_id), True, False)
                    send_message(update.effective_message, (tld(
                        chat.id,
                        "Private Note is *activated*, when a user retrieves a note, a note message will be sent to the PM."
                    )),
                                 parse_mode="markdown")
            else:
                sql.private_note(str(chat_id), True, False)
                send_message(update.effective_message, (tld(
                    chat.id,
                    "Private Note is *activated*, when a user retrieves a note, a note message will be sent to the PM."
                )),
                             parse_mode="markdown")
        elif args[0] in ("no", "off"):
            sql.private_note(str(chat_id), False, False)
            send_message(update.effective_message, (tld(
                chat.id,
                "Private Note is *deactivated*, note messages will be sent to the group."
            )),
                         parse_mode="markdown")
        else:
            send_message(update.effective_message, (tld(
                chat.id, "Unknown argument - please use 'yes', or 'no'.")))
    else:
        is_private, is_delete = sql.get_private_note(chat_id)
        print(is_private, is_delete)
        send_message(
            update.effective_message,
            (tld(chat.id, "Private note settings at {}: *{}*{}").format(
                chat_name, "Enabled" if is_private else "Disabled",
                " - *Hash will be deleted*" if is_delete else "")),
            parse_mode="markdown")
Beispiel #29
0
def promote(bot: Bot, update: Update, args: List[str]) -> str:
    message = update.effective_message
    user = update.effective_user
    chat = update.effective_chat
    conn = connected(bot, update, chat, user.id)
    if conn:
        chatD = dispatcher.bot.getChat(conn)
    else:
        chatD = update.effective_chat
        if chat.type == "private":
            return ""

    if user_can_promote(chat, user, bot.id) is False:
        message.reply_text(tld(chat.id, "admin_no_promote_perm"))
        return ""

    if not chatD.get_member(bot.id).can_promote_members:
        message.reply_text(tld(chat.id, "admin_err_no_perm"))
        return ""

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(tld(chat.id, "common_err_no_user"))
        return ""

    user_member = chatD.get_member(user_id)
    if user_member.status == 'administrator' or user_member.status == 'creator':
        message.reply_text(tld(chat.id, "admin_err_user_admin"))
        return ""

    if user_id == bot.id:
        message.reply_text(tld(chat.id, "admin_err_self_promote"))
        return ""

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

    bot.promoteChatMember(chatD.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(tld(chat.id, "admin_promote_success").format(
        mention_html(user.id, user.first_name),
        mention_html(user_member.user.id, user_member.user.first_name),
        html.escape(chatD.title)),
                       parse_mode=ParseMode.HTML)
    return f"<b>{html.escape(chatD.title)}:</b>" \
        "\n#PROMOTED" \
           f"\n<b>• Admin:</b> {mention_html(user.id, user.first_name)}" \
           f"\n<b>• User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}"
Beispiel #30
0
def set_blue_text_must_click(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    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,
                tl(update.effective_message,
                   "You can do this command in groups, not 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 == "off" or val == "no":
            sql.set_cleanbt(chat_id, False)
            if conn:
                text = tl(update.effective_message,
                          "Blue text cleaner was *disabled* in *{}*.").format(
                              chat_name)
            else:
                text = tl(update.effective_message,
                          "Blue text cleaner was *disabled*.")
            send_message(update.effective_message, text, parse_mode="markdown")

        elif val == "yes" or val == "on":
            sql.set_cleanbt(chat_id, True)
            if conn:
                text = tl(update.effective_message,
                          "Blue text cleaner was *enabled* in *{}*.").format(
                              chat_name)
            else:
                text = tl(update.effective_message,
                          "Blue text cleaner was *enabled*.")
            send_message(update.effective_message, text, parse_mode="markdown")

        else:
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "Unknown argument - please use 'yes', or 'no'."))
    else:
        send_message(
            update.effective_message,
            tl(update.effective_message,
               "Curent settings for Blue text cleaner at {}: *{}*").format(
                   chat_name,
                   "Enabled" if sql.is_enable(chat_id) else "Disabled"),
            parse_mode="markdown")