aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-07 14:46:13 +0100
committerGravatar kwzrd <[email protected]>2020-03-07 14:46:13 +0100
commit62c59c2c82d16d3e77ddc7bf3785e739d94de660 (patch)
treeb627a31561b59877f7ed832d0712cd50c9b735e2
parentDeseasonify: remove reference to non-existent task, redundant attr (diff)
Deseasonify: create daily tasks inside cog init; wait until ready
Previously, the cog was being instantiated once: once to launch the task, and once to be registered under `bot`. Now, the cog is only created once, and the task is built inside the cog's init. Since cogs are now loaded before the bot connects, the tasks need to wait before the bot's cache is ready, to ensure that the channel is found.
-rw-r--r--bot/seasons/easter/egg_facts.py4
-rw-r--r--bot/seasons/pride/pride_facts.py4
2 files changed, 6 insertions, 2 deletions
diff --git a/bot/seasons/easter/egg_facts.py b/bot/seasons/easter/egg_facts.py
index e66e25a3..260a505a 100644
--- a/bot/seasons/easter/egg_facts.py
+++ b/bot/seasons/easter/egg_facts.py
@@ -24,6 +24,7 @@ class EasterFacts(commands.Cog):
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:
@@ -34,7 +35,9 @@ class EasterFacts(commands.Cog):
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:
embed = self.make_embed()
await channel.send(embed=embed)
@@ -57,6 +60,5 @@ class EasterFacts(commands.Cog):
def setup(bot: commands.Bot) -> None:
"""Easter Egg facts cog load."""
- bot.loop.create_task(EasterFacts(bot).send_egg_fact_daily())
bot.add_cog(EasterFacts(bot))
log.info("EasterFacts cog loaded")
diff --git a/bot/seasons/pride/pride_facts.py b/bot/seasons/pride/pride_facts.py
index 5c19dfd0..fe5e3cf9 100644
--- a/bot/seasons/pride/pride_facts.py
+++ b/bot/seasons/pride/pride_facts.py
@@ -24,6 +24,7 @@ class PrideFacts(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.facts = self.load_facts()
+ self.daily_fact_task = self.bot.loop.create_task(self.send_pride_fact_daily())
@staticmethod
def load_facts() -> dict:
@@ -33,7 +34,9 @@ class PrideFacts(commands.Cog):
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:
await self.send_select_fact(channel, datetime.utcnow())
await asyncio.sleep(24 * 60 * 60)
@@ -101,6 +104,5 @@ class PrideFacts(commands.Cog):
def setup(bot: commands.Bot) -> None:
"""Cog loader for pride facts."""
- bot.loop.create_task(PrideFacts(bot).send_pride_fact_daily())
bot.add_cog(PrideFacts(bot))
log.info("Pride facts cog loaded!")