Skip to content

Commit ab0e036

Browse files
feat: Add more image URL helpers (#699)
* fix: UnboundLocalError when server has no icon * feat: implement Guild.banner_url * feat: add discovery & invite splash banner URLs * feat: add user banner URL * feat: get member avatar for a specified guild * fix: UnboundLocalError when server has no icon * feat: implement Guild.banner_url * feat: add discovery & invite splash banner URLs * feat: add user banner URL * feat: get member avatar for a specified guild * ci: correct from checks. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e7b7a2f commit ab0e036

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

interactions/api/models/guild.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,17 +1660,57 @@ async def get_all_members(self) -> List[Member]:
16601660
return [Member(**_, _client=self._client) for _ in _all_members]
16611661

16621662
@property
1663-
def icon_url(self) -> str:
1663+
def icon_url(self) -> Optional[str]:
16641664
"""
16651665
Returns the URL of the guild's icon.
1666-
:return: URL of the guild's icon (None will be returned if none of icon is set)
1666+
:return: URL of the guild's icon (None will be returned if no icon is set)
16671667
:rtype: str
16681668
"""
1669-
if self.icon is not None:
1670-
url = f"https://cdn.discordapp.com/icons/{int(self.id)}/{self.icon}"
1671-
url += ".gif" if self.icon.startswith("a_") else ".png"
1669+
if not self.icon:
1670+
return None
1671+
1672+
url = f"https://cdn.discordapp.com/icons/{int(self.id)}/{self.icon}"
1673+
url += ".gif" if self.icon.startswith("a_") else ".png"
1674+
return url
1675+
1676+
@property
1677+
def banner_url(self) -> Optional[str]:
1678+
"""
1679+
Returns the URL of the guild's banner.
1680+
:return: URL of the guild's banner (None will be returned if no banner is set)
1681+
:rtype: str
1682+
"""
1683+
if not self.banner:
1684+
return None
1685+
1686+
url = f"https://cdn.discordapp.com/banners/{int(self.id)}/{self.banner}"
1687+
url += ".gif" if self.banner.startswith("a_") else ".png"
16721688
return url
16731689

1690+
@property
1691+
def splash_url(self) -> Optional[str]:
1692+
"""
1693+
Returns the URL of the guild's invite splash banner.
1694+
:return: URL of the guild's invite splash banner (None will be returned if no banner is set)
1695+
:rtype: str
1696+
"""
1697+
if not self.banner:
1698+
return None
1699+
1700+
return f"https://cdn.discordapp.com/splashes/{int(self.id)}/{self.splash}.png"
1701+
1702+
@property
1703+
def discovery_splash_url(self) -> Optional[str]:
1704+
"""
1705+
Returns the URL of the guild's discovery splash banner.
1706+
:return: URL of the guild's discovery splash banner (None will be returned if no banner is set)
1707+
:rtype: str
1708+
"""
1709+
if not self.banner:
1710+
return None
1711+
1712+
return f"https://cdn.discordapp.com/discovery-splashes/{int(self.id)}/{self.discovery_splash}.png"
1713+
16741714

16751715
class GuildPreview(DictSerializerMixin):
16761716
"""

interactions/api/models/guild.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,13 @@ class Guild(DictSerializerMixin):
414414
) -> List[Member]: ...
415415
async def get_all_members(self) -> List[Member]: ...
416416
@property
417-
def icon_url(self) -> str: ...
417+
def icon_url(self) -> Optional[str]: ...
418+
@property
419+
def banner_url(self) -> Optional[str]: ...
420+
@property
421+
def splash_url(self) -> Optional[str]: ...
422+
@property
423+
def discovery_splash_url(self) -> Optional[str]: ...
418424

419425
class GuildPreview(DictSerializerMixin):
420426
_json: dict

interactions/api/models/member.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,18 @@ async def add_to_thread(
370370
user_id=int(self.user.id),
371371
thread_id=thread_id,
372372
)
373+
374+
def get_member_avatar_url(self, guild_id: int) -> Optional[str]:
375+
"""
376+
Returns the URL of the member's avatar for the specified guild.
377+
:param guild_id: The id of the guild to get the member's avatar from
378+
:type guild_id: int
379+
:return: URL of the members's avatar (None will be returned if no avatar is set)
380+
:rtype: str
381+
"""
382+
if not self.avatar:
383+
return None
384+
385+
url = f"https://cdn.discordapp.com/guilds/{guild_id}/users/{int(self.user.id)}/avatars/{self.avatar}"
386+
url += ".gif" if self.avatar.startswith("a_") else ".png"
387+
return url

interactions/api/models/member.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ class Member(DictSerializerMixin):
9090
self,
9191
thread_id: int,
9292
) -> None: ...
93+
def get_member_avatar_url(self, guild_id: int) -> Optional[str]: ...

interactions/api/models/user.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from .flags import UserFlags
24
from .misc import DictSerializerMixin, Snowflake
35

@@ -88,3 +90,17 @@ def avatar_url(self) -> str:
8890
else:
8991
url += f"embed/avatars/{int(self.discriminator) % 5}.png"
9092
return url
93+
94+
@property
95+
def banner_url(self) -> Optional[str]:
96+
"""
97+
Returns the URL of the user's banner.
98+
:return: URL of the user's banner (None will be returned if no banner is set)
99+
:rtype: str
100+
"""
101+
if not self.banner:
102+
return None
103+
104+
url = f"https://cdn.discordapp.com/banners/{int(self.id)}/{self.banner}"
105+
url += ".gif" if self.banner.startswith("a_") else ".png"
106+
return url

interactions/api/models/user.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ class User(DictSerializerMixin):
2727
def mention(self) -> str: ...
2828
@property
2929
def avatar_url(self) -> str: ...
30+
@property
31+
def banner_url(self) -> Optional[str]: ...

0 commit comments

Comments
 (0)