Skip to content

Commit bda1dfe

Browse files
fix: retain original File item urls for editing (#2847)
* fix file component patching * style(pre-commit): auto fixes from pre-commit.com hooks * component typing --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4392105 commit bda1dfe

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

discord/components.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,36 @@ class UnfurledMediaItem(AssetMixin):
670670
def __init__(self, url: str):
671671
self._state = None
672672
self._url: str = url
673+
self._static_url: str | None = (
674+
url if url and url.startswith("attachment://") else None
675+
)
673676
self.proxy_url: str | None = None
674677
self.height: int | None = None
675678
self.width: int | None = None
676679
self.content_type: str | None = None
677680
self.flags: AttachmentFlags | None = None
678681
self.attachment_id: int | None = None
679682

683+
def __repr__(self) -> str:
684+
return (
685+
f"<UnfurledMediaItem url={self.url!r} attachment_id={self.attachment_id}>"
686+
)
687+
688+
def __str__(self) -> str:
689+
return self.url or self.__repr__()
690+
680691
@property
681692
def url(self) -> str:
682693
"""Returns this media item's url."""
683694
return self._url
684695

696+
@url.setter
697+
def url(self, value: str) -> None:
698+
self._url = value
699+
self._static_url = (
700+
value if value and value.startswith("attachment://") else None
701+
)
702+
685703
@classmethod
686704
def from_dict(cls, data: UnfurledMediaItemPayload, state=None) -> UnfurledMediaItem:
687705

@@ -696,7 +714,7 @@ def from_dict(cls, data: UnfurledMediaItemPayload, state=None) -> UnfurledMediaI
696714
return r
697715

698716
def to_dict(self) -> dict[str, str]:
699-
return {"url": self.url}
717+
return {"url": self._static_url or self.url}
700718

701719

702720
class Thumbnail(Component):

discord/ui/file.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def size(self) -> int:
9090
"""The size of this file in bytes, if provided by Discord."""
9191
return self._underlying.size
9292

93+
def refresh_component(self, component: FileComponent) -> None:
94+
original = self._underlying.file
95+
component.file._static_url = original._static_url
96+
self._underlying = component
97+
9398
def to_component_dict(self) -> FileComponentPayload:
9499
return self._underlying.to_dict()
95100

0 commit comments

Comments
 (0)