diff options
Diffstat (limited to 'bot/cogs')
-rw-r--r-- | bot/cogs/halloween_facts.py | 75 | ||||
-rw-r--r-- | bot/cogs/spookyreact.py | 58 |
2 files changed, 123 insertions, 10 deletions
diff --git a/bot/cogs/halloween_facts.py b/bot/cogs/halloween_facts.py new file mode 100644 index 00000000..e97c80d2 --- /dev/null +++ b/bot/cogs/halloween_facts.py @@ -0,0 +1,75 @@ +import asyncio +import json +import random +from datetime import timedelta +from pathlib import Path + +import discord +from discord.ext import commands + +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) +HACKTOBERBOT_CHANNEL_ID = 498804484324196362 +INTERVAL = timedelta(hours=6).total_seconds() + + +class HalloweenFacts: + + def __init__(self, bot): + self.bot = bot + with open(Path("./bot/resources", "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(HACKTOBERBOT_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)) diff --git a/bot/cogs/spookyreact.py b/bot/cogs/spookyreact.py index 2652a60e..9146b797 100644 --- a/bot/cogs/spookyreact.py +++ b/bot/cogs/spookyreact.py @@ -1,11 +1,16 @@ +import logging +import re + +import discord + SPOOKY_TRIGGERS = { - 'spooky': "\U0001F47B", - 'skeleton': "\U0001F480", - 'doot': "\U0001F480", - 'pumpkin': "\U0001F383", - 'halloween': "\U0001F383", - 'jack-o-lantern': "\U0001F383", - 'danger': "\U00002620" + '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") } @@ -18,13 +23,46 @@ class SpookyReact: def __init__(self, bot): self.bot = bot - async def on_message(self, ctx): + async def on_message(self, ctx: discord.Message): """ A command to send the hacktoberbot github project + + Lines that begin with the bot's command prefix are ignored + + Hacktoberbot's own messages are ignored """ for trigger in SPOOKY_TRIGGERS.keys(): - if trigger in ctx.content.lower(): - await ctx.add_reaction(SPOOKY_TRIGGERS[trigger]) + trigger_test = re.search(SPOOKY_TRIGGERS[trigger][0], ctx.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(ctx): + return + else: + await ctx.add_reaction(SPOOKY_TRIGGERS[trigger][1]) + logging.info(f"Added '{trigger}' reaction to message ID: {ctx.id}") + + async def _short_circuit_check(self, ctx: discord.Message) -> bool: + """ + Short-circuit helper check. + + Return True if: + * author is the bot + * prefix is not None + """ + # Check for self reaction + if ctx.author == self.bot.user: + logging.info(f"Ignoring reactions on self message. Message ID: {ctx.id}") + return True + + # Check for command invocation + # Because on_message doesn't give a full Context object, generate one first + tmp_ctx = await self.bot.get_context(ctx) + if tmp_ctx.prefix: + logging.info(f"Ignoring reactions on command invocation. Message ID: {ctx.id}") + return True + + return False def setup(bot): |