aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/hacktober/halloween_facts.py
blob: bd164e3075646e4ef97668734a6acd5fc59a7925 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import asyncio
import json
import random
from datetime import timedelta
from pathlib import Path

import discord
from discord.ext import commands

from bot.constants import HACKTOBER_CHANNEL_ID

SPOOKY_EMOJIS = [
    "\N{BAT}",
    "\N{DERELICT HOUSE BUILDING}",
    "\N{EXTRATERRESTRIAL ALIEN}",
    "\N{GHOST}",
    "\N{JACK-O-LANTERN}",
    "\N{SKULL}",
    "\N{SKULL AND CROSSBONES}",
    "\N{SPIDER WEB}",
]
PUMPKIN_ORANGE = discord.Color(0xFF7518)
INTERVAL = timedelta(hours=6).total_seconds()


class HalloweenFacts:

    def __init__(self, bot):
        self.bot = bot
        with open(Path("bot", "resources", "halloween", "halloween_facts.json"), "r") as file:
            self.halloween_facts = json.load(file)
        self.channel = None
        self.last_fact = None

    async def on_ready(self):
        self.channel = self.bot.get_channel(HACKTOBER_CHANNEL_ID)
        self.bot.loop.create_task(self._fact_publisher_task())

    async def _fact_publisher_task(self):
        """
        A background task that runs forever, sending Halloween facts at random to the Discord channel with id equal to
        HACKTOBERFEST_CHANNEL_ID every INTERVAL seconds.
        """
        facts = list(enumerate(self.halloween_facts))
        while True:
            # Avoid choosing each fact at random to reduce chances of facts being reposted soon.
            random.shuffle(facts)
            for index, fact in facts:
                embed = self._build_embed(index, fact)
                await self.channel.send("Your regular serving of random Halloween facts", embed=embed)
                self.last_fact = (index, fact)
                await asyncio.sleep(INTERVAL)

    @commands.command(name="hallofact", aliases=["hallofacts"], brief="Get the most recent Halloween fact")
    async def get_last_fact(self, ctx):
        """
        Reply with the most recent Halloween fact.
        """
        if ctx.channel != self.channel:
            return
        index, fact = self.last_fact
        embed = self._build_embed(index, fact)
        await ctx.send("Halloween fact recap", embed=embed)

    @staticmethod
    def _build_embed(index, fact):
        """
        Builds a Discord embed from the given fact and its index.
        """
        emoji = random.choice(SPOOKY_EMOJIS)
        title = f"{emoji} Halloween Fact #{index + 1}"
        return discord.Embed(title=title, description=fact, color=PUMPKIN_ORANGE)


def setup(bot):
    bot.add_cog(HalloweenFacts(bot))