GuildMember.__post_init__()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
from __future__ import annotations
5
6
from dataclasses import dataclass
7
from typing import TYPE_CHECKING
8
9
from ..user.user import User
10
from ...utils.api_object import APIObject
11
from ...utils.snowflake import Snowflake
12
from ...utils.timestamp import Timestamp
13
from ...utils.types import MISSING
14
15
if TYPE_CHECKING:
16
    from typing import List, Optional
17
18
    from ...client import Client
19
    from ...utils.types import APINullable
20
21
22
@dataclass(repr=False)
23
class BaseMember(APIObject):
24
    """Represents the base of a guild member.
25
26
    Attributes
27
    ----------
28
    deaf: :class:`bool`
29
        Whether the user is deafened in voice channels
30
    joined_at: :class:`~pincer.utils.timestamp.Timestamp`
31
        Then the user joined the guild
32
    mute: :class:`bool`
33
        Whether the user is muted in voice channels
34
    roles: List[:class:`~pincer.utils.snowflake.Snowflake`]
35
        Array of role object ids
36
    hoisted_role: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
37
        The user's top role in the guild.
38
    """
39
40
    joined_at: APINullable[Timestamp] = MISSING
41
    roles: APINullable[List[Snowflake]] = MISSING
42
    deaf: bool = MISSING
43
    mute: bool = MISSING
44
45
    hoisted_role: APINullable[Snowflake] = MISSING
46
47
48
@dataclass(repr=False)
49
class PartialGuildMember(APIObject):
50
    """Represents a partial guild member.
51
    This is a reference to a member from a guild which does not contain
52
    all information.
53
54
    This gets used in form example message mentions.
55
56
    Attributes
57
    ----------
58
    id: :class:`~pincer.utils.snowflake.Snowflake`
59
        The user's id
60
    username: :class:`str`
61
        The user's username, not unique across the platform
62
    discriminator: :class:`str`
63
        The user's 4-digit discord-tag
64
    avatar: :class:`str`
65
        The user's avatar hash
66
    public_flags: :class:`int`
67
        The flags on a user's account
68
    member: :class:`~pincer.objects.guild.member.BaseMember`
69
        The user their (partial) guild information.
70
    """
71
72
    id: Snowflake
73
    username: str
74
    discriminator: str
75
    avatar: str
76
    public_flags: int
77
    member: Optional[BaseMember]
78
79
80
@dataclass(repr=False)
81
class GuildMember(BaseMember, User):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (8/7)
Loading history...
82
    """Represents a member which resides in a guild/server.
83
84
    Attributes
85
    ----------
86
    nick: APINullable[Optional[:class:`str`]]
87
        This users guild nickname
88
    pending: APINullable[:class:`bool`]
89
        Whether the user has not yet passed the guild's Membership
90
        Screening requirements
91
    is_pending: APINullable[:class:`bool`]
92
        Deprecated version of pending.
93
    permissions: APINullable[:class:`str`]
94
        Total permissions of the member in the channel,
95
        including overwrites, returned when in the interaction object
96
    premium_since: APINullable[Optional[:class:`~pincer.utils.timestamp.Timestamp`]]
97
        When the user started boosting the guild
98
    user: APINullable[:class:`~pincer.objects.user.user.User`]
99
        The user this guild member represents
100
    avatar: APINullable[:class:`str`]
101
        The user's avatar.
102
    communication_disabled_until: APINullable[Optional[:class:`~pincer.utils.timestamp.Timestamp`]]
103
        The timestamp at which the user is no longer timed out.
104
105
        .. note::
106
107
            This may be in the past if the user has been timed out recently.
108
109
    """  # noqa: E501
110
111
    nick: APINullable[Optional[str]] = MISSING
112
    pending: APINullable[bool] = MISSING
113
    is_pending: APINullable[bool] = MISSING
114
    permissions: APINullable[str] = MISSING
115
    premium_since: APINullable[Optional[Timestamp]] = MISSING
116
    user: APINullable[User] = MISSING
117
    avatar: APINullable[str] = MISSING
118
    communication_disabled_until: APINullable[Optional[Timestamp]] = MISSING
119
120
    def __post_init__(self):
121
        super().__post_init__()
122
123
        if self.user is not MISSING:
124
            self.set_user_data(self.user)
125
126
    def set_user_data(self, user: User):
127
        """
128
        Used to set the user parameters of a GuildMember instance
129
130
        user: APINullable[:class:`~pincer.objects.user.user.User`]
131
            A user class to copy the fields from
132
        """
133
134
        # Inspired from this thread
135
        # https://stackoverflow.com/questions/57962873/easiest-way-to-copy-all-fields-from-one-dataclass-instance-to-another
136
137
        for key, value in user.__dict__.items():
138
            setattr(self, key, value)
139
140
        self.user = MISSING
141
142
    @classmethod
143
    async def from_id(
0 ignored issues
show
Bug introduced by
Parameters differ from overridden 'from_id' method
Loading history...
144
        cls, client: Client, guild_id: int, user_id: int
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
145
    ) -> GuildMember:
146
        data = await client.http.get(f"guilds/{guild_id}/members/{user_id}")
147
        return cls.from_dict(data)
148