From d2577597365b408b853b671e452559257777f86f Mon Sep 17 00:00:00 2001 From: "Nyeki.py" <84998222+Nekidev@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:31:31 -0300 Subject: [PATCH] refactor(bot): :recycle: Move heartbeat update task to Utility cog. --- payphone/bot.py | 8 -------- payphone/cogs/utility/cog.py | 13 ++++++++++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/payphone/bot.py b/payphone/bot.py index cedb7a9..30ecc9b 100644 --- a/payphone/bot.py +++ b/payphone/bot.py @@ -80,8 +80,6 @@ class Payphone(commands.AutoShardedBot): await self.redis.ping() - self.update_heartbeat.start() - print(f"Ready! Logged in as {self.user.display_name}.") async def on_shard_ready(self, shard_id: int) -> None: @@ -452,12 +450,6 @@ class Payphone(commands.AutoShardedBot): name=self.user.display_name, icon_url=self.user.display_avatar.url ) - @tasks.loop(seconds=1) - async def update_heartbeat(self) -> None: - """Updates the heartbeat timestamp.""" - - self.heartbeat.value = int(datetime.now(timezone.utc).timestamp()) - bot = Payphone() diff --git a/payphone/cogs/utility/cog.py b/payphone/cogs/utility/cog.py index 09cbe13..fa062b3 100644 --- a/payphone/cogs/utility/cog.py +++ b/payphone/cogs/utility/cog.py @@ -1,7 +1,7 @@ from datetime import datetime, timezone import discord -from discord.ext import commands +from discord.ext import commands, tasks from payphone import bot from payphone.cogs import base @@ -12,6 +12,17 @@ class Utility(base.Cog): def __init__(self, bot_instance: bot.Payphone): self.bot = bot_instance + self.update_heartbeat.start() + + async def cog_unload(self) -> None: + self.update_heartbeat.cancel() + + @tasks.loop(seconds=5) + async def update_heartbeat(self) -> None: + """Updates the heartbeat timestamp.""" + + self.bot.heartbeat.value = int(datetime.now(timezone.utc).timestamp()) + @commands.command() @commands.cooldown(1, 60, commands.BucketType.user) @checks.is_unbanned()