aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-23 19:31:23 +0100
committerGravatar kwzrd <[email protected]>2020-03-23 19:31:23 +0100
commit79376abcb7d305484bf5283f95d3a32641aeb6d5 (patch)
tree659d6fa3f5b5655531ea8909f07571bec1c0c92d
parentDeseasonify: remove `load_extensions` method (diff)
Deseasonify: abdicate responsibility to wait until bot is ready
The wrapper will no longer wait for the bot to be ready before it calls the wrapped function for the first time. If the function requires the bot's cache to be ready, it is responsible for awaiting the method itself. This removes the need to acquire a reference to the bot instance inside the decorator, which seems to be difficult to do cleanly. As Mark adds, this may in fact be safer as the bot may temporarily disconnect while the task is active, and awaiting the bot's ready status every time will prevent issues in such a situation. Co-authored-by: MarkKoz <[email protected]>
-rw-r--r--bot/decorators.py10
-rw-r--r--bot/seasons/easter/egg_facts.py2
-rw-r--r--bot/seasons/pride/pride_facts.py2
3 files changed, 7 insertions, 7 deletions
diff --git a/bot/decorators.py b/bot/decorators.py
index 74976cd6..400f1bbb 100644
--- a/bot/decorators.py
+++ b/bot/decorators.py
@@ -12,7 +12,6 @@ from discord import Colour, Embed
from discord.ext import commands
from discord.ext.commands import CheckFailure, Context
-from bot.bot import bot
from bot.constants import ERROR_REPLIES, Month
ONE_DAY = 24 * 60 * 60
@@ -41,16 +40,13 @@ def seasonal_task(*allowed_months: Month, sleep_time: typing.Union[float, int] =
The decorated function will be called once every `sleep_time` seconds while
the current UTC month is in `allowed_months`. Sleep time defaults to 24 hours.
+
+ The wrapped task is responsible for waiting for the bot to be ready, if necessary.
"""
def decorator(task_body: typing.Callable) -> typing.Callable:
@functools.wraps(task_body)
async def decorated_task(*args, **kwargs) -> None:
- """
- Call `task_body` once every `sleep_time` seconds in `allowed_months`.
-
- Wait for bot to be ready before calling `task_body` for the first time.
- """
- await bot.wait_until_ready()
+ """Call `task_body` once every `sleep_time` seconds in `allowed_months`."""
log.info(f"Starting seasonal task {task_body.__qualname__} ({allowed_months})")
while True:
diff --git a/bot/seasons/easter/egg_facts.py b/bot/seasons/easter/egg_facts.py
index f61f9da4..d20df3de 100644
--- a/bot/seasons/easter/egg_facts.py
+++ b/bot/seasons/easter/egg_facts.py
@@ -35,6 +35,8 @@ class EasterFacts(commands.Cog):
@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())
diff --git a/bot/seasons/pride/pride_facts.py b/bot/seasons/pride/pride_facts.py
index 417a49a6..1a02eaaa 100644
--- a/bot/seasons/pride/pride_facts.py
+++ b/bot/seasons/pride/pride_facts.py
@@ -35,6 +35,8 @@ class PrideFacts(commands.Cog):
@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)
await self.send_select_fact(channel, datetime.utcnow())