Skip to content

Commit 6683c68

Browse files
Lumabotspre-commit-ci[bot]Paillat-devJustaSqu1d
authored
fix: ♻️ Handle exceptions during message edits in Paginator and BaseView timeouts (#3019)
* fix: ♻️ Handle discord.HTTPException during message edits in Paginator and BaseView * style(pre-commit): auto fixes from pre-commit.com hooks * fix: ♻️ Update changelog to reflect changes in BaseView and Paginator timeout behavior * Update discord/ext/pages/pagination.py Co-authored-by: Paillat <jeremiecotti@ik.me> Signed-off-by: Lumouille <144063653+Lumabots@users.noreply.github.com> * fix: reorder timeout error handling in changelog * style(pre-commit): auto fixes from pre-commit.com hooks * fix: format changelog for better readability * style(pre-commit): auto fixes from pre-commit.com hooks * chore: update changelog with new features, changes, and fixes for version 2.7.0 * style(pre-commit): auto fixes from pre-commit.com hooks * chore: update changelog with new features, changes, and fixes for Components V2 and other improvements * fix: improve error handling and clean up type annotations in Paginator and BaseView * style(pre-commit): auto fixes from pre-commit.com hooks * fix: improve message handling and type hint formatting in BaseView and ViewStore * style(pre-commit): auto fixes from pre-commit.com hooks * fix: replace discord error references with local imports in BaseView * style(pre-commit): auto fixes from pre-commit.com hooks * Apply suggestion from @Paillat-dev Signed-off-by: Paillat <jeremiecotti@ik.me> * style(pre-commit): auto fixes from pre-commit.com hooks * Update CHANGELOG.md Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com> Signed-off-by: Lumouille <144063653+Lumabots@users.noreply.github.com> --------- Signed-off-by: Lumouille <144063653+Lumabots@users.noreply.github.com> Signed-off-by: Paillat <jeremiecotti@ik.me> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <jeremiecotti@ik.me> Co-authored-by: Paillat <paillat@pycord.dev> Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com>
1 parent b2d5047 commit 6683c68

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ These changes are available on the `master` branch, but have not yet been releas
2626

2727
### Fixed
2828

29+
- Fixed an issue where views raised unnecessary errors on timeout.
30+
([#3019](https://github.com/Pycord-Development/pycord/pull/3019))
2931
- Fixed `RawMessageUpdateEvent.cached_message` being always `None` even when the message
3032
was cached. ([#3038](https://github.com/Pycord-Development/pycord/pull/3038))
3133
- Fixed an issue with downloading animated emojis which were originally uploaded as WebP

discord/ext/pages/pagination.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from __future__ import annotations
2626

27+
import contextlib
2728
from typing import List
2829

2930
import discord
@@ -604,11 +605,12 @@ async def on_timeout(self) -> None:
604605
page = self.pages[self.current_page]
605606
page = self.get_page_content(page)
606607
files = page.update_files()
607-
await self.message.edit(
608-
view=self,
609-
files=files or [],
610-
attachments=[],
611-
)
608+
with contextlib.suppress(discord.NotFound, discord.Forbidden):
609+
await self.message.edit(
610+
view=self,
611+
files=files or [],
612+
attachments=[],
613+
)
612614

613615
async def disable(
614616
self,

discord/ui/view.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29+
import contextlib
2930
import os
3031
import sys
3132
import time
@@ -56,7 +57,7 @@
5657
from ..components import Thumbnail as ThumbnailComponent
5758
from ..components import _component_factory
5859
from ..enums import ChannelType
59-
from ..utils import find
60+
from ..errors import Forbidden, NotFound
6061
from .core import ItemInterface
6162
from .item import ItemCallbackType, ViewItem
6263

@@ -97,7 +98,6 @@ def _walk_all_components_v2(components: list[Component]) -> Iterator[Component]:
9798

9899

99100
def _component_to_item(component: Component) -> ViewItem[V]:
100-
101101
if isinstance(component, ButtonComponent):
102102
from .button import Button
103103

@@ -321,9 +321,10 @@ async def on_timeout(self) -> None:
321321
message = self.message
322322

323323
if message:
324-
m = await message.edit(view=self)
325-
if m:
326-
self._message = m
324+
with contextlib.suppress(NotFound, Forbidden):
325+
m = await message.edit(view=self)
326+
if m:
327+
self._message = m
327328

328329
async def on_check_failure(self, interaction: Interaction) -> None:
329330
"""|coro|
@@ -692,7 +693,7 @@ def add_item(self, item: ViewItem[V]) -> Self:
692693

693694
if item._underlying.is_v2():
694695
raise ValueError(
695-
f"cannot use V2 components in View. Use DesignerView instead."
696+
"cannot use V2 components in View. Use DesignerView instead."
696697
)
697698
if isinstance(item._underlying, ActionRowComponent):
698699
for i in item.children:
@@ -729,7 +730,9 @@ def clear_items(self) -> None:
729730
def refresh(self, components: list[Component]):
730731
# This is pretty hacky at the moment
731732
old_state: dict[tuple[int, str], ViewItem[V]] = {
732-
(item.type.value, item.custom_id): item for item in self.children if item.is_dispatchable() # type: ignore
733+
(item.type.value, item.custom_id): item
734+
for item in self.children
735+
if item.is_dispatchable() # type: ignore
733736
}
734737
children: list[ViewItem[V]] = [
735738
item for item in self.children if not item.is_dispatchable()
@@ -889,7 +892,7 @@ def add_item(self, item: ViewItem[V]) -> Self:
889892

890893
if isinstance(item._underlying, (SelectComponent, ButtonComponent)):
891894
raise ValueError(
892-
f"cannot add Select or Button to DesignerView directly. Use ActionRow instead."
895+
"cannot add Select or Button to DesignerView directly. Use ActionRow instead."
893896
)
894897

895898
super().add_item(item)
@@ -951,7 +954,10 @@ def add_view(self, view: BaseView, message_id: int | None = None):
951954
view._start_listening_from_store(self)
952955
for item in view.walk_children():
953956
if item.is_storable():
954-
self._views[(item.type.value, message_id, item.custom_id)] = (view, item) # type: ignore
957+
self._views[(item.type.value, message_id, item.custom_id)] = (
958+
view,
959+
item,
960+
) # type: ignore
955961

956962
if message_id is not None:
957963
self._synced_message_views[message_id] = view

0 commit comments

Comments
 (0)