|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2015-present Rapptz |
| 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 Optional, TYPE_CHECKING |
| 28 | +from datetime import datetime |
| 29 | + |
| 30 | +from .asset import Asset |
| 31 | +from .utils import snowflake_time, _get_as_snowflake |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from .state import ConnectionState |
| 35 | + from .types.user import PrimaryGuild as PrimaryGuildPayload |
| 36 | + from typing_extensions import Self |
| 37 | + |
| 38 | + |
| 39 | +class PrimaryGuild: |
| 40 | + """Represents the primary guild identity of a :class:`User` |
| 41 | +
|
| 42 | + .. versionadded:: 2.6 |
| 43 | +
|
| 44 | + Attributes |
| 45 | + ----------- |
| 46 | + id: Optional[:class:`int`] |
| 47 | + The ID of the user's primary guild, if any. |
| 48 | + tag: Optional[:class:`str`] |
| 49 | + The primary guild's tag. |
| 50 | + identity_enabled: Optional[:class:`bool`] |
| 51 | + Whether the user has their primary guild publicly displayed. If ``None``, the user has a public guild but has not reaffirmed the guild identity after a change |
| 52 | +
|
| 53 | + .. warning:: |
| 54 | +
|
| 55 | + Users can have their primary guild publicly displayed while still having an :attr:`id` of ``None``. Be careful when checking this attribute! |
| 56 | + """ |
| 57 | + |
| 58 | + __slots__ = ('id', 'identity_enabled', 'tag', '_badge', '_state') |
| 59 | + |
| 60 | + def __init__(self, *, state: ConnectionState, data: PrimaryGuildPayload) -> None: |
| 61 | + self._state = state |
| 62 | + self._update(data) |
| 63 | + |
| 64 | + def _update(self, data: PrimaryGuildPayload): |
| 65 | + self.id = _get_as_snowflake(data, 'identity_guild_id') |
| 66 | + self.identity_enabled = data['identity_enabled'] |
| 67 | + self.tag = data.get('tag', None) |
| 68 | + self._badge = data.get('badge') |
| 69 | + |
| 70 | + @property |
| 71 | + def badge(self) -> Optional[Asset]: |
| 72 | + """Optional[:class:`Asset`]: Returns the primary guild's asset""" |
| 73 | + if self._badge is not None and self.id is not None: |
| 74 | + return Asset._from_primary_guild(self._state, self.id, self._badge) |
| 75 | + return None |
| 76 | + |
| 77 | + @property |
| 78 | + def created_at(self) -> Optional[datetime]: |
| 79 | + """Optional[:class:`datetime.datetime`]: Returns the primary guild's creation time in UTC.""" |
| 80 | + if self.id is not None: |
| 81 | + return snowflake_time(self.id) |
| 82 | + return None |
| 83 | + |
| 84 | + @classmethod |
| 85 | + def _default(cls, state: ConnectionState) -> Self: |
| 86 | + payload: PrimaryGuildPayload = {"identity_enabled": False} # type: ignore |
| 87 | + return cls(state=state, data=payload) |
| 88 | + |
| 89 | + def __repr__(self) -> str: |
| 90 | + return f'<PrimaryGuild id={self.id} identity_enabled={self.identity_enabled} tag={self.tag!r}>' |
0 commit comments