diff --git a/discord/components.py b/discord/components.py index 37642d3ec2..39576c9eea 100644 --- a/discord/components.py +++ b/discord/components.py @@ -670,6 +670,9 @@ class UnfurledMediaItem(AssetMixin): def __init__(self, url: str): self._state = None self._url: str = url + self._static_url: str | None = ( + url if url and url.startswith("attachment://") else None + ) self.proxy_url: str | None = None self.height: int | None = None self.width: int | None = None @@ -677,11 +680,26 @@ def __init__(self, url: str): self.flags: AttachmentFlags | None = None self.attachment_id: int | None = None + def __repr__(self) -> str: + return ( + f"" + ) + + def __str__(self) -> str: + return self.url or self.__repr__() + @property def url(self) -> str: """Returns this media item's url.""" return self._url + @url.setter + def url(self, value: str) -> None: + self._url = value + self._static_url = ( + value if value and value.startswith("attachment://") else None + ) + @classmethod def from_dict(cls, data: UnfurledMediaItemPayload, state=None) -> UnfurledMediaItem: @@ -696,7 +714,7 @@ def from_dict(cls, data: UnfurledMediaItemPayload, state=None) -> UnfurledMediaI return r def to_dict(self) -> dict[str, str]: - return {"url": self.url} + return {"url": self._static_url or self.url} class Thumbnail(Component): diff --git a/discord/ui/file.py b/discord/ui/file.py index 052e0ea9ab..dc06b83648 100644 --- a/discord/ui/file.py +++ b/discord/ui/file.py @@ -90,6 +90,11 @@ def size(self) -> int: """The size of this file in bytes, if provided by Discord.""" return self._underlying.size + def refresh_component(self, component: FileComponent) -> None: + original = self._underlying.file + component.file._static_url = original._static_url + self._underlying = component + def to_component_dict(self) -> FileComponentPayload: return self._underlying.to_dict()