aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/halloween/timeleft.py
blob: 295acc8985a42356d7f42e02e0124120be989f03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import logging
from datetime import datetime
from typing import Tuple

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: commands.Bot):
        self.bot = bot

    @staticmethod
    def in_october() -> bool:
        """Return True if the current month is October."""
        return datetime.utcnow().month == 10

    @staticmethod
    def load_date() -> Tuple[int, datetime, datetime]:
        """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, 11, 1, 11, 59, 59)
        start = datetime(year, 10, 1)
        return now, end, start

    @commands.command()
    async def timeleft(self, ctx: commands.Context) -> None:
        """
        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: commands.Bot) -> None:
    """Cog load."""
    bot.add_cog(TimeLeft(bot))