Skip to content

Commit 8c00456

Browse files
Lulalabypre-commit-ci[bot]NeloBlivionPaillat-dev
authored
feat: Add support for user.primary_guild (#2876)
Signed-off-by: Lala Sabathil <[email protected]> Signed-off-by: plun1331 <[email protected]> Signed-off-by: UK <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: UK <[email protected]> Co-authored-by: Paillat <[email protected]>
1 parent 59d3f38 commit 8c00456

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ These changes are available on the `master` branch, but have not yet been releas
7171
([#2854](https://github.com/Pycord-Development/pycord/pull/2854))
7272
- Added `AuditLogDiff.communication_disabled_until`.
7373
([#2883](https://github.com/Pycord-Development/pycord/pull/2883))
74+
- Added `discord.User.primary_guild` and the `PrimaryGuild` class.
75+
([#2876](https://github.com/Pycord-Development/pycord/pull/2876))
7476

7577
### Fixed
7678

discord/asset.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
if TYPE_CHECKING:
4040
ValidStaticFormatTypes = Literal["webp", "jpeg", "jpg", "png"]
4141
ValidAssetFormatTypes = Literal["webp", "jpeg", "jpg", "png", "gif"]
42+
from .state import ConnectionState
43+
4244

4345
VALID_STATIC_FORMATS = frozenset({"jpeg", "jpg", "webp", "png"})
4446
VALID_ASSET_FORMATS = VALID_STATIC_FORMATS | {"gif"}
@@ -201,6 +203,33 @@ def _from_avatar_decoration(
201203
animated=animated,
202204
)
203205

206+
@classmethod
207+
def _from_user_primary_guild_tag(
208+
cls, state: ConnectionState, identity_guild_id: int, badge_id: str
209+
) -> Asset:
210+
"""Creates an Asset for a user's primary guild (tag) badge.
211+
212+
Parameters
213+
----------
214+
state: ConnectionState
215+
The connection state.
216+
identity_guild_id: int
217+
The ID of the guild.
218+
badge_id: str
219+
The badge hash/id.
220+
221+
Returns
222+
-------
223+
:class:`Asset`
224+
The primary guild badge asset.
225+
"""
226+
return cls(
227+
state,
228+
url=f"{Asset.BASE}/guild-tag-badges/{identity_guild_id}/{badge_id}.png?size=256",
229+
key=badge_id,
230+
animated=False,
231+
)
232+
204233
@classmethod
205234
def _from_guild_avatar(
206235
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+
__all__ = ("PrimaryGuild",)
34+
35+
36+
class PrimaryGuild:
37+
"""
38+
Represents a Discord Primary Guild.
39+
40+
.. versionadded:: 2.7
41+
42+
Attributes
43+
----------
44+
identity_guild_id: int
45+
The ID of the guild.
46+
identity_enabled: :class:`bool`
47+
Whether the primary guild is enabled.
48+
tag: str
49+
The tag of the primary guild.
50+
"""
51+
52+
def __init__(self, data: PrimaryGuildPayload, state: "ConnectionState") -> None:
53+
self.identity_guild_id: int | None = (
54+
int(data.get("identity_guild_id") or 0) or None
55+
)
56+
self.identity_enabled: bool | None = data.get("identity_enabled", None)
57+
self.tag: str | None = data.get("tag", None)
58+
self._badge: str | None = data.get("badge", None)
59+
self._state: "ConnectionState" = state
60+
61+
def __repr__(self) -> str:
62+
return f"<PrimaryGuild identity_guild_id={self.identity_guild_id} identity_enabled={self.identity_enabled} tag={self.tag}>"
63+
64+
@property
65+
def badge(self) -> Asset | None:
66+
"""Returns the badge asset, if available.
67+
68+
.. versionadded:: 2.7
69+
"""
70+
if self._badge is None:
71+
return None
72+
return Asset._from_user_primary_guild_tag(
73+
self._state, self.identity_guild_id, self._badge
74+
)

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from .flags import PublicUserFlags
3636
from .iterators import EntitlementIterator
3737
from .monetization import Entitlement
38+
from .primary_guild import PrimaryGuild
3839
from .utils import MISSING, _bytes_to_base64_data, snowflake_time
3940

4041
if TYPE_CHECKING:
@@ -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,13 @@ def _update(self, data: UserPayload) -> None:
151154
self.nameplate = Nameplate(data=nameplate, state=self._state)
152155
else:
153156
self.nameplate = None
157+
primary_guild_payload = data.get("primary_guild", None)
158+
if primary_guild_payload and primary_guild_payload.get("identity_enabled"):
159+
self.primary_guild = PrimaryGuild(
160+
data=primary_guild_payload, state=self._state
161+
)
162+
else:
163+
self.primary_guild = None
154164
self._public_flags = data.get("public_flags", 0)
155165
self.bot = data.get("bot", False)
156166
self.system = data.get("system", False)
@@ -170,6 +180,7 @@ def _copy(cls: type[BU], user: BU) -> BU:
170180
self.bot = user.bot
171181
self._state = user._state
172182
self._public_flags = user._public_flags
183+
self.primary_guild = user.primary_guild
173184

174185
return self
175186

0 commit comments

Comments
 (0)