diff options
| author | 2020-04-02 14:39:24 +0200 | |
|---|---|---|
| committer | 2020-04-02 14:39:24 +0200 | |
| commit | d77a2bbc50305d05371197f4cfe3354cfca4c627 (patch) | |
| tree | be1eed54972d9843f66114311f93b68b579046ac /bot/exts/easter/egg_facts.py | |
| parent | Merge pull request #382 from ks129/game-fuzzy (diff) | |
| parent | Merge master: adjust `Space` cog location (diff) | |
Merge pull request #329 from python-discord/seasonal-purge
Deseasonify: Make all cogs available year-round, and manage only branding by season.
Diffstat (limited to 'bot/exts/easter/egg_facts.py')
| -rw-r--r-- | bot/exts/easter/egg_facts.py | 60 | 
1 files changed, 60 insertions, 0 deletions
| diff --git a/bot/exts/easter/egg_facts.py b/bot/exts/easter/egg_facts.py new file mode 100644 index 00000000..83918fb0 --- /dev/null +++ b/bot/exts/easter/egg_facts.py @@ -0,0 +1,60 @@ +import logging +import random +from json import load +from pathlib import Path + +import discord +from discord.ext import commands + +from bot.constants import Channels, Colours, Month +from bot.utils.decorators import seasonal_task + +log = logging.getLogger(__name__) + + +class EasterFacts(commands.Cog): +    """ +    A cog contains a command that will return an easter egg fact when called. + +    It also contains a background task which sends an easter egg fact in the event channel everyday. +    """ + +    def __init__(self, bot: commands.Bot): +        self.bot = bot +        self.facts = self.load_json() + +        self.daily_fact_task = self.bot.loop.create_task(self.send_egg_fact_daily()) + +    @staticmethod +    def load_json() -> dict: +        """Load a list of easter egg facts from the resource JSON file.""" +        p = Path("bot/resources/easter/easter_egg_facts.json") +        with p.open(encoding="utf8") as f: +            return load(f) + +    @seasonal_task(Month.APRIL) +    async def send_egg_fact_daily(self) -> None: +        """A background task that sends an easter egg fact in the event channel everyday.""" +        await self.bot.wait_until_ready() + +        channel = self.bot.get_channel(Channels.seasonalbot_commands) +        await channel.send(embed=self.make_embed()) + +    @commands.command(name='eggfact', aliases=['fact']) +    async def easter_facts(self, ctx: commands.Context) -> None: +        """Get easter egg facts.""" +        embed = self.make_embed() +        await ctx.send(embed=embed) + +    def make_embed(self) -> discord.Embed: +        """Makes a nice embed for the message to be sent.""" +        return discord.Embed( +            colour=Colours.soft_red, +            title="Easter Egg Fact", +            description=random.choice(self.facts) +        ) + + +def setup(bot: commands.Bot) -> None: +    """Easter Egg facts cog load.""" +    bot.add_cog(EasterFacts(bot)) | 
