Esempio n. 1
0
def lyrics(update: Update, context: CallbackContext):
    message = update.effective_message
    text = message.text[len('/lyrics '):]
    args = message.text.split(" ")
    args = args[1:]

    if args and len(args) != 0:
        song = " ".join(args).split("- ")
    else:
        song = ""
        LOGGER.log(2, "No arguments given.")

    reply_text = f'Looks up for lyrics'

    if len(song) == 2:
        song[0].strip()
        song[1].strip()
        try:
            lyrics = "\n".join(
                PyLyrics.getLyrics(song[0], song[1]).split("\n")[:20])
        except ValueError as e:
            return update.effective_message.reply_text("Song %s not found!" %
                                                       song[1],
                                                       failed=True)
        else:
            lyricstext = LYRICSINFO % (song[0].replace(
                " ", "_"), song[1].replace(" ", "_"))
            return update.effective_message.reply_text(lyrics + lyricstext,
                                                       parse_mode="MARKDOWN")
    else:
        return update.effective_message.reply_text(
            "Invalid args- try Artist - Title!", failed=True)
Esempio n. 2
0
 def is_admin(update: Update, context: CallbackContext, *args, **kwargs):
     if is_bot_admin(update.effective_chat, context.bot.id):
         return func(update, context, *args, **kwargs)
     else:
         try:
             update.effective_message.reply_text("I'm not admin!")
         except:
             LOGGER.log(2, "Reply message not found.")
Esempio n. 3
0
def delete_join(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    join = update.effective_message.new_chat_members
    if can_delete(chat, context.bot.id):
        del_join = sql.get_del_pref(chat.id)
        if del_join:
            try:
                update.message.delete()
            except:
                LOGGER.log(2, "Could not delete join message. Line: 609")
Esempio n. 4
0
def welcome_gban(update, context, user_id):
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message

    if sql.is_user_gbanned(user_id):
        chat.kick_member(user_id)
        try:
            if msg:
                msg.delete()
        except:
            LOGGER.log(2, "Could not find the message to delete.")
Esempio n. 5
0
 def send_msg_rights(update: Update, context: CallbackContext, *args,
                     **kwargs):
     is_muted = bot_send_messages(update.effective_chat, context.bot.id)
     if (not is_muted or is_muted == True) and is_muted != False:
         return func(update, context, *args, **kwargs)
     else:
         try:
             context.bot.leave_chat(int(update.effective_chat.id))
             LOGGER.log(2, "Left a group where I was muted.")
         except telegram.TelegramError:
             LOGGER.log(2, "Could not leave chat.")
Esempio n. 6
0
 def promote_rights(update: Update, context: CallbackContext, *args,
                    **kwargs):
     if update.effective_chat.get_member(
             context.bot.id).can_restrict_members:
         return func(update, context, *args, **kwargs)
     else:
         try:
             update.effective_message.reply_text(
                 "I can't restrict people here! "
                 "Make sure I'm admin and can restrict members.")
         except:
             LOGGER.log(
                 2, "Cannot send messages: Chat ID {}".format(
                     str(update.effective_chat.id)))
Esempio n. 7
0
    def is_admin(update: Update, context: CallbackContext, *args, **kwargs):
        user = update.effective_user  # type: Optional[User]
        message = update.effective_message
        if user and is_user_admin(update.effective_chat, user.id):
            return func(update, context, *args, **kwargs)

        elif not user:
            pass

        elif DEL_CMDS and message.text != None and " " not in message.text:
            update.effective_message.delete()

        else:
            try:
                msg = update.effective_message.reply_text(
                    "Who dis non-admin telling me what to do?")
                time.sleep(5)
                msg.delete()
            except:
                LOGGER.log(2, "Reply message not found.")
Esempio n. 8
0
def filters(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user
    msg = update.effective_message  # type: Optional[Message]
    args = msg.text.split(
        None,
        1)  # use python's maxsplit to separate Cmd, keyword, and reply_text

    if len(args) < 2:
        return

    extracted = split_quotes(args[1])
    if len(extracted) < 1:
        return
    # set trigger -> lower, so as to avoid adding duplicate filters with different cases
    keyword = extracted[0].lower()

    is_sticker = False
    is_document = False
    is_image = False
    is_voice = False
    is_audio = False
    is_video = False
    buttons = []

    # determine what the contents of the filter are - text, image, sticker, etc
    if len(extracted) >= 2:
        offset = len(extracted[1]) - len(
            msg.text)  # set correct offset relative to command + notename
        content, buttons = button_markdown_parser(
            extracted[1], entities=msg.parse_entities(), offset=offset)
        content = content.strip()
        if not content:
            msg.reply_text(
                "There is no note message - You can't JUST have buttons, you need a message to go with it!"
            )
            return ""

    elif msg.reply_to_message and msg.reply_to_message.sticker:
        content = msg.reply_to_message.sticker.file_id
        is_sticker = True

    elif msg.reply_to_message and msg.reply_to_message.document:
        content = msg.reply_to_message.document.file_id
        is_document = True

    elif msg.reply_to_message and msg.reply_to_message.photo:
        try:
            offset = len(msg.reply_to_message.caption)
        except:
            offset = 0
            LOGGER.log(2, "Photo has no caption.")

        ignore_underscore_case, buttons = button_markdown_parser(
            msg.reply_to_message.caption,
            entities=msg.reply_to_message.parse_entities(),
            offset=offset)
        content = msg.reply_to_message.photo[
            -1].file_id  # last elem = best quality
        is_image = True

    elif msg.reply_to_message and msg.reply_to_message.audio:
        content = msg.reply_to_message.audio.file_id
        is_audio = True

    elif msg.reply_to_message and msg.reply_to_message.voice:
        content = msg.reply_to_message.voice.file_id
        is_voice = True

    elif msg.reply_to_message and msg.reply_to_message.video:
        content = msg.reply_to_message.video.file_id
        is_video = True

    else:
        msg.reply_text("You didn't specify what to reply with!")
        return ""

    # Add the filter
    # Note: perhaps handlers can be removed somehow using sql.get_chat_filters
    for handler in dispatcher.handlers.get(HANDLER_GROUP, []):
        if handler.filters == (keyword, chat.id):
            dispatcher.remove_handler(handler, HANDLER_GROUP)

    sql.add_filter(chat.id, keyword, content, is_sticker, is_document,
                   is_image, is_audio, is_voice, is_video, buttons)
    log = "<b>{}:</b>" \
          "\n#FILTERS" \
          "\n<b>• Action:</b> added" \
          "\n<b>• Admin:</b> {}" \
          "\n<b>• Trigger:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), keyword)

    update.effective_message.reply_text(
        "Filter has been saved for '`{}`'.".format(keyword),
        parse_mode=ParseMode.MARKDOWN)

    send_filter_log(update, context, log)
    raise DispatcherHandlerStop
Esempio n. 9
0
def new_member(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message # type: Optional[Message]
    chat_name = chat.title or chat.first or chat.username # type: Optional:[chat name]
    should_welc, cust_welcome, welc_type, welc_caption = sql.get_welc_pref(chat.id)
    welc_mutes = sql.welcome_mutes(chat.id)
    human_checks = sql.get_human_checks(user.id, chat.id)
    prev_welc = sql.get_clean_pref(chat.id)
    media = False
    no_mute = False

    try:
        for mem in msg.new_chat_members:
            user_id = mem.id
    except:
        LOGGER.log("User being added or no ID found for user.")
    
    gban_checks = get_gbanned_user(user_id)
    
    join_log = sql.get_join_event_pref(chat.id)
    join_members = msg.new_chat_members

    if should_welc:
        sent = None
        new_members = msg.new_chat_members
        for new_mem in new_members:
            spamwatch_banned = client.get_ban(new_mem.id)
            user_add = context.bot.get_chat_member(chat.id, user.id)

            # edge case of empty name - occurs for some bugs.
            first_name = new_mem.first_name or "PersonWithNoName"
            # Give the owner a special welcome
            log = ""
            if join_log == True:
                log += "<b>{}:</b>" \
                       "\n#WELCOME" \
                       "\n<b>A new user has joined:</b>" \
                       "\n<b>• User:</b> {}" \
                       "\n<b>• ID:</b> <code>{}</code>".format(escape(chat.title), mention_html(mem.id, mem.first_name), mem.id)
                if user.id != mem.id:
                    log += "\n<b>• Added by:</b> {}".format(mention_html(user.id, user.first_name))

            if new_mem.id == OWNER_ID:
                update.effective_message.reply_text("Master is in the houseeee, let's get this party started!")
                continue

            #### BAN CHECKERS ####
            # Ignore welc messages for gbanned users
            if gban_checks:
                continue

            # Ignore welc messages for SpamWatch banned users
            if spamwatch_banned:
                continue

            # Make bot greet admins
            elif new_mem.id == context.bot.id:
                update.effective_message.reply_text("Hey {}, I'm {}! Thank you for adding me to {}" 
                " and be sure to check /help in PM for more commands and tricks!".format(user.first_name, 
                                                                                         context.bot.first_name, chat_name))

            else:
                # If welcome message is media, send with appropriate function
                if welc_type != sql.Types.TEXT and welc_type != sql.Types.BUTTON_TEXT:
                    if welc_type == sql.Types.PHOTO:
                        media = True
                        type = 'photo'
                        if welc_caption:
                            res = format_welcome_message(welc_caption, first_name, chat, new_mem)
                            buttons = sql.get_welc_buttons(chat.id)
                            keyb = build_keyboard(buttons)

                        keyboard = InlineKeyboardMarkup(keyb)

                        sent = send(update, context, cust_welcome, keyboard,
                                    sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name), res, type)

                        if sent:
                            sql.set_clean_welcome(chat.id, sent.message_id)
                    elif welc_type == sql.Types.VIDEO:
                        media = True
                        type = 'video'
                        if welc_caption:
                            res = format_welcome_message(welc_caption, first_name, chat, new_mem)
                            buttons = sql.get_welc_buttons(chat.id)
                            keyb = build_keyboard(buttons)

                        keyboard = InlineKeyboardMarkup(keyb)

                        sent = send(update, context, cust_welcome, keyboard,
                                    sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name), res, type)

                        if sent:
                            sql.set_clean_welcome(chat.id, sent.message_id)
                    elif welc_type == sql.Types.DOCUMENT:
                        media = True
                        type = 'document'
                        if welc_caption:
                            res = format_welcome_message(
                                welc_caption, first_name, chat, new_mem)
                            buttons = sql.get_welc_buttons(chat.id)
                            keyb = build_keyboard(buttons)

                        keyboard = InlineKeyboardMarkup(keyb)

                        sent = send(update, context, cust_welcome, keyboard,
                                    sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name), res, type)

                        if sent:
                            sql.set_clean_welcome(chat.id, sent.message_id)
                    else:
                        media = True
                        ENUM_FUNC_MAP[welc_type](chat.id, cust_welcome)

                # else, move on
                if media == False:
                    if welc_caption:
                        res = format_welcome_message(welc_caption, first_name, chat, new_mem)
                        buttons = sql.get_welc_buttons(chat.id)
                        keyb = build_keyboard(buttons)
                    else:
                        res = sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name)
                        keyb = []

                    keyboard = InlineKeyboardMarkup(keyb)

                    sent = send(update, context, res, keyboard,
                                sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name))  # type: Optional[Message]
                #User exception from mutes:
                if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)) or human_checks or gban_checks:
                    no_mute = True

                if not no_mute:
                    #Join welcome: soft mute
                    if welc_mutes == "soft":
                        context.bot.restrict_chat_member(chat.id, new_mem.id, WELCOME_PERMISSIONS_SOFT,
                                                        until_date=(int(time.time() + 24 * 60 * 60)))
                    #Join welcome: strong mute
                    if welc_mutes == "strong":
                        mute_message = msg.reply_text("Hey {} (`{}`),\nClick the button below to prove you're human:".format(new_mem.first_name, 
                                                                                                                            new_mem.id),
                            reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(text="Tap here to speak", 
                            callback_data="user_join_({})".format(new_mem.id))]]), parse_mode=ParseMode.MARKDOWN)
                        context.bot.restrict_chat_member(chat.id, new_mem.id, WELCOME_PERMISSIONS_STRONG)

                    #Join welcome: aggressive mute
                    elif welc_mutes == "aggressive":
                        mute_message = msg.reply_text("Hey {} (`{}`),\nClick the button below to prove you're human:".format(new_mem.first_name, 
                                                                                                                            new_mem.id), 
                            reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(text="Tap here to speak",
                            callback_data="user_join_({})".format(new_mem.id))]]), parse_mode=ParseMode.MARKDOWN)
                        context.bot.restrict_chat_member(chat.id, new_mem.id, WELCOME_PERMISSIONS_AGGRESSIVE)
            delete_join(update, context)

            if prev_welc:
                try:
                    context.bot.delete_message(chat.id, prev_welc)
                except BadRequest as excp:
                    pass

            if sent:
                sql.set_clean_welcome(chat.id, sent.message_id)

            if not human_checks and welc_mutes == "aggressive":
                t = threading.Thread(target=aggr_mute_check, args=(
                    context.bot, chat, mute_message.message_id, new_mem.id,))
                t.start()

            return log

    return ""