From 7fd0801d78938a7da2a8108f6ccbc5ab96cd57bd Mon Sep 17 00:00:00 2001 From: "Nyeki.py" <84998222+Nekidev@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:15:27 -0300 Subject: [PATCH] fix(bot): :bug: Calls not being hung up after 1 or 15 minutes. --- payphone/cogs/calls/cog.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/payphone/cogs/calls/cog.py b/payphone/cogs/calls/cog.py index 9f4a434..fd2c6de 100644 --- a/payphone/cogs/calls/cog.py +++ b/payphone/cogs/calls/cog.py @@ -1,7 +1,5 @@ from datetime import datetime, timezone, timedelta -import asyncio - import secrets import discord @@ -26,15 +24,15 @@ class Calls(base.Cog): def __init__(self, bot_instance: bot.Payphone): self.bot = bot_instance + self.hangup_calls.add_exception_type(Exception) self.hangup_calls.start() def cog_unload(self) -> None: - self.hangup_calls.stop() + self.hangup_calls.cancel() return super().cog_unload() - @tasks.loop(minutes=1) - @transactions.atomic() + @tasks.loop(seconds=15) async def hangup_calls(self) -> None: """Hangs up inactive calls. @@ -58,11 +56,13 @@ class Calls(base.Cog): if ( message is None and call.started_at < datetime.now(timezone.utc) - timedelta(minutes=1) - ) or message.created_at < datetime.now(timezone.utc) - timedelta( - minutes=15 + ) or ( + message is not None + and message.created_at + < datetime.now(timezone.utc) - timedelta(minutes=15) ): for channel in call.channels: - hung_up_channel_ids += channel.id + hung_up_channel_ids.append(channel.id) try: await self.bot.get_partial_messageable(channel.id).send( @@ -72,14 +72,11 @@ class Calls(base.Cog): except errors.DiscordException: pass - await self.bot.redis.srem(constants.CHANNELS_ONCALL_SET, *hung_up_channel_ids) - - @hangup_calls.before_loop - async def before_hangup_calls(self): - """Wait until the bot is ready to start looping.""" - - await self.bot.wait_until_ready() - await asyncio.wait(5) + if len(hung_up_channel_ids) > 0: + await self.bot.redis.srem( + constants.CHANNELS_ONCALL_SET, *hung_up_channel_ids + ) + await models.Channel.filter(id__in=hung_up_channel_ids).update(call=None) @commands.command(aliases=["c"]) @commands.guild_only()