diff options
| author | 2021-09-04 23:31:14 -0400 | |
|---|---|---|
| committer | 2021-09-04 23:31:14 -0400 | |
| commit | 3377e46dc890626a29b05e134799b6219598107b (patch) | |
| tree | bdb4a2c6efbe113ec0e355a9eff5070cc8ec52aa /bot/exts/holidays/halloween/spookyreact.py | |
| parent | Move Easter to Holidays Folder (diff) | |
Move Halloween to Holidays folder
Moves all the hallowen features to the holidays folder.
Also updates the paths to reflect the folder moves.
Diffstat (limited to 'bot/exts/holidays/halloween/spookyreact.py')
| -rw-r--r-- | bot/exts/holidays/halloween/spookyreact.py | 70 | 
1 files changed, 70 insertions, 0 deletions
diff --git a/bot/exts/holidays/halloween/spookyreact.py b/bot/exts/holidays/halloween/spookyreact.py new file mode 100644 index 00000000..25e783f4 --- /dev/null +++ b/bot/exts/holidays/halloween/spookyreact.py @@ -0,0 +1,70 @@ +import logging +import re + +import discord +from discord.ext.commands import Cog + +from bot.bot import Bot +from bot.constants import Month +from bot.utils.decorators import in_month + +log = logging.getLogger(__name__) + +SPOOKY_TRIGGERS = { +    "spooky": (r"\bspo{2,}ky\b", "\U0001F47B"), +    "skeleton": (r"\bskeleton\b", "\U0001F480"), +    "doot": (r"\bdo{2,}t\b", "\U0001F480"), +    "pumpkin": (r"\bpumpkin\b", "\U0001F383"), +    "halloween": (r"\bhalloween\b", "\U0001F383"), +    "jack-o-lantern": (r"\bjack-o-lantern\b", "\U0001F383"), +    "danger": (r"\bdanger\b", "\U00002620") +} + + +class SpookyReact(Cog): +    """A cog that makes the bot react to message triggers.""" + +    def __init__(self, bot: Bot): +        self.bot = bot + +    @in_month(Month.OCTOBER) +    @Cog.listener() +    async def on_message(self, message: discord.Message) -> None: +        """Triggered when the bot sees a message in October.""" +        for name, trigger in SPOOKY_TRIGGERS.items(): +            trigger_test = re.search(trigger[0], message.content.lower()) +            if trigger_test: +                # Check message for bot replies and/or command invocations +                # Short circuit if they're found, logging is handled in _short_circuit_check +                if await self._short_circuit_check(message): +                    return +                else: +                    await message.add_reaction(trigger[1]) +                    log.info(f"Added {name!r} reaction to message ID: {message.id}") + +    async def _short_circuit_check(self, message: discord.Message) -> bool: +        """ +        Short-circuit helper check. + +        Return True if: +          * author is the bot +          * prefix is not None +        """ +        # Check for self reaction +        if message.author == self.bot.user: +            log.debug(f"Ignoring reactions on self message. Message ID: {message.id}") +            return True + +        # Check for command invocation +        # Because on_message doesn't give a full Context object, generate one first +        ctx = await self.bot.get_context(message) +        if ctx.prefix: +            log.debug(f"Ignoring reactions on command invocation. Message ID: {message.id}") +            return True + +        return False + + +def setup(bot: Bot) -> None: +    """Load the Spooky Reaction Cog.""" +    bot.add_cog(SpookyReact(bot))  |