コード例 #1
0
ファイル: __init__.py プロジェクト: Jp-31/bobobot
def __list_all_modules():
    from os.path import dirname, basename, isfile
    import glob
    # This generates a list of modules in this folder for the * in __main__ to work.
    mod_paths = glob.glob(dirname(__file__) + "/*.py")
    all_modules = [
        basename(f)[:-3] for f in mod_paths
        if isfile(f) and f.endswith(".py") and not f.endswith('__init__.py')
    ]  # RTL is broken atm

    if LOAD or NO_LOAD:
        to_load = LOAD
        if to_load:
            if not all(
                    any(mod == module_name for module_name in all_modules)
                    for mod in to_load):
                LOGGER.error("Invalid loadorder names. Quitting.")
                quit(1)

        else:
            to_load = all_modules

        if NO_LOAD:
            LOGGER.info("Not loading: {}".format(NO_LOAD))
            return [item for item in to_load if item not in NO_LOAD]

        return to_load

    return all_modules
コード例 #2
0
ファイル: users.py プロジェクト: Jp-31/bobobot
def get_user_id(username):
    # ensure valid userid
    if len(username) <= 5:
        return None

    if username.startswith('@'):
        username = username[1:]

    users = sql.get_userid_by_name(username)

    if not users:
        return None

    elif len(users) == 1:
        return users[0].user_id

    else:
        for user_obj in users:
            try:
                userdat = dispatcher.bot.get_chat(user_obj.user_id)
                if userdat.username == username:
                    return userdat.id

            except BadRequest as excp:
                if excp.message == 'Chat not found':
                    pass
                else:
                    LOGGER.exception("Error extracting user ID")

    return None
コード例 #3
0
ファイル: lyrics.py プロジェクト: Jp-31/bobobot
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)
コード例 #4
0
ファイル: global_bans.py プロジェクト: Jp-31/bobobot
def gban_notification(update: Update,
                      context: CallbackContext,
                      user_info,
                      should_message=True):
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    chat_member = user_info
    user_r = sql.get_gbanned_user(chat_member.user.id)


    chatban_text = "User {} is currently globally banned and is removed from {} with an " \
                   "immediate effect.".format(mention_html(chat_member.user.id,
                                                           chat_member.user.first_name
                                                           or "Deleted Account"), chat.title)

    if sql.is_user_gbanned(chat_member.user.id):
        if user_r.reason:
            chatban_text += "\n<b>Reason</b>: {}".format(user_r.reason)

        if should_message:
            try:
                msg.reply_text(chatban_text, parse_mode=ParseMode.HTML)
            except:
                context.bot.send_message(chat.id,
                                         chatban_text,
                                         parse_mode=ParseMode.HTML)
                LOGGER.exception("Reply with gban notification.")
コード例 #5
0
ファイル: chat_status.py プロジェクト: Jp-31/bobobot
 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.")
コード例 #6
0
ファイル: welcome.py プロジェクト: Jp-31/bobobot
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")
コード例 #7
0
ファイル: chat_status.py プロジェクト: Jp-31/bobobot
 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.")
コード例 #8
0
ファイル: global_bans.py プロジェクト: Jp-31/bobobot
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.")
コード例 #9
0
def help_button(update: Update, context: CallbackContext):
    query = update.callback_query
    mod_match = re.match(r"help_module\((.+?)\)", query.data)
    prev_match = re.match(r"help_prev\((.+?)\)", query.data)
    next_match = re.match(r"help_next\((.+?)\)", query.data)
    back_match = re.match(r"help_back", query.data)
    try:
        if mod_match:
            module = mod_match.group(1)
            text = HELPABLE[module].__help__
            query.message.reply_text(text=text,
                                     parse_mode=ParseMode.MARKDOWN,
                                     reply_markup=InlineKeyboardMarkup([[
                                         InlineKeyboardButton(
                                             text="Back",
                                             callback_data="help_back")
                                     ]]))

        elif prev_match:
            curr_page = int(prev_match.group(1))
            query.message.reply_text(HELP_STRINGS,
                                     parse_mode=ParseMode.MARKDOWN,
                                     reply_markup=InlineKeyboardMarkup(
                                         paginate_modules(
                                             curr_page - 1, HELPABLE, "help")))

        elif next_match:
            next_page = int(next_match.group(1))
            query.message.reply_text(HELP_STRINGS,
                                     parse_mode=ParseMode.MARKDOWN,
                                     reply_markup=InlineKeyboardMarkup(
                                         paginate_modules(
                                             next_page + 1, HELPABLE, "help")))

        elif back_match:
            query.message.reply_text(text=HELP_STRINGS,
                                     parse_mode=ParseMode.MARKDOWN,
                                     reply_markup=InlineKeyboardMarkup(
                                         paginate_modules(0, HELPABLE,
                                                          "help")))

        # ensure no spinny white circle
        context.bot.answer_callback_query(query.id)
        query.message.delete()
    except BadRequest as excp:
        if excp.message == "Message is not modified":
            pass
        elif excp.message == "Query_id_invalid":
            pass
        elif excp.message == "Message can't be deleted":
            pass
        else:
            LOGGER.exception("Exception in help buttons. %s", str(query.data))
コード例 #10
0
ファイル: sed.py プロジェクト: Jp-31/bobobot
def sed(update: Update, context: CallbackContext):
    sed_result = separate_sed(update.effective_message.text)
    if sed_result and update.effective_message.reply_to_message:
        if update.effective_message.reply_to_message.text:
            to_fix = update.effective_message.reply_to_message.text
        elif update.effective_message.reply_to_message.caption:
            to_fix = update.effective_message.reply_to_message.caption
        else:
            return

        repl, repl_with, flags = sed_result

        if not repl:
            update.effective_message.reply_to_message.reply_text(
                "You're trying to replace... "
                "nothing with something?")
            return

        try:
            check = re.match(repl, to_fix, flags=re.IGNORECASE)

            if check and check.group(0).lower() == to_fix.lower():
                update.effective_message.reply_to_message.reply_text(
                    "Hey everyone, {} is trying to make "
                    "me say stuff I don't wanna "
                    "say!".format(update.effective_user.first_name))
                return

            if 'i' in flags and 'g' in flags:
                text = re.sub(repl, repl_with, to_fix, flags=re.I).strip()
            elif 'i' in flags:
                text = re.sub(repl, repl_with, to_fix, count=1,
                              flags=re.I).strip()
            elif 'g' in flags:
                text = re.sub(repl, repl_with, to_fix).strip()
            else:
                text = re.sub(repl, repl_with, to_fix, count=1).strip()
        except sre_constants.error:
            LOGGER.warning(update.effective_message.text)
            LOGGER.exception("SRE constant error")
            update.effective_message.reply_text(
                "Do you even sed? Apparently not.")
            return

        # empty string errors -_-
        if len(text) >= telegram.MAX_MESSAGE_LENGTH:
            update.effective_message.reply_text(
                "The result of the sed command was too long for \
                                                 telegram!")
        elif text:
            update.effective_message.reply_to_message.reply_text(text)
コード例 #11
0
ファイル: locks.py プロジェクト: Jp-31/bobobot
def rest_handler(update: Update, context: CallbackContext):
    msg = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    for restriction, filter in RESTRICTION_TYPES.items():
        if filter(update) and sql.is_restr_locked(
                chat.id, restriction) and can_delete(chat, context.bot.id):
            try:
                msg.delete()
            except BadRequest as excp:
                if excp.message == "Message to delete not found":
                    pass
                else:
                    LOGGER.exception("ERROR in restrictions")
            break
コード例 #12
0
ファイル: chat_status.py プロジェクト: Jp-31/bobobot
 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)))
コード例 #13
0
ファイル: backups.py プロジェクト: Jp-31/bobobot
def import_data(update: Update, context: CallbackContext):
    msg = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    # TODO: allow uploading doc with command, not just as reply
    # only work with a doc
    if msg.reply_to_message and msg.reply_to_message.document:
        try:
            file_info = context.bot.get_file(
                msg.reply_to_message.document.file_id)
        except BadRequest:
            msg.reply_text(
                "Try downloading and reuploading the file as yourself before importing - this one seems "
                "to be iffy!")
            return

        with BytesIO() as file:
            file_info.download(out=file)
            file.seek(0)
            data = json.load(file)

        # only import one group
        if len(data) > 1 and str(chat.id) not in data:
            msg.reply_text(
                "Theres more than one group here in this file, and none have the same chat id as this group "
                "- how do I choose what to import?")
            return

        # Select data source
        if str(chat.id) in data:
            data = data[str(chat.id)]['hashes']
        else:
            data = data[list(data.keys())[0]]['hashes']

        try:
            for mod in DATA_IMPORT:
                mod.__import_data__(str(chat.id), data)
        except Exception:
            msg.reply_text(
                "An exception occured while restoring your data. The process may not be complete. If "
                "you're having issues with this, message @MarieSupport with your backup file so the "
                "issue can be debugged. My owners would be happy to help, and every bug "
                "reported makes me better! Thanks! :)")
            LOGGER.exception("Import for chatid %s with name %s failed.",
                             str(chat.id), str(chat.title))
            return

        # TODO: some of that link logic
        # NOTE: consider default permissions stuff?
        msg.reply_text("Backup fully imported. Welcome back! :D")
コード例 #14
0
def sban(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(" ")

    update.effective_message.delete()

    user_id, reason = extract_user_and_text(message, args)

    if not user_id:
        return ""

    try:
        member = chat.get_member(user_id)
    except BadRequest as excp:
        if excp.message == "User not found":
            return ""
        else:
            raise

    if is_user_ban_protected(chat, user_id, member):
        return ""

    if user_id == context.bot.id:
        return ""

    log = "<b>{}:</b>" \
          "\n#SILENT_BAN" \
          "\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)
    if reason:
        log += "\n<b>• Reason:</b> {}".format(reason)

    try:
        chat.kick_member(user_id)
        return log

    except BadRequest as excp:
        if excp.message == "Reply message not found":
            return log
        else:
            LOGGER.warning(update)
            LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s",
                             user_id, chat.title, chat.id, excp.message)
    return ""
コード例 #15
0
def migrate_chats(update: Update, context: CallbackContext):
    msg = update.effective_message  # type: Optional[Message]
    if msg.migrate_to_chat_id:
        old_chat = update.effective_chat.id
        new_chat = msg.migrate_to_chat_id
    elif msg.migrate_from_chat_id:
        old_chat = msg.migrate_from_chat_id
        new_chat = update.effective_chat.id
    else:
        return

    LOGGER.info("Migrating from %s, to %s", str(old_chat), str(new_chat))
    for mod in MIGRATEABLE:
        mod.__migrate__(old_chat, new_chat)

    LOGGER.info("Successfully migrated!")
    raise DispatcherHandlerStop
コード例 #16
0
ファイル: users.py プロジェクト: Jp-31/bobobot
def broadcast(update: Update, context: CallbackContext):
    to_send = update.effective_message.text.split(None, 1)
    if len(to_send) >= 2:
        chats = sql.get_all_chats() or []
        failed = 0
        for chat in chats:
            try:
                context.bot.sendMessage(int(chat.chat_id), to_send[1])
                sleep(0.1)
            except TelegramError:
                failed += 1
                LOGGER.warning("Couldn't send broadcast to %s, group name %s",
                               str(chat.chat_id), str(chat.chat_name))

        update.effective_message.reply_text(
            "Broadcast complete. {} groups failed to receive the message, probably "
            "due to being kicked.".format(failed))
コード例 #17
0
ファイル: blacklist.py プロジェクト: Jp-31/bobobot
def del_blacklist(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[Message]
    to_match = extract_text(message)
    if not to_match:
        return

    chat_filters = sql.get_chat_blacklist(chat.id)
    for trigger in chat_filters:
        pattern = r"( |^|[^\w])" + re.escape(trigger) + r"( |$|[^\w])"
        if re.search(pattern, to_match, flags=re.IGNORECASE):
            try:
                message.delete()
            except BadRequest as excp:
                if excp.message == "Message to delete not found":
                    pass
                else:
                    LOGGER.exception("Error while deleting blacklist message.")
            break
コード例 #18
0
def main():
    test_handler = CustomCommandHandler(CMD_PREFIX, "test", test)
    start_handler = CustomCommandHandler(CMD_PREFIX, "start", start)

    help_handler = CustomCommandHandler(CMD_PREFIX, "help", get_help)
    help_callback_handler = CallbackQueryHandler(help_button, pattern=r"help_")

    settings_handler = CustomCommandHandler(CMD_PREFIX, "settings",
                                            get_settings)
    settings_callback_handler = CallbackQueryHandler(settings_button,
                                                     pattern=r"stngs_")

    donate_handler = CustomCommandHandler(CMD_PREFIX, "donate", donate)
    migrate_handler = MessageHandler(Filters.status_update.migrate,
                                     migrate_chats)

    # dispatcher.add_handler(test_handler)
    dispatcher.add_handler(start_handler)
    dispatcher.add_handler(help_handler)
    dispatcher.add_handler(settings_handler)
    dispatcher.add_handler(help_callback_handler)
    dispatcher.add_handler(settings_callback_handler)
    dispatcher.add_handler(migrate_handler)
    dispatcher.add_handler(donate_handler)

    # dispatcher.add_error_handler(error_callback)

    if WEBHOOK:
        LOGGER.info("Using webhooks.")
        updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)

        if CERT_PATH:
            updater.bot.set_webhook(url=URL + TOKEN,
                                    certificate=open(CERT_PATH, 'rb'))
        else:
            updater.bot.set_webhook(url=URL + TOKEN)

    else:
        LOGGER.info("Using long polling.")
        updater.start_polling(timeout=15, read_latency=4)

    updater.idle()
コード例 #19
0
ファイル: chat_status.py プロジェクト: Jp-31/bobobot
    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.")
コード例 #20
0
ファイル: log_channel.py プロジェクト: Jp-31/bobobot
        def log_action(update: Update, context: CallbackContext, *args,
                       **kwargs):
            result = func(update, context, *args, **kwargs)
            chat = update.effective_chat  # type: Optional[Chat]
            message = update.effective_message  # type: Optional[Message]
            if result:
                if chat.type == chat.SUPERGROUP and chat.username:
                    result += "\n<b>• Link:</b> " \
                              "<a href=\"http://telegram.me/{}/{}\">go to message</a>".format(chat.username,
                                                                                           message.message_id)
                log_chat = sql.get_chat_log_channel(chat.id)
                if log_chat:
                    send_log(context, log_chat, chat.id, result)
            elif result == "":
                pass
            else:
                LOGGER.warning(
                    "%s was set as loggable, but had no return statement.",
                    func)

            return result
コード例 #21
0
ファイル: global_mutes.py プロジェクト: Jp-31/bobobot
def gmute_notification(update: Update, context: CallbackContext, user_id, should_message=False):
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    user = context.bot.get_chat(user_id)
    user_r = sql.get_gmuted_user(user_id)
    
    
    chatmute_text = "User {} is currently globally muted and is silenced from {} with an " \
                   "immediate effect.".format(mention_html(user.id, user.first_name or "Deleted Account"), chat.title)
    
    if sql.is_user_gmuted(user_id):
        if user_r.reason:
            chatmute_text += "\n<b>Reason</b>: {}".format(user_r.reason)
                
        if should_message:
            try:
                msg.reply_text(chatmute_text, parse_mode=ParseMode.HTML)
            except:
                context.bot.send_message(chat.id, 
                                         chatmute_text, parse_mode=ParseMode.HTML)
                LOGGER.exception()
コード例 #22
0
ファイル: welcome.py プロジェクト: Jp-31/bobobot
def send(update, context, message, keyboard, backup_message, caption=None, type=None):
    try:
        if caption and type == 'photo':
            msg = update.effective_message.reply_photo(message, caption, parse_mode=ParseMode.HTML, reply_markup=keyboard)
        elif caption and type == 'video':
            msg = update.effective_message.reply_video(message, duration=None, caption=caption, parse_mode=ParseMode.HTML, reply_markup=keyboard)
        elif caption and type == 'document':
            msg = update.effective_message.reply_document(message, filename=None, caption=caption, parse_mode=ParseMode.HTML, reply_markup=keyboard)
        else:
            msg = update.effective_message.reply_text(message, parse_mode=ParseMode.HTML, reply_markup=keyboard)
    except IndexError:
        msg = update.effective_message.reply_text(markdown_parser(backup_message +
                                                                  "\nNote: the current message was "
                                                                  "invalid due to markdown issues. Could be "
                                                                  "due to the user's name."),
                                                  parse_mode=ParseMode.MARKDOWN)
    except KeyError:
        msg = update.effective_message.reply_text(markdown_parser(backup_message +
                                                                  "\nNote: the current message is "
                                                                  "invalid due to an issue with some misplaced "
                                                                  "curly brackets. Please update"),
                                                  parse_mode=ParseMode.MARKDOWN)
    except BadRequest as excp:
        if excp.message == "Button_url_invalid":
            msg = update.effective_message.reply_text(markdown_parser(backup_message +
                                                                      "\nNote: the current message has an invalid url "
                                                                      "in one of its buttons. Please update."),
                                                      parse_mode=ParseMode.MARKDOWN)
        elif excp.message == "Unsupported url protocol":
            msg = update.effective_message.reply_text(markdown_parser(backup_message +
                                                                      "\nNote: the current message has buttons which "
                                                                      "use url protocols that are unsupported by "
                                                                      "telegram. Please update."),
                                                      parse_mode=ParseMode.MARKDOWN)
        elif excp.message == "Wrong url host":
            msg = update.effective_message.reply_text(markdown_parser(backup_message +
                                                                      "\nNote: the current message has some bad urls. "
                                                                      "Please update."),
                                                      parse_mode=ParseMode.MARKDOWN)
            LOGGER.warning(message)
            LOGGER.warning(keyboard)
            LOGGER.exception("Could not parse! got invalid url host errors")
        else:
            msg = context.bot.send_message(update.effective_chat.id, markdown_parser(backup_message +
                                                                      "\nNote: An error occured when sending the "
                                                                      "custom message. Please update."),
                                                      parse_mode=ParseMode.MARKDOWN)
            LOGGER.exception("Couldn't send a welcome message: {}".format(excp))

    return msg
コード例 #23
0
ファイル: log_channel.py プロジェクト: Jp-31/bobobot
    def setlog(update: Update, context: CallbackContext):
        message = update.effective_message  # type: Optional[Message]
        chat = update.effective_chat  # type: Optional[Chat]
        if chat.type == chat.CHANNEL:
            message.reply_text(
                "Now, forward the /setlog to the group you want to tie this channel to!"
            )

        elif message.forward_from_chat:
            sql.set_chat_log_channel(chat.id, message.forward_from_chat.id)
            try:
                message.delete()
            except BadRequest as excp:
                if excp.message == "Message to delete not found":
                    pass
                else:
                    LOGGER.exception(
                        "Error deleting message in log channel. Should work anyway though."
                    )

            try:
                context.bot.send_message(
                    message.forward_from_chat.id,
                    "This channel has been set as the log channel for {}.".
                    format(chat.title or chat.first_name))
            except Unauthorized as excp:
                if excp.message == "Forbidden: bot is not a member of the channel chat":
                    context.bot.send_message(chat.id,
                                             "Successfully set log channel!")
                else:
                    LOGGER.exception("ERROR in setting the log channel.")

            context.bot.send_message(chat.id, "Successfully set log channel!")

        else:
            message.reply_text("The steps to set a log channel are:\n"
                               " - add bot to the desired channel\n"
                               " - send /setlog to the channel\n"
                               " - forward the /setlog to the group\n")
コード例 #24
0
ファイル: log_channel.py プロジェクト: Jp-31/bobobot
    def send_log(context, log_chat_id: str, orig_chat_id: str, result: str):
        try:
            context.bot.send_message(log_chat_id,
                                     result,
                                     parse_mode=ParseMode.HTML)
        except BadRequest as excp:
            if excp.message == "Chat not found":
                context.bot.send_message(
                    orig_chat_id,
                    "This log channel has been deleted - unsetting.")
                sql.stop_chat_logging(orig_chat_id)
            else:
                LOGGER.warning(excp.message)
                LOGGER.warning(result)
                LOGGER.exception("Could not parse")

                context.bot.send_message(
                    log_chat_id, result +
                    "\n\nFormatting has been disabled due to an unexpected error."
                )
コード例 #25
0
def settings_button(update: Update, context: CallbackContext):
    query = update.callback_query
    user = update.effective_user
    mod_match = re.match(r"stngs_module\((.+?),(.+?)\)", query.data)
    prev_match = re.match(r"stngs_prev\((.+?),(.+?)\)", query.data)
    next_match = re.match(r"stngs_next\((.+?),(.+?)\)", query.data)
    back_match = re.match(r"stngs_back\((.+?)\)", query.data)
    try:
        if mod_match:
            chat_id = mod_match.group(1)
            module = mod_match.group(2)
            chat = context.bot.get_chat(chat_id)
            text = "*{}* has the following settings for the *{}* module:\n\n".format(escape_markdown(chat.title),
                                                                                     CHAT_SETTINGS[module].__mod_name__) + \
                   CHAT_SETTINGS[module].__chat_settings__(chat_id, user.id)
            query.message.reply_text(
                text=text,
                parse_mode=ParseMode.MARKDOWN,
                reply_markup=InlineKeyboardMarkup([[
                    InlineKeyboardButton(
                        text="Back",
                        callback_data="stngs_back({})".format(chat_id))
                ]]))

        elif prev_match:
            chat_id = prev_match.group(1)
            curr_page = int(prev_match.group(2))
            chat = context.bot.get_chat(chat_id)
            query.message.reply_text(
                "Hi there! There are quite a few settings for {} - go ahead and pick what "
                "you're interested in.".format(chat.title),
                reply_markup=InlineKeyboardMarkup(
                    paginate_modules(curr_page - 1,
                                     CHAT_SETTINGS,
                                     "stngs",
                                     chat=chat_id)))

        elif next_match:
            chat_id = next_match.group(1)
            next_page = int(next_match.group(2))
            chat = context.bot.get_chat(chat_id)
            query.message.reply_text(
                "Hi there! There are quite a few settings for {} - go ahead and pick what "
                "you're interested in.".format(chat.title),
                reply_markup=InlineKeyboardMarkup(
                    paginate_modules(next_page + 1,
                                     CHAT_SETTINGS,
                                     "stngs",
                                     chat=chat_id)))

        elif back_match:
            chat_id = back_match.group(1)
            chat = context.bot.get_chat(chat_id)
            query.message.reply_text(
                text=
                "Hi there! There are quite a few settings for {} - go ahead and pick what "
                "you're interested in.".format(escape_markdown(chat.title)),
                parse_mode=ParseMode.MARKDOWN,
                reply_markup=InlineKeyboardMarkup(
                    paginate_modules(0, CHAT_SETTINGS, "stngs", chat=chat_id)))

        # ensure no spinny white circle
        context.bot.answer_callback_query(query.id)
        query.message.delete()
    except BadRequest as excp:
        if excp.message == "Message is not modified":
            pass
        elif excp.message == "Query_id_invalid":
            pass
        elif excp.message == "Message can't be deleted":
            pass
        else:
            LOGGER.exception("Exception in settings buttons. %s",
                             str(query.data))
コード例 #26
0
ファイル: msg_deleting.py プロジェクト: Jp-31/bobobot
def purge(update: Update, context: CallbackContext) -> str:
    msg = update.effective_message  # type: Optional[Message]
    args = msg.text.split(" ")
    if msg.reply_to_message:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        if can_delete(chat, context.bot.id):
            message_id = msg.reply_to_message.message_id
            delete_to = msg.message_id - 1
            if args and len(args) > 1 and args[1].isdigit():
                new_del = message_id + (int(args[1]) - 1)
                # No point deleting messages which haven't been written yet.
                if new_del < delete_to:
                    delete_to = new_del

            for m_id in range(delete_to, message_id - 1,
                              -1):  # Reverse iteration over message ids
                try:
                    context.bot.deleteMessage(chat.id, m_id)
                except BadRequest as err:
                    if err.message == "Message can't be deleted":
                        context.bot.send_message(
                            chat.id,
                            "Cannot delete all messages. The messages may be too old, I might "
                            "not have delete rights, or this might not be a supergroup."
                        )

                    elif err.message != "Message to delete not found":
                        LOGGER.exception("Error while purging chat messages.")

            try:
                msg.delete()
            except BadRequest as err:
                if err.message == "Message can't be deleted":
                    context.bot.send_message(
                        chat.id,
                        "Cannot delete all messages. The messages may be too old, I might "
                        "not have delete rights, or this might not be a supergroup."
                    )

                elif err.message != "Message to delete not found":
                    LOGGER.exception("Error while purging chat messages.")

            purge_msg = context.bot.send_message(
                chat.id, "Purged {} messages. "
                "\nThis message will be self-destructed in 3 seconds.".format(
                    str((delete_to - message_id) + 1)))
            time.sleep(3)
            purge_msg.delete()
            return "<b>{}:</b>" \
                   "\n#PURGE" \
                   "\n<b>• Admin:</b> {}" \
                   "\nPurged <code>{}</code> messages.".format(html.escape(chat.title),
                                                               mention_html(user.id, user.first_name),
                                                               (delete_to - message_id) + 1)

    else:
        msg.reply_text(
            "Reply to a message to select where to start purging from.")

    return ""
コード例 #27
0
ファイル: muting.py プロジェクト: Jp-31/bobobot
def temp_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, reason = extract_user_and_text(message, args)

    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return ""

    try:
        member = chat.get_member(user_id)
    except BadRequest as excp:
        if excp.message == "User not found":
            message.reply_text("I can't seem to find this user")
            return ""
        else:
            raise

    if is_user_admin(chat, user_id, member):
        message.reply_text("I really wish I could restrict admins...")
        return ""

    if user_id == context.bot.id:
        message.reply_text("I'm not gonna RESTRICT myself, are you crazy?")
        return ""

    if not reason:
        message.reply_text(
            "You haven't specified a time to restrict this user for!")
        return ""

    split_reason = reason.split(None, 1)

    time_val = split_reason[0].lower()
    if len(split_reason) > 1:
        reason = split_reason[1]
    else:
        reason = ""

    mutetime = extract_time(message, time_val)

    if not mutetime:
        return ""

    log = "<b>{}:</b>" \
          "\n#TEMP RESTRICTED" \
          "\n<b>• Admin:</b> {}" \
          "\n<b>• User:</b> {}" \
          "\n<b>• ID:</b> <code>{}</code>" \
          "\n<b>• Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name),
                                       mention_html(member.user.id, member.user.first_name), user_id, time_val)
    if reason:
        log += "\n<b>• Reason:</b> {}".format(reason)

    try:
        if member.can_send_messages is None or member.can_send_messages:
            context.bot.restrict_chat_member(chat.id,
                                             user_id,
                                             NOMEDIA_PERMISSIONS,
                                             until_date=mutetime)
            message.reply_text(
                "{} restricted from sending media for {}!".format(
                    mention_html(member.user.id, member.user.first_name),
                    time_val),
                parse_mode=ParseMode.HTML)
            return log
        else:
            message.reply_text("This user is already restricted.")

    except BadRequest as excp:
        if excp.message == "Reply message not found":
            # Do not reply
            message.reply_text(
                "{} restricted from sending media for {}!".format(
                    mention_html(member.user.id, member.user.first_name),
                    time_val),
                parse_mode=ParseMode.HTML,
                quote=False)
            return log
        else:
            LOGGER.warning(update)
            LOGGER.exception("ERROR muting user %s in chat %s (%s) due to %s",
                             user_id, chat.title, chat.id, excp.message)
            message.reply_text("Well damn, I can't restrict that user.")

    return ""
コード例 #28
0
ファイル: locks.py プロジェクト: Jp-31/bobobot
def del_lockables(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[Message]

    for lockable, filter in LOCK_TYPES.items():
        if lockable == "rtl":
            if sql.is_locked(chat.id, lockable) and can_delete(
                    chat, context.bot.id):
                if message.caption:
                    check = ad.detect_alphabet(u'{}'.format(message.caption))
                    if 'ARABIC' in check:
                        try:
                            message.delete()
                        except BadRequest as excp:
                            if excp.message == "Message to delete not found":
                                pass
                            else:
                                LOGGER.exception("ERROR in lockables")
                if message.text:
                    check = ad.detect_alphabet(u'{}'.format(message.text))
                    if 'ARABIC' in check:
                        try:
                            message.delete()
                        except BadRequest as excp:
                            if excp.message == "Message to delete not found":
                                pass
                            else:
                                LOGGER.exception("ERROR in lockables")
            break
        if filter(update) and sql.is_locked(chat.id, lockable) and can_delete(
                chat, context.bot.id):
            if lockable == "bots":
                new_members = update.effective_message.new_chat_members
                for new_mem in new_members:
                    if new_mem.is_bot:
                        if not is_bot_admin(chat, context.bot.id):
                            message.reply_text(
                                "I see a bot, and I've been told to stop them joining... "
                                "but I'm not admin!")
                            return

                        chat.kick_member(new_mem.id)
                        message.reply_text(
                            "Only admins are allowed to add bots to this chat! Get outta here."
                        )
            else:
                chat_id = chat.id
                whitelist = get_whitelisted_urls(chat_id)
                for i in whitelist:
                    if i.__eq__(message.text):
                        return
                try:
                    message.delete()
                    lock_message = context.bot.send_message(
                        chat.id,
                        "Message deleted because it contained a locked item: {}."
                        .format(lockable))
                    time.sleep(2)
                    lock_message.delete()
                except BadRequest as excp:
                    if excp.message == "Message to delete not found":
                        pass
                    else:
                        LOGGER.exception("ERROR in lockables bots")

            break
コード例 #29
0
def temp_ban(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, reason = extract_user_and_text(message, args)

    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return ""

    try:
        member = chat.get_member(user_id)
    except BadRequest as excp:
        if excp.message == "User not found":
            message.reply_text("I can't seem to find this user")
            return ""
        else:
            raise

    if is_user_ban_protected(chat, user_id, member):
        message.reply_text("I really wish I could ban admins...")
        return ""

    if user_id == context.bot.id:
        message.reply_text("I'm not gonna BAN myself, are you crazy?")
        return ""

    if not reason:
        message.reply_text(
            "You haven't specified a time to ban this user for!")
        return ""

    split_reason = reason.split(None, 1)

    time_val = split_reason[0].lower()
    if len(split_reason) > 1:
        reason = split_reason[1]
    else:
        reason = ""

    bantime = extract_time(message, time_val)

    if not bantime:
        return ""

    log = "<b>{}:</b>" \
          "\n#TEMPBAN" \
          "\n<b>• Admin:</b> {}" \
          "\n<b>• User:</b> {}" \
          "\n<b>• ID:</b> <code>{}</code>" \
          "\n<b>• Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name),
                                                                mention_html(member.user.id, member.user.first_name),
                                                                             user_id, time_val)
    if reason:
        log += "\n<b>• Reason:</b> {}".format(reason)

    try:
        chat.kick_member(user_id, until_date=bantime)
        context.bot.send_sticker(update.effective_chat.id,
                                 BAN_STICKER)  # banhammer marie sticker
        reply = "{} has been temporarily banned for {}!".format(
            mention_html(member.user.id, member.user.first_name), time_val)
        message.reply_text(reply, parse_mode=ParseMode.HTML)
        return log

    except BadRequest as excp:
        if excp.message == "Reply message not found":
            # Do not reply
            message.reply_text(
                "Banned! User will be banned for {}.".format(time_val),
                quote=False)
            return log
        else:
            LOGGER.warning(update)
            LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s",
                             user_id, chat.title, chat.id, excp.message)
            message.reply_text("Well damn, I can't ban that user.")

    return ""
コード例 #30
0
    dispatcher.add_handler(start_handler)
    dispatcher.add_handler(help_handler)
    dispatcher.add_handler(settings_handler)
    dispatcher.add_handler(help_callback_handler)
    dispatcher.add_handler(settings_callback_handler)
    dispatcher.add_handler(migrate_handler)
    dispatcher.add_handler(donate_handler)

    # dispatcher.add_error_handler(error_callback)

    if WEBHOOK:
        LOGGER.info("Using webhooks.")
        updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN)

        if CERT_PATH:
            updater.bot.set_webhook(url=URL + TOKEN,
                                    certificate=open(CERT_PATH, 'rb'))
        else:
            updater.bot.set_webhook(url=URL + TOKEN)

    else:
        LOGGER.info("Using long polling.")
        updater.start_polling(timeout=15, read_latency=4)

    updater.idle()


if __name__ == '__main__':
    LOGGER.info("Successfully loaded modules: " + str(ALL_MODULES))
    main()