From f0437f30aef9a7682efe5191290a5e7b99ab75b4 Mon Sep 17 00:00:00 2001 From: Rafael Bradley Date: Sun, 16 Mar 2025 00:49:34 -0300 Subject: [PATCH] fix(shard): :bug: Channel webhooks suddenly stopping working. --- payphone/bot.py | 2 +- payphone/cogs/calls/cog.py | 47 ++++++++++++++++++++++---------------- payphone/core/calls.py | 23 +++++++++++++++++++ 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/payphone/bot.py b/payphone/bot.py index 6df55b7..e23fa29 100644 --- a/payphone/bot.py +++ b/payphone/bot.py @@ -194,7 +194,7 @@ class Payphone(commands.AutoShardedBot): return prefix - def clean_message(self, message: discord.Message, call) -> str: + def clean_message(self, message: discord.Message) -> str: """Cleans a message. Args: diff --git a/payphone/cogs/calls/cog.py b/payphone/cogs/calls/cog.py index f07c857..e1c4605 100644 --- a/payphone/cogs/calls/cog.py +++ b/payphone/cogs/calls/cog.py @@ -247,7 +247,7 @@ class Calls(base.Cog): ), ) - message_content = self.bot.clean_message(message, call) + message_content = self.bot.clean_message(message) if message.stickers: if participant.user.flag & ( @@ -289,7 +289,7 @@ class Calls(base.Cog): other_channel ).fetch_message(original_message_id) - replied_message_content = self.bot.clean_message(replied_message, call) + replied_message_content = self.bot.clean_message(replied_message) replied_participant = await call.get_participant( replied_message.author.id ) @@ -364,26 +364,33 @@ class Calls(base.Cog): ) async with aiohttp.ClientSession() as session: - webhook = discord.Webhook.from_url( - await self.bot.call_handler.get_channel_webhook_url(other_channel), - session=session, - bot_token=constants.TOKEN, - ) + for _ in range(3): + try: + webhook = discord.Webhook.from_url( + await self.bot.call_handler.get_channel_webhook_url(other_channel), + session=session, + bot_token=constants.TOKEN, + ) - propagated_msg = await webhook.send( - content=message_content, - username=participant.display_name, - avatar_url=participant.display_avatar_url, - embeds=embeds, - wait=True, - ) + propagated_msg = await webhook.send( + content=message_content, + username=participant.display_name, + avatar_url=participant.display_avatar_url, + embeds=embeds, + wait=True, + ) - await call.set_message( - message.id, - message.channel.id, - propagated_msg.id, - message.author.id, - ) + await call.set_message( + message.id, + message.channel.id, + propagated_msg.id, + message.author.id, + ) + + break + + except discord.errors.DiscordException: + await self.bot.call_handler.refresh_channel_webhook_url(other_channel) @commands.Cog.listener() async def on_raw_message_delete( diff --git a/payphone/core/calls.py b/payphone/core/calls.py index deb6731..d40373d 100644 --- a/payphone/core/calls.py +++ b/payphone/core/calls.py @@ -563,6 +563,29 @@ class CallHandler: await self.bot.redis.hset("calls:channels:webhooks", channel_id, webhook.url) return webhook.url + + async def refresh_channel_webhook_url(self, channel_id: int) -> str: + """Refreshes a channel's webhook. + + Args: + channel_id (int): The channel ID of the webhook to refresh. + + Returns: + str: The new webhook's URL. + """ + + await self.bot.redis.hdel("calls:channels:webhooks", channel_id) + + channel = await self.bot.get_or_fetch_channel(channel_id) + + if channel is None: + return + + for webhook in await channel.webhooks(): + if webhook.name == "Payphone": + await webhook.delete() + + return await self.get_channel_webhook_url(channel_id) async def get_message(self, message_id: int) -> Message | None: """Gets a message sent in a call.