aboutsummaryrefslogtreecommitdiff
path: root/discord/role.py
blob: 490df159bb52558fe0073b367db4d255d8130ca4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# -*- coding: utf-8 -*-

"""
The MIT License (MIT)

Copyright (c) 2015 Rapptz

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

from .permissions import Permissions

class Colour(object):
    """Represents a Discord role colour. This class is similar
    to an (red, green, blue) tuple.

    There is an alias for this called Color.

    Supported operations:

    +-----------+--------------------------------------+
    | Operation |             Description              |
    +===========+======================================+
    | x == y    | Checks if two colours are equal.     |
    +-----------+--------------------------------------+
    | x != y    | Checks if two colours are not equal. |
    +-----------+--------------------------------------+

    Instance attributes:

    .. attribute:: value

        The raw integer colour value.
    """

    def __init__(self, value):
        self.value = value

    def _get_byte(self, byte):
        return (self.value >> (8 * byte)) & 0xff

    def __eq__(self, other):
        return self.value == getattr(other, 'value', None)

    def __ne__(self, other):
        return isinstance(other, Colour) and self.value != other.value

    @property
    def r(self):
        """Returns the red component of the colour."""
        return self._get_byte(2)

    @property
    def g(self):
        """Returns the green component of the colour."""
        return self._get_byte(1)

    @property
    def b(self):
        """Returns the blue component of the colour."""
        return self._get_byte(0)

    def to_tuple(self):
        """Returns an (r, g, b) tuple representing the colour."""
        return (self.r, self.g, self.b)

    @classmethod
    def default(cls):
        """A factory method that returns a :class:`Colour` with a value of 0."""
        return cls(0)

    @classmethod
    def cyan(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x1abc9c."""
        return cls(0x1abc9c)

    @classmethod
    def green(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x2ecc71."""
        return cls(0x2ecc71)

    @classmethod
    def blue(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x3498db."""
        return cls(0x3498db)

    @classmethod
    def purple(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x9b59b6."""
        return cls(0x9b59b6)

    @classmethod
    def yellow(cls):
        """A factory method that returns a :class:`Colour` with a value of 0xf1c40f."""
        return cls(0xf1c40f)

    @classmethod
    def orange(cls):
        """A factory method that returns a :class:`Colour` with a value of 0xe67e22."""
        return cls(0xe67e22)

    @classmethod
    def red(cls):
        """A factory method that returns a :class:`Colour` with a value of 0xe74c3c."""
        return cls(0xe74c3c)

    @classmethod
    def grey(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x95a5a6."""
        return cls(0x95a5a6)

    @classmethod
    def dark_grey(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x7f8c8d."""
        return cls(0x7f8c8d)

    @classmethod
    def navy_blue(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x34495e."""
        return cls(0x34495e)

    @classmethod
    def dark_cyan(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x11806a."""
        return cls(0x11806a)

    @classmethod
    def dark_green(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x1f8b4c."""
        return cls(0x1f8b4c)

    @classmethod
    def dark_blue(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x206694."""
        return cls(0x206694)

    @classmethod
    def dark_purple(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x71368a."""
        return cls(0x71368a)

    @classmethod
    def strong_orange(cls):
        """A factory method that returns a :class:`Colour` with a value of 0xc27c0e."""
        return cls(0xc27c0e)

    @classmethod
    def dark_orange(cls):
        """A factory method that returns a :class:`Colour` with a value of 0xa84300."""
        return cls(0xa84300)

    @classmethod
    def dark_red(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x992d22."""
        return cls(0x992d22)

    @classmethod
    def dark_grey_blue(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x979c9f."""
        return cls(0x979c9f)

    @classmethod
    def light_grey(cls):
        """A factory method that returns a :class:`Colour` with a value of 0xbcc0c0."""
        return cls(0xbcc0c0)

    @classmethod
    def dark_navy_blue(cls):
        """A factory method that returns a :class:`Colour` with a value of 0x2c3e50."""
        return cls(0x2c3e50)


Color = Colour

class Role(object):
    """Represents a Discord role in a :class:`Server`.

    Instance attributes:

    .. attribute:: id

        The ID for the role.
    .. attribute:: name

        The name of the role.
    .. attribute:: permissions

        A :class:`Permissions` that represents the role's permissions.
    .. attribute:: color
                   colour

        A :class:`Colour` representing the role colour.
    .. attribute:: hoist

        A boolean representing if the role will be displayed separately from other members.
    .. attribute:: position

        The position of the role. This number is usually positive.
    .. attribute:: managed

        A boolean indicating if the role is managed by the server through some form of integration
        such as Twitch.
    """

    def __init__(self, **kwargs):
        self.update(**kwargs)

    def update(self, **kwargs):
        self.id = kwargs.get('id')
        self.name = kwargs.get('name')
        self.permissions = Permissions(kwargs.get('permissions', 0))
        self.position = kwargs.get('position', 0)
        self.colour = Colour(kwargs.get('color', 0))
        self.hoist = kwargs.get('hoist', False)
        self.managed = kwargs.get('managed', False)
        self.color = self.colour
        self._is_everyone = kwargs.get('everyone', False)

    def is_everyone(self):
        """Checks if the role is the @everyone role."""
        return self.position == -1