Example #1
0
def permanent_pin(update, context):
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message
    args = context.args

    get_permapin = sql.get_permapin(chat.id)
    if get_permapin and not user.id == context.bot.id:
        try:
            to_del = context.bot.pinChatMessage(chat.id,
                                                get_permapin,
                                                disable_notification=True)
        except BadRequest:
            sql.set_permapin(chat.id, 0)
            if chat.username:
                old_pin = "https://t.me/{}/{}".format(chat.username,
                                                      get_permapin)
            else:
                old_pin = "https://t.me/c/{}/{}".format(
                    str(chat.id)[4:], get_permapin)
            send_message(
                update.effective_message,
                tl(
                    update.effective_message,
                    "*Permanen pin error:*\nSaya tidak bisa menyematkan pesan di sini!\nPastikan saya admin dan dapat pin pesan.\n\nPermanen pin di nonaktifkan, [pesan permanen pin lama ada disini]({})"
                ).format(old_pin),
                parse_mode="markdown")
            return

        if to_del:
            try:
                context.bot.deleteMessage(chat.id, message.message_id + 1)
            except BadRequest:
                print("Permanent pin error: cannot delete pin msg")
Example #2
0
def permanent_pin_set(update, context):
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
        if not args:
            get_permapin = sql.get_permapin(chat_id)
            text_maker = tl(update.effective_message,
                            "Permanen pin saat ini di atur: `{}`").format(
                                bool(int(get_permapin)))
            if get_permapin:
                if chat.username:
                    old_pin = "https://t.me/{}/{}".format(
                        chat.username, get_permapin)
                else:
                    old_pin = "https://t.me/c/{}/{}".format(
                        str(chat.id)[4:], get_permapin)
                text_maker += tl(
                    update.effective_message,
                    "\nUntuk menonaktifkan permanen pin: `/permanentpin off`")
                text_maker += tl(
                    update.effective_message,
                    "\n\n[Pesan permanen pin ada disini]({})").format(old_pin)
            send_message(update.effective_message,
                         tl(update.effective_message, text_maker),
                         parse_mode="markdown")
            return ""
        prev_message = args[0]
        if prev_message == "off":
            sql.set_permapin(chat_id, 0)
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "Permanen pin telah di nonaktifkan!"))
            return
        if "/" in prev_message:
            prev_message = prev_message.split("/")[-1]
    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_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title
        if update.effective_message.reply_to_message:
            prev_message = update.effective_message.reply_to_message.message_id
        elif len(args) >= 1 and args[0] == "off":
            sql.set_permapin(chat.id, 0)
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "Permanen pin telah di nonaktifkan!"))
            return
        else:
            get_permapin = sql.get_permapin(chat_id)
            text_maker = tl(update.effective_message,
                            "Permanen pin saat ini di atur: `{}`").format(
                                bool(int(get_permapin)))
            if get_permapin:
                if chat.username:
                    old_pin = "https://t.me/{}/{}".format(
                        chat.username, get_permapin)
                else:
                    old_pin = "https://t.me/c/{}/{}".format(
                        str(chat.id)[4:], get_permapin)
                text_maker += tl(
                    update.effective_message,
                    "\nUntuk menonaktifkan permanen pin: `/permanentpin off`")
                text_maker += tl(
                    update.effective_message,
                    "\n\n[Pesan permanen pin ada disini]({})").format(old_pin)
            send_message(update.effective_message,
                         text_maker,
                         parse_mode="markdown")
            return ""

    is_group = chat.type != "private" and chat.type != "channel"

    if prev_message and is_group:
        sql.set_permapin(chat.id, prev_message)
        send_message(
            update.effective_message,
            tl(update.effective_message, "Permanent pin berhasil di atur!"))
        return "<b>{}:</b>" \
            "\n#PERMANENT_PIN" \
            "\n<b>Admin:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name))

    return ""