Skip to content

Commit 94ec8f9

Browse files
NeloBlivionpre-commit-ci[bot]
authored andcommitted
fix: retain original File item urls for editing (Pycord-Development#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> (cherry picked from commit bda1dfe)
1 parent b27b662 commit 94ec8f9

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
@@ -665,18 +665,36 @@ class UnfurledMediaItem(AssetMixin):
665665
def __init__(self, url: str):
666666
self._state = None
667667
self._url: str = url
668+
self._static_url: str | None = (
669+
url if url and url.startswith("attachment://") else None
670+
)
668671
self.proxy_url: str | None = None
669672
self.height: int | None = None
670673
self.width: int | None = None
671674
self.content_type: str | None = None
672675
self.flags: AttachmentFlags | None = None
673676
self.attachment_id: int | None = None
674677

678+
def __repr__(self) -> str:
679+
return (
680+
f"<UnfurledMediaItem url={self.url!r} attachment_id={self.attachment_id}>"
681+
)
682+
683+
def __str__(self) -> str:
684+
return self.url or self.__repr__()
685+
675686
@property
676687
def url(self) -> str:
677688
"""Returns this media item's url."""
678689
return self._url
679690

691+
@url.setter
692+
def url(self, value: str) -> None:
693+
self._url = value
694+
self._static_url = (
695+
value if value and value.startswith("attachment://") else None
696+
)
697+
680698
@classmethod
681699
def from_dict(cls, data: UnfurledMediaItemPayload, state=None) -> UnfurledMediaItem:
682700

@@ -691,7 +709,7 @@ def from_dict(cls, data: UnfurledMediaItemPayload, state=None) -> UnfurledMediaI
691709
return r
692710

693711
def to_dict(self) -> dict[str, str]:
694-
return {"url": self.url}
712+
return {"url": self._static_url or self.url}
695713

696714

697715
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)