aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/holidays/halloween/halloween_facts.py
blob: daad26d359b502b72a8a0ec02673b301d5dda2db (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
import json
import random
from datetime import timedelta
from pathlib import Path

import discord
from discord.ext import commands
from pydis_core.utils.logging import get_logger

from bot.bot import Bot

log = get_logger(__name__)

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 = 0xFF7518
INTERVAL = timedelta(hours=6).total_seconds()

FACTS = json.loads(Path("bot/resources/holidays/halloween/halloween_facts.json").read_text("utf8"))
FACTS = list(enumerate(FACTS))


class HalloweenFacts(commands.Cog):
    """A Cog for displaying interesting facts about Halloween."""

    def random_fact(self) -> tuple[int, str]:
        """Return a random fact from the loaded facts."""
        return random.choice(FACTS)

    @commands.command(name="spookyfact", aliases=("halloweenfact",), brief="Get the most recent Halloween fact")
    async def get_random_fact(self, ctx: commands.Context) -> None:
        """Reply with the most recent Halloween fact."""
        index, fact = self.random_fact()
        embed = self._build_embed(index, fact)
        await ctx.send(embed=embed)

    @staticmethod
    def _build_embed(index: int, fact: str) -> discord.Embed:
        """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)


async def setup(bot: Bot) -> None:
    """Load the Halloween Facts Cog."""
    await bot.add_cog(HalloweenFacts())