Ejemplo n.º 1
0
    def info(self, context):
        """
        Return basic info for the list of reminders
        @return:
        """
        now = int(time.time())
        left = self.time - now

        if self.channel:
            channel = context.guild.get_channel(int(self.channel)).mention
        else:
            channel = '???'

        message = '`' + self.message + '`' + ' (' + channel + ')\t\t'
        if left > 0:
            message += lib.secs_to_days(left)
        else:
            message += lib.get_string('remind:anytimenow', self.guild)

        # Is there a repeating interval?
        if self.intervaltime is not None:
            message += '\t\t**(' + lib.get_string(
                'remind:interval', self.guild).format(
                    lib.secs_to_days(self.intervaltime)) + ')**'

        return message
Ejemplo n.º 2
0
    async def run_time(self, context):
        """
        Check how much time is left in the event
        :param context:
        :return:
        """
        user = User(context.message.author.id, context.guild.id, context)
        event = Event.get_by_guild(user.get_guild())
        now = int(time.time())

        # Make sure the event exists
        if event is None or not event.is_valid():
            return await context.send(user.get_mention() + ', ' + lib.get_string('event:err:noexists', user.get_guild()))

        # Is the event scheduled to start, but has not yet started?
        if not event.is_running() and event.is_scheduled():
            left = lib.secs_to_days(event.get_start_time() - now)
            return await context.send(user.get_mention() + ', ' + lib.get_string('event:timetostart', user.get_guild()).format(left))

        # If the event is not running and it is NOT scheduled, then we don't know what time it will start.
        elif not event.is_running():
            return await context.send(user.get_mention() + ', ' + lib.get_string('event:err:noexists', user.get_guild()))

        # At this point, the event must be running. If it is scheduled, then we can get the time left from the end time. Otherwise, we don't know.
        elif event.is_scheduled():
            left = lib.secs_to_days(event.get_end_time() - now)
            return await context.send(user.get_mention() + ', ' + lib.get_string('event:timeleft', user.get_guild()).format(left))

        else:
            return await context.send(user.get_mention() + ', ' + lib.get_string('event:noendtime', user.get_guild()))
Ejemplo n.º 3
0
    async def run_check_all(self, context):
        """
        Print a table of all the user's goals.
        @param context:
        @return:
        """

        now = int(time.time())
        user = User(context.message.author.id, context.guild.id, context)
        embed = discord.Embed(title=lib.get_string('goals', user.get_guild()),
                              color=10038562)

        for type in self.types:

            type_string = lib.get_string('goal:' + type, user.get_guild())
            goal = user.get_goal(type)
            if goal is not None:
                progress = user.get_goal_progress(type)
                left = lib.secs_to_days(goal['reset'] - now)
                text = lib.get_string('goal:yourgoal',
                                      user.get_guild()).format(
                                          type_string, goal['goal']) + "\n"
                text += lib.get_string('goal:status', user.get_guild()).format(
                    progress['percent'], type_string, progress['current'],
                    progress['goal']) + "\n"
                text += lib.get_string('goal:timeleft',
                                       user.get_guild()).format(
                                           left, type_string)
            else:
                text = None

            embed.add_field(name=type_string, value=text, inline=False)

        # Send the message
        await context.send(embed=embed)
Ejemplo n.º 4
0
    async def run_time(self, context):
        """
        Check how long until the goal resets
        :param context:
        :return:
        """
        user = User(context.message.author.id, context.guild.id, context)

        # Currently only daily goals implemented
        type = 'daily'

        # Get the goal of this type for this user
        goal = user.get_goal(type)
        if goal:

            now = int(time.time())
            reset = goal['reset']
            left = lib.secs_to_days(reset - now)
            return await context.send(
                user.get_mention() + ', ' + lib.get_string(
                    'goal:timeleft', user.get_guild()).format(left, type))

        else:
            return await context.send(
                user.get_mention() + ', ' +
                lib.get_string('goal:nogoal', user.get_guild()).format(type))
Ejemplo n.º 5
0
    async def run_remind(self, context, user, cmd):
        """
        Set or configure a reminder
        @param context:
        @param user:
        @param cmd:
        @return:
        """
        now = int(time.time())

        remind_time = None
        message = None
        channel = None
        repeat = None

        # Check the first format: in x send y to #z. E.g. in 15 send Hi there everyone to #channel. Or: in 25 send Hey there
        regex = {
            'in': '^in\s(\d+)\ssend\s(.*?)(\sto\s\<\#([0-9]+)\>)?$',
            'at': '^at\s(\d{4}|\d{2}\:\d{2})(\son\s(.*?))?\ssend\s(.*?)(\sto\s\<\#([0-9]+)\>)?$',
            'every': '^every\s(day|hour|week)\s(from|at)\s(\d{4}|\d{2}\:\d{2})\ssend\s(.*?)(\sto\s\<\#([0-9]+)\>)?$'
        }
        if re.search(regex['in'], cmd, re.IGNORECASE):

            matches = re.findall(regex['in'], cmd, re.IGNORECASE)

            # Make sure the time in mins is valid.
            if int(matches[0][0]) <= 0:
                return await context.send(lib.get_string('remind:err:time', user.get_guild()))

            remind_time = now + (60 * int(matches[0][0]))

            message = matches[0][1]
            if lib.is_number(matches[0][3]):
                channel = int(matches[0][3])
            else:
                channel = context.message.channel.id

        # Next format to check: at hh:mm send y to #z. E.g. at 17:00 send Hello there to #channel.
        elif re.search(regex['at'], cmd, re.IGNORECASE):

            matches = re.findall(regex['at'], cmd, re.IGNORECASE)

            requested_time = matches[0][0]
            requested_date = matches[0][2] if matches[0][2] != '' else None

            # If they passed the time through with a colon, remove that.
            if ':' in requested_time:
                requested_time = requested_time.replace(':', '')

            # Now convert the time to an int.
            requested_time = int(requested_time)

            timezone = pytz.timezone(user.get_setting('timezone'))
            timezone_date = datetime.now(timezone).strftime('%d-%m-%Y') if requested_date is None else requested_date
            timezone_time = int(datetime.now(timezone).strftime('%H%M'))

            # Build the datetime object for the current date (in user's timezone) and the requested time.
            try:
                reminder_time = datetime.strptime(timezone_date + ' ' + str(requested_time), '%d-%m-%Y %H%M')
            except ValueError:
                return await context.send(lib.get_string('remind:err:date', user.get_guild()))

            # If they manually specified a date and it is in the past, send an error.
            if requested_date is not None and int(timezone.localize(reminder_time).timestamp()) <= now:
                return await context.send(lib.get_string('remind:err:date', user.get_guild()))

            # If the time they requested has already passed (but they did not specify a date), alter the date ahead by 1 day.
            if requested_time <= timezone_time:
                reminder_time += timedelta(days=1)

            # Convert it to a UTC timestamp.
            remind_time = int(timezone.localize(reminder_time).timestamp())

            message = matches[0][3]
            if lib.is_number(matches[0][5]):
                channel = int(matches[0][5])
            else:
                channel = context.message.channel.id

        elif re.search(regex['every'], cmd, re.IGNORECASE):

            matches = re.findall(regex['every'], cmd, re.IGNORECASE)

            interval = matches[0][0]
            requested_time = matches[0][2]
            message = matches[0][3]
            if lib.is_number(matches[0][5]):
                channel = int(matches[0][5])
            else:
                channel = context.message.channel.id

            # If they passed the time through with a colon, remove that.
            if ':' in requested_time:
                requested_time = requested_time.replace(':', '')

            # Check interval is valid.
            if interval not in list(self._reminder_intervals.keys()):
                return await context.send(lib.get_string('remind:err:interval', user.get_guild()))

            # Now convert the time to an int.
            requested_time = int(requested_time)

            timezone = pytz.timezone(user.get_setting('timezone'))
            timezone_date = datetime.now(timezone).strftime('%d-%m-%Y')
            timezone_time = int(datetime.now(timezone).strftime('%H%M'))

            # Build the datetime object for the current date (in user's timezone) and the requested time.
            try:
                reminder_time = datetime.strptime(timezone_date + ' ' + str(requested_time), '%d-%m-%Y %H%M')
            except ValueError:
                return await context.send(lib.get_string('remind:err:date', user.get_guild()))

            # If the time they requested has already passed (but they did not specify a date), alter the date ahead by 1 day.
            if requested_time <= timezone_time:
                reminder_time += timedelta(days=1)

            # Convert it to a UTC timestamp.
            remind_time = int(timezone.localize(reminder_time).timestamp())

            # Now get the interval time to add each time the reminder is set.
            repeat = self._reminder_intervals[interval]

        else:
            return await context.send(user.get_mention() + ', ' + lib.get_string('remind:err:format', user.get_guild()))

        # Check the channel is valid.
        if not context.guild.get_channel(channel):
            return await context.send(lib.get_string('remind:err:channel', user.get_guild()))

        # Check that the message is not too long?
        if len(message) > 255:
            return await context.send(lib.get_string('remind:err:message', user.get_guild()).format(len(message)))

        # If we get this far, we have parsed the command into variables.
        params = {
            'user': user.get_id(),
            'guild': user.get_guild(),
            'time': remind_time,
            'channel': channel,
            'message': message,
            'intervaltime': repeat
        }

        reminder = Reminder.create(params)
        if reminder:
            return await context.send(user.get_mention() + ', ' + lib.get_string('remind:created', user.get_guild()).format(
                lib.secs_to_days(remind_time - now))
            )