From 8b831fb6aa59459024ecac6a65639dce4df48eff Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 9 Sep 2023 11:38:03 -0400 Subject: [PATCH] start loop in init, use before loop, waiting on bot ready, to set up needed data. --- cogs/loops/daily_loop.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py index 61cde16..c92ca77 100644 --- a/cogs/loops/daily_loop.py +++ b/cogs/loops/daily_loop.py @@ -21,18 +21,8 @@ class DailyLoop(commands.Cog, name="Daily Loop"): self.bot = bot self.current_election = None - @commands.Cog.listener() - async def on_ready(self): - """ - Starts the loop when bot is ready. - """ - await self.bot.wait_until_ready() - - # wait until the discord bot is all ready. Bot needs to know guilds. - self.current_election = self.get_current_cycle() - - # pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop. - await self.daily_loop.start() # pylint: disable=no-member + # pylint doesn't recognize that daily_loop is subclassed from tasks.loop. + self.daily_loop.start() # pylint: disable=no-member @tasks.loop(time=time(hour=0, tzinfo=timezone("GMT"))) async def daily_loop(self): @@ -50,6 +40,17 @@ class DailyLoop(commands.Cog, name="Daily Loop"): if self.current_election.election_over: self.current_election = None + @daily_loop.before_loop + async def before_loop(self): + """ + Actions run before the loop starts. Want to wait for bot to be ready and get the current cycle. + """ + + await self.bot.wait_until_ready() + + # Bot needs to know guilds to get the current cycle. + self.current_election = self.get_current_cycle() + def get_current_cycle(self) -> ElectionCycle: """ Creates an object for the current election cycle if one is active. An election is considered active