diff options
author | 2020-03-15 18:34:48 +0100 | |
---|---|---|
committer | 2020-03-15 18:34:48 +0100 | |
commit | edf419cffc04fe1f462a2595c37fc047f9f9687c (patch) | |
tree | e405cd6463a7a58bc0eab36975f4783cee65571f | |
parent | Deseasonify: add convenience decorator for seasonal tasks (diff) |
Deseasonify: apply seasonal task mechanism to tasks as appropriate
-rw-r--r-- | bot/seasons/easter/egg_facts.py | 14 | ||||
-rw-r--r-- | bot/seasons/pride/pride_facts.py | 13 |
2 files changed, 6 insertions, 21 deletions
diff --git a/bot/seasons/easter/egg_facts.py b/bot/seasons/easter/egg_facts.py index cfa420f6..f61f9da4 100644 --- a/bot/seasons/easter/egg_facts.py +++ b/bot/seasons/easter/egg_facts.py @@ -1,7 +1,5 @@ -import asyncio import logging import random -from datetime import datetime from json import load from pathlib import Path @@ -9,6 +7,7 @@ import discord from discord.ext import commands from bot.constants import Channels, Colours, Month +from bot.decorators import seasonal_task log = logging.getLogger(__name__) @@ -24,7 +23,6 @@ class EasterFacts(commands.Cog): self.bot = bot self.facts = self.load_json() - self.active_months = {Month.april} self.daily_fact_task = self.bot.loop.create_task(self.send_egg_fact_daily()) @staticmethod @@ -34,17 +32,11 @@ class EasterFacts(commands.Cog): 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) - - while True: - current_month = Month(datetime.utcnow().month) - if current_month in self.active_months: - await channel.send(embed=self.make_embed()) - - await asyncio.sleep(24 * 60 * 60) + await channel.send(embed=self.make_embed()) @commands.command(name='eggfact', aliases=['fact']) async def easter_facts(self, ctx: commands.Context) -> None: diff --git a/bot/seasons/pride/pride_facts.py b/bot/seasons/pride/pride_facts.py index 2df9c8cd..417a49a6 100644 --- a/bot/seasons/pride/pride_facts.py +++ b/bot/seasons/pride/pride_facts.py @@ -1,4 +1,3 @@ -import asyncio import json import logging import random @@ -11,6 +10,7 @@ import discord from discord.ext import commands from bot.constants import Channels, Colours, Month +from bot.decorators import seasonal_task log = logging.getLogger(__name__) @@ -24,7 +24,6 @@ class PrideFacts(commands.Cog): self.bot = bot self.facts = self.load_facts() - self.active_months = {Month.june} self.daily_fact_task = self.bot.loop.create_task(self.send_pride_fact_daily()) @staticmethod @@ -33,17 +32,11 @@ class PrideFacts(commands.Cog): with open(Path("bot/resources/pride/facts.json"), "r", encoding="utf-8") as f: return json.load(f) + @seasonal_task(Month.june) async def send_pride_fact_daily(self) -> None: """Background task to post the daily pride fact every day.""" - await self.bot.wait_until_ready() channel = self.bot.get_channel(Channels.seasonalbot_commands) - - while True: - current_month = Month(datetime.utcnow().month) - if current_month in self.active_months: - await self.send_select_fact(channel, datetime.utcnow()) - - await asyncio.sleep(24 * 60 * 60) + await self.send_select_fact(channel, datetime.utcnow()) async def send_random_fact(self, ctx: commands.Context) -> None: """Provides a fact from any previous day, or today.""" |