From 8cd1613e3c8d013168d967c5a718ad4644da7eb3 Mon Sep 17 00:00:00 2001 From: "Nyeki.py" <84998222+Nekidev@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:35:32 -0300 Subject: [PATCH] fix(bot): :bug: Attempt to fix `p.hangup`. --- payphone/cogs/calls/cog.py | 18 ++++++++++++------ payphone/db/utils.py | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/payphone/cogs/calls/cog.py b/payphone/cogs/calls/cog.py index fd2c6de..5e99d04 100644 --- a/payphone/cogs/calls/cog.py +++ b/payphone/cogs/calls/cog.py @@ -173,6 +173,10 @@ class Calls(base.Cog): i += 1 if other_channel_id in last_calls_other_channels_ids: + await self.bot.redis.sadd( + constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id) + ) + other_channel_id = await self.bot.redis.spop( constants.CHANNELS_SEARCH_QUEUE ) @@ -180,9 +184,6 @@ class Calls(base.Cog): if other_channel_id is None: break - await self.bot.redis.sadd( - constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id) - ) continue break @@ -354,12 +355,17 @@ class Calls(base.Cog): i += 1 if other_channel_id in last_calls_other_channels_ids: - other_channel_id = await self.bot.redis.spop( - constants.CHANNELS_SEARCH_QUEUE - ) await self.bot.redis.sadd( constants.CHANNELS_SEARCH_QUEUE, str(other_channel_id) ) + + other_channel_id = await self.bot.redis.spop( + constants.CHANNELS_SEARCH_QUEUE + ) + + if other_channel_id is None: + break + continue break diff --git a/payphone/db/utils.py b/payphone/db/utils.py index 7fc0c23..d6e81eb 100644 --- a/payphone/db/utils.py +++ b/payphone/db/utils.py @@ -5,19 +5,28 @@ import discord from payphone.db import models -async def get_channel(channel: discord.TextChannel) -> models.Channel: +async def get_channel( + channel: discord.TextChannel, for_update: bool = False +) -> models.Channel: """Returns a channel from the database, creating or updating it if it already exists. Args: channel (discord.TextChannel): The channel to fetch. + for_update (bool, optional): Whether the select should be FOR UPDATE or not. Returns: models.Channel: The channel row. """ - row, _ = await models.Channel.update_or_create( - id=channel.id, server=channel.guild.id - ) + model = models.Channel + + if for_update: + model = model.select_for_update() + + row = await model.get_or_none(id=channel.id) + + if row is None: + row = await models.Channel.create(id=channel.id, server_id=channel.guild.id) channel_name = channel.name.replace("discord", "d").replace("wumpus", "w") webhook_name = f"Payphone #{channel_name}"