diff options
| author | 2019-03-29 08:12:40 -0400 | |
|---|---|---|
| committer | 2019-03-29 08:12:40 -0400 | |
| commit | add3cf6ffc453c2f3f494c4600665a624d14c1c6 (patch) | |
| tree | 8f6f189f98d8a5145111406d3f1b01be5a39b640 | |
| parent | Add missing Cog inheritance to 8ball (diff) | |
| parent | Merge pull request #142 from Suhail6inkling/timeleft (diff) | |
Merge branch 'master' into hotfix
| -rw-r--r-- | bot/seasons/halloween/timeleft.py | 67 | 
1 files changed, 67 insertions, 0 deletions
| diff --git a/bot/seasons/halloween/timeleft.py b/bot/seasons/halloween/timeleft.py new file mode 100644 index 00000000..af205573 --- /dev/null +++ b/bot/seasons/halloween/timeleft.py @@ -0,0 +1,67 @@ +import logging +from datetime import datetime + +from discord.ext import commands + +log = logging.getLogger(__name__) + + +class TimeLeft: +    """ +    A Cog that tells you how long left until Hacktober is over! +    """ + +    def __init__(self, bot): +        self.bot = bot + +    @staticmethod +    def in_october(): +        """ +        Return True if the current month is October. +        """ + +        return datetime.utcnow().month == 10 + +    @staticmethod +    def load_date(): +        """ +        Return of a tuple of the current time and the end and start times of the following October. +        """ + +        now = datetime.utcnow() +        year = now.year +        if now.month > 10: +            year += 1 +        end = datetime(year, 10, 31, 11, 59, 59) +        start = datetime(year, 10, 1) +        return now, end, start + +    @commands.command() +    async def timeleft(self, ctx): +        """ +        Calculates the time left until the end of Hacktober. + +        Whilst in October, displays the days, hours and minutes left. +        Only displays the days left until the beginning and end whilst in a different month +        """ + +        now, end, start = self.load_date() +        diff = end - now +        days, seconds = diff.days, diff.seconds +        if self.in_october(): +            minutes = seconds // 60 +            hours, minutes = divmod(minutes, 60) +            await ctx.send(f"There is currently only {days} days, {hours} hours and {minutes}" +                           "minutes left until the end of Hacktober.") +        else: +            start_diff = start - now +            start_days = start_diff.days +            await ctx.send( +                f"It is not currently Hacktober. However, the next one will start in {start_days} days " +                f"and will finish in {days} days." +            ) + + +def setup(bot): +    bot.add_cog(TimeLeft(bot)) +    log.info("TimeLeft cog loaded") | 
