Skip to content

Commit

Permalink
fix timer logs spamming the console with 1 second diff
Browse files Browse the repository at this point in the history
  • Loading branch information
i-am-zaidali committed Mar 8, 2024
1 parent 3307fef commit fa74445
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions timer/timers.py
Expand Up @@ -97,6 +97,8 @@ async def cog_load(self):

self.max_duration: int = await self.config.max_duration()
self.bot.add_view(self.view)
if self.bot.get_cog("Dev"):
self.bot.add_dev_env_value("timer", lambda x: self)

async def get_timer(self, guild_id: int, timer_id: int):
if not (guild := self.cache.get(guild_id)):
Expand Down Expand Up @@ -126,6 +128,7 @@ async def _back_to_config(self):
async def cog_unload(self):
self.task.cancel()
self.view.stop()
self.bot.remove_dev_env_value("timer")
await self._back_to_config()

@tasks.loop(seconds=1)
Expand All @@ -146,6 +149,9 @@ async def end_timer(self):
key=lambda x: math.ceil(x.remaining_time),
sortkey=lambda x: math.ceil(x.remaining_time),
)
if not self.to_end:
log.debug("No timers to end, stopping task.")
return self.end_timer.stop()

interval = max(math.ceil(getattr(next(iter(self.to_end), None), "remaining_time", 1)), 1)
self.end_timer.change_interval(seconds=interval)
Expand Down Expand Up @@ -189,7 +195,10 @@ async def timer_start(
await ctx.tick(message="Timer for `{}` started!".format(name))

self.to_end.clear()
self.end_timer.restart()
if self.end_timer.is_running():
self.end_timer.restart()
else:
self.end_timer.start()
self.task = self.end_timer.get_task()

@timer.command(name="end")
Expand All @@ -210,7 +219,10 @@ async def timer_end(self, ctx: commands.Context, timer_id: int):
await ctx.tick(message="Timer ended!")

self.to_end.clear()
self.end_timer.restart()
if self.end_timer.is_running():
self.end_timer.restart()
else:
self.end_timer.start()
self.task = self.end_timer.get_task()

@timer.command(name="list")
Expand Down

0 comments on commit fa74445

Please sign in to comment.