aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/holidays/halloween/spookyrating.py
blob: 14530e6b384dda6144eb69f957c961f2bacc2d0d (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
import bisect
import json
import random
from pathlib import Path

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

from bot.bot import Bot
from bot.constants import Colours

log = get_logger(__name__)

data: dict[str, dict[str, str]] = json.loads(
    Path("bot/resources/holidays/halloween/spooky_rating.json").read_text("utf8")
)
SPOOKY_DATA = sorted((int(key), value) for key, value in data.items())


class SpookyRating(commands.Cog):
    """A cog for calculating one's spooky rating."""

    def __init__(self):
        self.local_random = random.Random()

    @commands.command()
    @commands.cooldown(rate=1, per=5, type=commands.BucketType.user)
    async def spookyrating(self, ctx: commands.Context, who: discord.Member = None) -> None:
        """
        Calculates the spooky rating of someone.

        Any user will always yield the same result, no matter who calls the command
        """
        if who is None:
            who = ctx.author

        # This ensures that the same result over multiple runtimes
        self.local_random.seed(who.id)
        spooky_percent = self.local_random.randint(1, 101)

        # We need the -1 due to how bisect returns the point
        # see the documentation for further detail
        # https://docs.python.org/3/library/bisect.html#bisect.bisect
        index = bisect.bisect(SPOOKY_DATA, (spooky_percent,)) - 1

        _, data = SPOOKY_DATA[index]

        embed = discord.Embed(
            title=data["title"],
            description=f"{who} scored {spooky_percent}%!",
            color=Colours.orange
        )
        embed.add_field(
            name="A whisper from Satan",
            value=data["text"]
        )
        embed.set_thumbnail(
            url=data["image"]
        )

        await ctx.send(embed=embed)


async def setup(bot: Bot) -> None:
    """Load the Spooky Rating Cog."""
    await bot.add_cog(SpookyRating())