diff options
author | 2019-05-11 04:51:30 +1000 | |
---|---|---|
committer | 2019-05-11 04:51:30 +1000 | |
commit | 0e2c6a1b0daef5b569da7652801b5725bf1ed95b (patch) | |
tree | c3d57e50f2ba07cb2d9e7476d3b9441c1f671bd9 /bot/seasons/halloween/timeleft.py | |
parent | not importing aiohttp now (diff) | |
parent | Merge pull request #198 from Suhail6inkling/constants_fix (diff) |
Merge branch 'master' into hanukkah_embed_iceman
Diffstat (limited to 'bot/seasons/halloween/timeleft.py')
-rw-r--r-- | bot/seasons/halloween/timeleft.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/bot/seasons/halloween/timeleft.py b/bot/seasons/halloween/timeleft.py new file mode 100644 index 00000000..3ea2d9ad --- /dev/null +++ b/bot/seasons/halloween/timeleft.py @@ -0,0 +1,63 @@ +import logging +from datetime import datetime + +from discord.ext import commands + +log = logging.getLogger(__name__) + + +class TimeLeft(commands.Cog): + """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 next 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): + """Cog load.""" + + bot.add_cog(TimeLeft(bot)) + log.info("TimeLeft cog loaded") |