From 100de11ce61a0913d40b9660b1b07ebbb51d1ebc Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Fri, 11 Aug 2023 02:51:36 +0200 Subject: [PATCH] Fix http errors for interaction deferring and message changes (#6229) --- redbot/core/utils/menus.py | 2 +- redbot/core/utils/views.py | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/redbot/core/utils/menus.py b/redbot/core/utils/menus.py index 5c01423da91..d3d057285a1 100644 --- a/redbot/core/utils/menus.py +++ b/redbot/core/utils/menus.py @@ -37,6 +37,7 @@ def __init__(self, emoji: discord.PartialEmoji, func: _ControlCallable): self.func = func async def callback(self, interaction: discord.Interaction): + await interaction.response.defer() ctx = self.view.ctx pages = self.view.source.entries controls = None @@ -52,7 +53,6 @@ async def callback(self, interaction: discord.Interaction): await self.func(ctx, pages, controls, message, page, timeout, emoji) except Exception: pass - await interaction.response.defer() async def menu( diff --git a/redbot/core/utils/views.py b/redbot/core/utils/views.py index ee20fbfb39c..10c1f23ecab 100644 --- a/redbot/core/utils/views.py +++ b/redbot/core/utils/views.py @@ -193,14 +193,18 @@ def source(self): return self._source async def on_timeout(self): - if self.delete_after_timeout and not self.message.flags.ephemeral: - await self.message.delete() - elif self.disable_after_timeout: - for child in self.children: - child.disabled = True - await self.message.edit(view=self) - else: - await self.message.edit(view=None) + try: + if self.delete_after_timeout and not self.message.flags.ephemeral: + await self.message.delete() + elif self.disable_after_timeout: + for child in self.children: + child.disabled = True + await self.message.edit(view=self) + else: + await self.message.edit(view=None) + except discord.HTTPException: + # message could no longer be there or we may not be able to edit/delete it anymore + pass def _get_select_menu(self): # handles modifying the select menu if more than 25 pages are provided @@ -523,9 +527,14 @@ async def on_timeout(self): if self.disable_buttons: self.confirm_button.disabled = True self.dismiss_button.disabled = True - await self.message.edit(view=self) + view = self else: - await self.message.edit(view=None) + view = None + try: + await self.message.edit(view=view) + except discord.HTTPException: + # message could no longer be there or we may not be able to edit it anymore + pass @discord.ui.button(label=_("Yes"), style=discord.ButtonStyle.green) async def confirm_button(self, interaction: discord.Interaction, button: discord.ui.Button):