示例#1
0
def process_timezone_message(message, database):
    log.info("Processing timezone")
    bldr = utils.str_bldr()

    timezones = re.findall(r'(?:timezone!? )([\w/]{1,50})',
                           message.body,
                           flags=re.IGNORECASE)
    if not len(timezones):
        log.debug("Couldn't find a timezone in your message")
        bldr.append("I couldn't find a timezone in your message.")

    elif timezones[0] not in pytz.common_timezones:
        log.debug(f"Invalid timezone: {timezones[0]}")
        bldr.append(f"{timezones[0]} is not a valid timezone.")

    else:
        user = database.get_or_add_user(message.author.name)
        if timezones[0] == "UTC":
            user.timezone = None
            bldr.append(f"Reset your timezone to the default")
        else:
            user.timezone = timezones[0]
            bldr.append(f"Updated your timezone to {timezones[0]}")

        log.info(f"u/{message.author.name} timezone updated to {timezones[0]}")

    return [''.join(bldr)]
示例#2
0
def process_clock_message(message, database):
    log.info("Processing clock")
    bldr = utils.str_bldr()

    clocks = re.findall(r'(?:clock!? +)([\d]{2})',
                        message.body,
                        flags=re.IGNORECASE)
    if not len(clocks):
        log.debug("Couldn't find a clock type in your message")
        bldr.append("I couldn't find a clock type in your message.")

    else:
        user = database.get_or_add_user(message.author.name)
        if clocks[0] == "24":
            user.time_format = None
            bldr.append(f"Reset your clock type to the default 24 hour clock")
        elif clocks[0] == "12":
            user.time_format = "12"
            bldr.append(f"Updated your clock type to a 12 hour clock")
        else:
            log.debug(f"Invalid clock type: {clocks[0]}")
            bldr.append(f"{clocks[0]} is not a valid clock type.")
            return bldr

        log.info(f"u/{message.author.name} clock type updated to {clocks[0]}")

    return [''.join(bldr)]
示例#3
0
def process_delete_comment(message, reddit, database):
    log.info("Processing delete comment")
    bldr = utils.str_bldr()

    ids = re.findall(r'delete!\s(\w+)', message.body, flags=re.IGNORECASE)
    if len(ids) == 0:
        log.debug("Couldn't find a thread id to delete")
        bldr.append("I couldn't find a thread id to delete.")
    else:
        db_comment = database.get_comment_by_thread(ids[0])
        if db_comment is not None:
            if db_comment.user == message.author.name:
                comment = reddit.get_comment(db_comment.comment_id)
                if not reddit.delete_comment(comment):
                    log.debug(
                        f"Unable to delete comment: {db_comment.comment_id}")
                    bldr.append("Something went wrong deleting the comment")
                else:
                    database.delete_comment(db_comment)
                    log.debug(f"Deleted comment: {db_comment.comment_id}")
                    bldr.append("Comment deleted.")
            else:
                log.debug(
                    f"Bot wasn't replying to owner: {db_comment.user} : {message.author.name}"
                )
                bldr.append("It looks like the bot wasn't replying to you.")
        else:
            log.debug(f"Comment doesn't exist: {ids[0]}")
            bldr.append("This comment doesn't exist or was already deleted.")

    return [''.join(bldr)]
示例#4
0
	def render_notification(self):
		bldr = utils.str_bldr()

		bldr.append("RemindMeBot reminder here!")
		bldr.append("\n\n")

		if self.message is not None:
			bldr.append("I'm here to remind you:\n\n> ")
			bldr.append(self.message)
			bldr.append("\n\n")

		bldr.append("The source comment or message:\n\n>")
		bldr.append(self.source)
		bldr.append("\n\n")

		if self.requested_date is None:
			bldr.append("This reminder was created before I started saving the creation date of reminders.")
		else:
			bldr.append("You requested this reminder on: ")
			bldr.append(utils.render_time(self.requested_date, self.timezone))
		bldr.append("\n\n")

		bldr.append("[Click here](")
		bldr.append(utils.build_message_link(
			static.ACCOUNT_NAME,
			"Reminder",
			f"[{self.message}]\n\n{static.TRIGGER}! "
		))
		bldr.append(") and set the time after the ")
		bldr.append(static.TRIGGER)
		bldr.append(" command to be reminded of the original comment again.")

		return bldr
示例#5
0
def process_remove_all_reminders(message, database):
    log.info("Processing remove all reminders message")

    current_reminders = get_reminders_string(message.author.name, database,
                                             True)

    reminders_deleted = database.delete_user_reminders(message.author.name)
    log.debug(f"Deleted {reminders_deleted} reminders")

    bldr = utils.str_bldr()
    if reminders_deleted != 0:
        bldr.append("Deleted **")
        bldr.append(str(reminders_deleted))
        bldr.append("** reminders.\n\n")

    cakeday = database.get_cakeday(message.author.name)
    if cakeday is not None:
        database.delete_cakeday(cakeday)
        bldr.append("Deleted cakeday reminder.\n\n")

    bldr.append("\n\n")
    bldr.append("*****")
    bldr.append("\n\n")

    bldr.extend(current_reminders)

    return bldr
示例#6
0
    def render_confirmation(self, timezone):
        bldr = utils.str_bldr()
        bldr.append("I will message you every year at ")
        bldr.append(
            utils.render_time(self.date_time, timezone, "%m-%d %H:%M:%S %Z"))
        bldr.append(" to remind you of your cakeday.")

        bldr.append("\n\n")

        bldr.append("[Click here](")
        bldr.append(
            utils.build_message_link(static.ACCOUNT_NAME,
                                     "Delete Cakeday Reminder",
                                     "Delete! cakeday"))
        bldr.append(") to delete this reminder.")

        return bldr
示例#7
0
	def render_comment_confirmation(self):
		bldr = utils.str_bldr()

		if self.defaulted:
			bldr.append("**Defaulted to one day.**\n\n")

		if self.timezone is not None:
			bldr.append(f"Your default time zone is set to `{self.timezone}`. ")

		bldr.append("I will be messaging you on ")
		bldr.append(utils.render_time(self.target_date, self.timezone))
		bldr.append(" to remind you of [**this link**](")
		bldr.append(utils.replace_np(self.source))
		bldr.append(")")

		bldr.append("\n\n")

		bldr.append("[**")
		if self.count_duplicates > 0:
			bldr.append(str(self.count_duplicates))
			bldr.append(" OTHERS CLICKED")
		else:
			bldr.append("CLICK")
		bldr.append(" THIS LINK**](")
		bldr.append(utils.build_message_link(
			static.ACCOUNT_NAME,
			"Reminder",
			f"[{self.source}]\n\n{static.TRIGGER}! "
			f"{utils.get_datetime_string(self.target_date, format_string='%Y-%m-%d %H:%M:%S %Z')}"
		))
		bldr.append(") to send a PM to also be reminded and to reduce spam.")

		if self.thread_id is not None:
			bldr.append("\n\n")
			bldr.append("^(Parent commenter can ) [^(delete this message to hide from others.)](")
			bldr.append(utils.build_message_link(
				static.ACCOUNT_NAME,
				"Delete Comment",
				f"Delete! {self.thread_id}"
			))
			bldr.append(")")

		return bldr
示例#8
0
def process_remove_reminder(message, database):
	log.info("Processing remove reminder message")
	bldr = utils.str_bldr()

	ids = re.findall(r'remove!\s(\d+)', message.body, flags=re.IGNORECASE)
	if len(ids) == 0:
		bldr.append("I couldn't find a reminder id to remove.")
	else:
		reminder = database.get_reminder(ids[0])
		if reminder is None or reminder.user.name != message.author.name:
			bldr.append("It looks like you don't own this reminder or it doesn't exist.")
		else:
			database.delete_reminder(reminder)
			bldr.append("Reminder deleted.")

	bldr.append("\n\n")
	bldr.append("*****")
	bldr.append("\n\n")

	bldr.extend(get_reminders_string(message.author.name, database))

	return bldr
示例#9
0
	def render_message_confirmation(self, comment_return=None):
		bldr = utils.str_bldr()
		if self.result_message is not None:
			bldr.append(self.result_message)
			bldr.append("\n\n")
		bldr.append("I will be messaging you on ")
		bldr.append(utils.render_time(self.target_date, self.timezone))
		bldr.append(" to remind you")
		if self.message is None:
			bldr.append(" of [**this link**](")
			bldr.append(self.source)
			bldr.append(")")
		else:
			bldr.append(": ")
			bldr.append(self.message)

		if comment_return is not None and comment_return in (
			ReturnType.FORBIDDEN,
			ReturnType.THREAD_LOCKED,
			ReturnType.DELETED_COMMENT,
			ReturnType.RATELIMIT,
			ReturnType.THREAD_REPLIED
		):
			bldr.append("\n\n")
			bldr.append("I'm sending this to you as a message instead of replying to your comment because ")
			if comment_return == ReturnType.FORBIDDEN:
				bldr.append("I'm not allowed to reply in this subreddit.")
			elif comment_return == ReturnType.THREAD_LOCKED:
				bldr.append("the thread is locked.")
			elif comment_return == ReturnType.DELETED_COMMENT:
				bldr.append("it was deleted before I could get to it.")
			elif comment_return == ReturnType.RATELIMIT:
				bldr.append("I'm new to this subreddit and have already replied to another thread here recently.")
			elif comment_return == ReturnType.THREAD_REPLIED:
				bldr.append("I've already replied to another comment in this thread.")

		return bldr
示例#10
0
def process_remove_reminder(message, database):
    log.info("Processing remove reminder message")
    bldr = utils.str_bldr()

    ids = re.findall(r'remove!\s(\d+)', message.body, flags=re.IGNORECASE)
    if len(ids) == 0:
        cakeday_string = re.findall(r'remove!\s(cakeday)',
                                    message.body,
                                    flags=re.IGNORECASE)
        if len(cakeday_string):
            cakeday = database.get_cakeday(message.author.name)
            if cakeday is None:
                bldr.append("You don't have a cakeday reminder set.")
            else:
                database.delete_cakeday(cakeday)
                bldr.append("Cakeday reminder deleted.")
        else:
            bldr.append("I couldn't find a reminder id to remove.")
    else:
        reminder = database.get_reminder(ids[0])
        if reminder is None or reminder.user != message.author.name:
            bldr.append(
                "It looks like you don't own this reminder or it doesn't exist."
            )
        else:
            if database.delete_reminder(reminder):
                bldr.append("Reminder deleted.")
            else:
                bldr.append("Something went wrong, reminder not deleted.")

    bldr.append("\n\n")
    bldr.append("*****")
    bldr.append("\n\n")

    bldr.extend(get_reminders_string(message.author.name, database))

    return bldr
示例#11
0
def get_reminders_string(user_name,
                         database,
                         previous=False,
                         include_all=False):
    result_messages = []
    bldr = utils.str_bldr()

    regular_reminders, recurring_reminders = database.get_user_reminders(
        user_name)
    if len(regular_reminders) or len(recurring_reminders):
        if previous:
            bldr.append("Your previous reminders:")
        else:
            bldr.append("Your current reminders:")
        bldr.append("\n\n")

        if len(regular_reminders) + len(recurring_reminders) > 1:
            bldr.append("[Click here to delete all your reminders](")
            bldr.append(
                utils.build_message_link(static.ACCOUNT_NAME, "Remove All",
                                         "RemoveAll!"))
            bldr.append(")\n\n")

        user = database.get_or_add_user(user_name)
        if user.timezone is not None:
            bldr.append("Your timezone is currently set to: `")
            bldr.append(user.timezone)
            bldr.append("`\n\n")

        for reminders in [recurring_reminders, regular_reminders]:
            if len(reminders):
                log.debug(f"Building list with {len(reminders)} reminders")
                add_list_header(bldr, reminders[0].recurrence is not None)

                for reminder in reminders:
                    bldr.append("|")
                    if "reddit.com" in reminder.source:
                        bldr.append("[Source](")
                        bldr.append(
                            utils.check_append_context_to_link(
                                reminder.source))
                        bldr.append(")")
                    else:
                        bldr.append(reminder.source)
                    bldr.append("|")
                    if reminder.message is not None:
                        bldr.append(reminder.message.replace("|", "|"))
                    bldr.append("|")
                    bldr.append(
                        utils.render_time(reminder.target_date, reminder.user))
                    bldr.append("|")
                    bldr.append(
                        utils.render_time_diff(utils.datetime_now(),
                                               reminder.target_date))
                    if reminder.recurrence is not None:
                        bldr.append("|")
                        bldr.append(reminder.recurrence)
                    bldr.append("|")
                    bldr.append("[Remove](")
                    bldr.append(
                        utils.build_message_link(static.ACCOUNT_NAME, "Remove",
                                                 f"Remove! {reminder.id}"))
                    bldr.append(")")
                    bldr.append("|\n")

                    if utils.bldr_length(bldr) > 9000:
                        if include_all:
                            result_messages.append(''.join(bldr))
                            bldr = []
                            add_list_header(
                                bldr, reminders[0].recurrence is not None)
                        else:
                            bldr.append("\nToo many reminders to display.")
                            break

                bldr.append("\n")

    else:
        bldr.append("You don't have any reminders.")

    result_messages.append(''.join(bldr))
    return result_messages
示例#12
0
    def render_notification(self):
        bldr = utils.str_bldr()
        bldr.append("RemindMeBot reminder here!")
        bldr.append("\n\n")

        if self.message is not None:
            bldr.append("I'm here to remind you:\n\n> ")
            bldr.append(self.message)
            bldr.append("\n\n")

        bldr.append("The source comment or message:\n\n>")
        bldr.append(utils.check_append_context_to_link(self.source))
        bldr.append("\n\n")

        if self.requested_date is None:
            bldr.append(
                "This reminder was created before I started saving the creation date of reminders."
            )
        else:
            bldr.append("You requested this reminder on: ")
            bldr.append(utils.render_time(self.requested_date, self.user))
        bldr.append("\n\n")

        if self.recurrence is not None:
            if self.user.recurring_sent > static.RECURRING_LIMIT:
                bldr.append("I've sent you at least ")
                bldr.append(str(static.RECURRING_LIMIT))
                bldr.append(
                    " recurring reminders since I last heard from you, so I'm automatically canceling this reminder. "
                )
                bldr.append("[Click here](")
                bldr.append(
                    utils.build_message_link(
                        static.ACCOUNT_NAME, "ReminderRepeat",
                        f"[{(self.message[:500] if self.message is not None else self.source)}]\n\n{static.TRIGGER_RECURRING}! {self.recurrence}"
                    ))
                bldr.append(") to recreate it.")
            else:
                if self.is_cakeday():
                    bldr.append("I will message you every year at ")
                    bldr.append(
                        utils.render_time(self.target_date, self.user,
                                          "%m-%d %H:%M:%S %Z"))
                    bldr.append(" to remind you of your cakeday.")

                else:
                    bldr.append(
                        "This is a repeating reminder. I'll message you again in `"
                    )
                    bldr.append(self.recurrence)
                    bldr.append("`, which is ")
                    bldr.append(
                        utils.render_time(
                            utils.parse_time(self.recurrence, self.target_date,
                                             self.user.timezone), self.user))
                    bldr.append(".")

            bldr.append("\n\n")

            bldr.append("[Click here](")
            bldr.append(
                utils.build_message_link(static.ACCOUNT_NAME, "Remove",
                                         f"Remove! {self.id}"))
            bldr.append(") to delete this reminder.")

        else:
            bldr.append("[Click here](")
            bldr.append(
                utils.build_message_link(
                    static.ACCOUNT_NAME, "Reminder",
                    f"[{(self.message[:500] if self.message is not None else self.source)}]\n\n{static.TRIGGER}! "
                ))
            bldr.append(") and set the time after the ")
            bldr.append(static.TRIGGER)
            bldr.append(
                " command to be reminded of the original comment again.")

        return bldr
示例#13
0
    def render_comment_confirmation(self,
                                    thread_id,
                                    count_duplicates=0,
                                    pushshift_minutes=0):
        bldr = utils.str_bldr()
        if pushshift_minutes > 15:
            bldr.append("There is a ")
            if pushshift_minutes > 60:
                bldr.append(str(int(round(pushshift_minutes / 60, 1))))
                bldr.append(" hour")
            else:
                bldr.append(str(pushshift_minutes))
                bldr.append(" minute")
            bldr.append(" delay fetching comments.")
            bldr.append("\n\n")

        if self.defaulted:
            bldr.append("**Defaulted to one day.**\n\n")

        if self.user.timezone is not None:
            bldr.append("Your [default time zone](")
            bldr.append(static.INFO_POST_SETTINGS)
            bldr.append(") is set to `")
            bldr.append(self.user.timezone)
            bldr.append("`. ")

        if self.is_cakeday():
            bldr.append("I will [message you every year](")
            bldr.append(static.INFO_POST_CAKEDAY)
            bldr.append(") at ")
            bldr.append(
                utils.render_time(self.target_date, self.user,
                                  "%m-%d %H:%M:%S %Z"))
            bldr.append(" to remind you of your cakeday.")

        else:
            if self.defaulted or self.target_date < utils.datetime_now():
                bldr.append("I will be messaging you on ")
            else:
                bldr.append("I will be messaging you in ")
                bldr.append(
                    utils.render_time_diff(self.requested_date,
                                           self.target_date))
                bldr.append(" on ")
            bldr.append(utils.render_time(self.target_date, self.user))
            if self.recurrence is not None:
                bldr.append(" [and then every](")
                bldr.append(static.INFO_POST_REPEAT)
                bldr.append(") `")
                bldr.append(self.recurrence)
                bldr.append("`")
            bldr.append(" to remind you of [**this link**](")
            bldr.append(utils.check_append_context_to_link(self.source))
            bldr.append(")")

        bldr.append("\n\n")

        bldr.append("[**")
        if count_duplicates > 0:
            bldr.append(str(count_duplicates))
            bldr.append(" OTHERS CLICKED")
        else:
            bldr.append("CLICK")
        bldr.append(" THIS LINK**](")
        bldr.append(
            utils.build_message_link(
                static.ACCOUNT_NAME, "Reminder",
                f"[{self.source}]\n\n{static.TRIGGER}! "
                f"{utils.get_datetime_string(self.target_date, format_string='%Y-%m-%d %H:%M:%S %Z')}"
            ))
        bldr.append(") to send a PM to also be reminded and to reduce spam.")

        if thread_id is not None:
            bldr.append("\n\n")
            bldr.append(
                "^(Parent commenter can ) [^(delete this message to hide from others.)]("
            )
            bldr.append(
                utils.build_message_link(static.ACCOUNT_NAME, "Delete Comment",
                                         f"Delete! {thread_id}"))
            bldr.append(")")

        return bldr
示例#14
0
    def render_message_confirmation(self,
                                    result_message,
                                    comment_return=None,
                                    pushshift_minutes=0):
        bldr = utils.str_bldr()
        if pushshift_minutes > 15 and comment_return is not None:
            bldr.append("There is a ")
            if pushshift_minutes > 60:
                bldr.append(str(int(round(pushshift_minutes / 60, 1))))
                bldr.append(" hour")
            else:
                bldr.append(str(int(pushshift_minutes)))
                bldr.append(" minute")
            bldr.append(" delay fetching comments.")
            bldr.append("\n\n")

        if result_message is not None:
            bldr.append(result_message)
            bldr.append("\n\n")

        if self.is_cakeday():
            bldr.append("I will message you every year at ")
            bldr.append(
                utils.render_time(self.target_date, self.user,
                                  "%m-%d %H:%M:%S %Z"))
            bldr.append(" to remind you of your cakeday.")

        else:
            if self.target_date < utils.datetime_now():
                bldr.append("I will be messaging you on ")
            else:
                bldr.append("I will be messaging you in ")
                bldr.append(
                    utils.render_time_diff(utils.datetime_now(),
                                           self.target_date))
                bldr.append(" on ")
            bldr.append(utils.render_time(self.target_date, self.user))
            if self.recurrence is not None:
                bldr.append(" and then every `")
                bldr.append(self.recurrence)
                bldr.append("`")
            bldr.append(" to remind you")
            if self.message is None:
                bldr.append(" of [**this link**](")
                bldr.append(utils.check_append_context_to_link(self.source))
                bldr.append(")")
            else:
                bldr.append(": ")
                bldr.append(self.message)

        if comment_return is not None and comment_return in (
                ReturnType.FORBIDDEN, ReturnType.THREAD_LOCKED,
                ReturnType.DELETED_COMMENT, ReturnType.RATELIMIT,
                ReturnType.THREAD_REPLIED):
            bldr.append("\n\n")
            bldr.append(
                "I'm sending this to you as a message instead of replying to your comment because "
            )
            if comment_return == ReturnType.FORBIDDEN:
                bldr.append("I'm not allowed to reply in this subreddit.")
            elif comment_return == ReturnType.THREAD_LOCKED:
                bldr.append("the thread is locked.")
            elif comment_return == ReturnType.DELETED_COMMENT:
                bldr.append("it was deleted before I could get to it.")
            elif comment_return == ReturnType.RATELIMIT:
                bldr.append(
                    "I'm new to this subreddit and have already replied to another thread here recently."
                )
            elif comment_return == ReturnType.THREAD_REPLIED:
                bldr.append(
                    "I've already replied to another comment in this thread.")

        return bldr
示例#15
0
def get_reminders_string(user, database, previous=False):
    bldr = utils.str_bldr()

    reminders = database.get_user_reminders(user)
    cakeday = database.get_cakeday(user)
    if len(reminders) or cakeday is not None:
        if previous:
            bldr.append("Your previous reminders:")
        else:
            bldr.append("Your current reminders:")
        bldr.append("\n\n")

        if len(reminders) > 1:
            bldr.append("[Click here to delete all your reminders](")
            bldr.append(
                utils.build_message_link(static.ACCOUNT_NAME, "Remove All",
                                         "RemoveAll!"))
            bldr.append(")\n\n")

        user_settings = database.get_settings(user)
        if user_settings.timezone is not None:
            bldr.append("Your timezone is currently set to: `")
            bldr.append(user_settings.timezone)
            bldr.append("`\n\n")

        log.debug(
            f"Building list with {len(reminders)} reminders and {(0 if cakeday is None else 1)} cakeday"
        )
        bldr.append("|Source|Message|Date|In|Remove|\n")
        bldr.append("|-|-|-|-|:-:|\n")
        if cakeday is not None:
            bldr.append("||")
            bldr.append("Happy cakeday!")
            bldr.append("|")
            bldr.append("Yearly on ")
            bldr.append(
                utils.render_time(cakeday.date_time, user_settings.timezone,
                                  "%m-%d %H:%M:%S %Z"))
            bldr.append("|")
            bldr.append(
                utils.render_time_diff(utils.datetime_now(),
                                       cakeday.date_time))
            bldr.append("|")
            bldr.append("[Remove](")
            bldr.append(
                utils.build_message_link(static.ACCOUNT_NAME,
                                         "Remove Cakeday Reminder",
                                         "Remove! cakeday"))
            bldr.append(")")
            bldr.append("|\n")

        for reminder in reminders:
            bldr.append("|")
            if "reddit.com" in reminder.source:
                bldr.append("[Source](")
                bldr.append(reminder.source)
                bldr.append(")")
            else:
                bldr.append(reminder.source)
            bldr.append("|")
            if reminder.message is not None:
                bldr.append(reminder.message.replace("|", "&#124;"))
            bldr.append("|")
            bldr.append(
                utils.render_time(reminder.target_date, reminder.timezone))
            bldr.append("|")
            bldr.append(
                utils.render_time_diff(utils.datetime_now(),
                                       reminder.target_date))
            bldr.append("|")
            bldr.append("[Remove](")
            bldr.append(
                utils.build_message_link(static.ACCOUNT_NAME, "Remove",
                                         f"Remove! {reminder.db_id}"))
            bldr.append(")")
            bldr.append("|\n")

            if utils.bldr_length(bldr) > 9000:
                log.debug("Message length too long, returning early")
                bldr.append("\nToo many reminders to display.")
                break
    else:
        bldr.append("You don't have any reminders.")

    return bldr