Skip to content

Commit 1cdc82a

Browse files
committed
feat: Add support for user.primary_guild
Introduces the PrimaryGuild class and its type definition, adds the primary_guild attribute to the User model, and implements asset support for primary guild badges. Updates the changelog to document these additions.
1 parent 2b63af9 commit 1cdc82a

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ These changes are available on the `master` branch, but have not yet been releas
6969
([#2818](https://github.com/Pycord-Development/pycord/pull/2818))
7070
- Added `Interaction.attachment_size_limit`.
7171
([#2854](https://github.com/Pycord-Development/pycord/pull/2854))
72+
- Added `discord.User.primary_guild` and the `PrimaryGuild` class.
73+
([#2876](https://github.com/Pycord-Development/pycord/pull/2876))
7274

7375
### Fixed
7476

discord/asset.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ def _from_avatar_decoration(
201201
animated=animated,
202202
)
203203

204+
@classmethod
205+
def _from_user_primary_guild_tag(cls, state, identity_guild_id: int, badge_id: str) -> Asset:
206+
"""Creates an Asset for a user's primary guild (tag) badge.
207+
208+
Parameters
209+
----------
210+
state: ConnectionState
211+
The connection state.
212+
identity_guild_id: int
213+
The ID of the guild.
214+
badge_id: str
215+
The badge hash/id.
216+
217+
Returns
218+
-------
219+
:class:`Asset`
220+
The primary guild badge asset.
221+
"""
222+
return cls(
223+
state,
224+
url=f"{Asset.BASE}/guild-tag-badges/{identity_guild_id}/{badge_id}.png?size=256",
225+
key=badge_id,
226+
animated=False,
227+
)
228+
204229
@classmethod
205230
def _from_guild_avatar(
206231
cls, state, guild_id: int, member_id: int, avatar: str

discord/primary_guild.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2021-present Pycord Development
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
DEALINGS IN THE SOFTWARE.
23+
"""
24+
25+
from typing import TYPE_CHECKING
26+
27+
if TYPE_CHECKING:
28+
from .state import ConnectionState
29+
30+
from .asset import Asset
31+
from .types.primary_guild import PrimaryGuild as PrimaryGuildPayload
32+
33+
34+
class PrimaryGuild:
35+
"""
36+
Represents a Discord Primary Guild.
37+
38+
.. versionadded:: 2.7
39+
40+
Attributes
41+
----------
42+
identity_guild_id: int
43+
The ID of the guild.
44+
identity_enabled: :class:`bool`
45+
Whether the primary guild is enabled.
46+
tag: str
47+
The tag of the primary guild.
48+
"""
49+
50+
def __init__(self, data: PrimaryGuildPayload, state: "ConnectionState") -> None:
51+
self.identity_guild_id: int = data["identity_guild_id"]
52+
self.identity_enabled: bool | None = data.get("identity_enabled", None)
53+
self.tag: str = data["tag"]
54+
self._badge: str = data["badge"]
55+
self._state: "ConnectionState" = state
56+
57+
def __repr__(self) -> str:
58+
return f"<PrimaryGuild identity_guild_id={self.identity_guild_id} identity_enabled={self.identity_enabled} tag={self.tag}>"
59+
60+
@property
61+
def badge(self) -> Asset | None:
62+
"""Returns the badge asset, if available.
63+
64+
.. versionadded:: 2.7
65+
66+
.. note::
67+
This information is only available via :meth:`Client.fetch_user`.
68+
"""
69+
if self._badge is None:
70+
return None
71+
return Asset._from_user_primary_guild_tag(self._state, self.identity_guild_id, self._badge)
72+
73+
74+
__all__ = ("PrimaryGuild",)

discord/types/primary_guild.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2021-present Pycord Development
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
DEALINGS IN THE SOFTWARE.
23+
"""
24+
25+
from __future__ import annotations
26+
27+
from typing import TypedDict
28+
29+
from .snowflake import Snowflake
30+
31+
32+
class PrimaryGuild(TypedDict):
33+
identity_guild_id: Snowflake
34+
identity_enabled: bool | None
35+
tag: str
36+
badge: str

discord/user.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import discord.abc
3131

32+
from .primary_guild import PrimaryGuild
3233
from .asset import Asset
3334
from .collectibles import Nameplate
3435
from .colour import Colour
@@ -78,6 +79,7 @@ class BaseUser(_UserTag):
7879
"_avatar_decoration",
7980
"_state",
8081
"nameplate",
82+
"primary_guild",
8183
)
8284

8385
if TYPE_CHECKING:
@@ -94,6 +96,7 @@ class BaseUser(_UserTag):
9496
_avatar_decoration: dict | None
9597
_public_flags: int
9698
nameplate: Nameplate | None
99+
primary_guild: PrimaryGuild | None
97100

98101
def __init__(
99102
self, *, state: ConnectionState, data: UserPayload | PartialUserPayload
@@ -151,6 +154,7 @@ def _update(self, data: UserPayload) -> None:
151154
self.nameplate = Nameplate(data=nameplate, state=self._state)
152155
else:
153156
self.nameplate = None
157+
self.primary_guild = data.get("primary_guild", None)
154158
self._public_flags = data.get("public_flags", 0)
155159
self.bot = data.get("bot", False)
156160
self.system = data.get("system", False)
@@ -170,6 +174,7 @@ def _copy(cls: type[BU], user: BU) -> BU:
170174
self.bot = user.bot
171175
self._state = user._state
172176
self._public_flags = user._public_flags
177+
self.primary_guild = user.primary_guild
173178

174179
return self
175180

0 commit comments

Comments
 (0)