Skip to content

Commit 69c81c3

Browse files
LulalabyPaillat-dev
authored andcommitted
feat: Add support for user.primary_guild (Pycord-Development#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]> (cherry picked from commit 8c00456) Signed-off-by: Paillat-dev <[email protected]>
1 parent 6085463 commit 69c81c3

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"}
@@ -204,6 +206,33 @@ def _from_avatar_decoration(cls, state, user_id: int, avatar_decoration: str) ->
204206
animated=animated,
205207
)
206208

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

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, Undefined, snowflake_time
3940
from .utils.private import bytes_to_base64_data
4041

@@ -79,6 +80,7 @@ class BaseUser(_UserTag):
7980
"_avatar_decoration",
8081
"_state",
8182
"nameplate",
83+
"primary_guild",
8284
)
8385

8486
if TYPE_CHECKING:
@@ -95,6 +97,7 @@ class BaseUser(_UserTag):
9597
_avatar_decoration: dict | None
9698
_public_flags: int
9799
nameplate: Nameplate | None
100+
primary_guild: PrimaryGuild | None
98101

99102
def __init__(self, *, state: ConnectionState, data: UserPayload | PartialUserPayload) -> None:
100103
self._state = state
@@ -142,6 +145,13 @@ def _update(self, data: UserPayload) -> None:
142145
self.nameplate = Nameplate(data=nameplate, state=self._state)
143146
else:
144147
self.nameplate = None
148+
primary_guild_payload = data.get("primary_guild", None)
149+
if primary_guild_payload and primary_guild_payload.get("identity_enabled"):
150+
self.primary_guild = PrimaryGuild(
151+
data=primary_guild_payload, state=self._state
152+
)
153+
else:
154+
self.primary_guild = None
145155
self._public_flags = data.get("public_flags", 0)
146156
self.bot = data.get("bot", False)
147157
self.system = data.get("system", False)
@@ -161,6 +171,7 @@ def _copy(cls: type[BU], user: BU) -> BU:
161171
self.bot = user.bot
162172
self._state = user._state
163173
self._public_flags = user._public_flags
174+
self.primary_guild = user.primary_guild
164175

165176
return self
166177

0 commit comments

Comments
 (0)