Passed
Pull Request — main (#378)
by
unknown
01:52
created

Permission.__init__()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nop 2
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 enum import Enum
8
from typing import Tuple, Optional
9
10
11
class Permissions(Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
12
    CREATE_INSTANT_INVITE = 1 << 0
13
    KICK_MEMBERS = 1 << 1
14
    BAN_MEMBERS = 1 << 2
15
    ADMINISTRATOR = 1 << 3
16
    MANAGE_CHANNELS = 1 << 4
17
    MANAGE_GUIlD = 1 << 5
18
    ADD_REACTIONS = 1 << 6
19
    VIEW_AUDIT_LOG = 1 << 7
20
    PRIORITY_SPEAKER = 1 << 8
21
    STREAM = 1 << 9
22
    VIEW_CHANNEL = 1 << 10
23
    SEND_MESSAGES = 1 << 11
24
    SEND_TTS_MESSAGES = 1 << 12
25
    MANAGE_MESSAGES = 1 << 13
26
    EMBED_LINKS = 1 << 14
27
    ATTACH_FILES = 1 << 15
28
    READ_MESSAGE_HISTORY = 1 << 16
29
    MENTION_EVERYONE = 1 << 17
30
    USE_EXTERNAL_EMOJIS = 1 << 18
31
    VIEW_GUILD_INSIGHTS = 1 << 19
32
    CONNECT = 1 << 20
33
    SPEAK = 1 << 21
34
    MUTE_MEMBERS = 1 << 22
35
    DEAFEN_MEMBERS = 1 << 23
36
    MOVE_MEMBERS = 1 << 24
37
    USE_VAD = 1 << 25
38
    CHANGE_NICKNAME = 1 << 26
39
    MANAGE_NICKNAMES = 1 << 27
40
    MANAGE_ROLES = 1 << 28
41
    MANAGE_WEBHOOKS = 1 << 29
42
    MANAGE_EMOJIS_AND_STICKERS = 1 << 30
43
    USE_APPLICATION_COMMANDS = 1 << 31
44
    REQUEST_TO_SPEAK = 1 << 32
45
    MANAGE_EVENTS = 1 << 33
46
    MANAGE_THREADS = 1 << 34
47
    CREATE_PUBLIC_THREADS = 1 << 35
48
    CREATE_PRIVATE_THREADS = 1 << 36
49
    USE_EXTERNAL_STICKERS = 1 << 37
50
    SEND_MESSAGES_IN_THREADS = 1 << 38
51
    START_EMBEDDED_ACTIVITIES = 1 << 39
52
    MODERATE_MEMBERS = 1 << 40
53
54
55
@dataclass(kw_only=True)
0 ignored issues
show
Bug introduced by
The keyword kw_only does not seem to exist for the function call.
Loading history...
56
class Permission:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
best-practice introduced by
Too many instance attributes (41/7)
Loading history...
57
    create_instant_invite: Optional[bool] = None
58
    kick_members: Optional[bool] = None
59
    ban_members: Optional[bool] = None
60
    administrator: Optional[bool] = None
61
    manage_channels: Optional[bool] = None
62
    manage_guild: Optional[bool] = None
63
    add_reactions: Optional[bool] = None
64
    view_audit_log: Optional[bool] = None
65
    priority_speaker: Optional[bool] = None
66
    stream: Optional[bool] = None
67
    view_channel: Optional[bool] = None
68
    send_messages: Optional[bool] = None
69
    send_tts_messages: Optional[bool] = None
70
    manage_messages: Optional[bool] = None
71
    embed_links: Optional[bool] = None
72
    attach_files: Optional[bool] = None
73
    read_message_history: Optional[bool] = None
74
    mention_everyone: Optional[bool] = None
75
    use_external_emojis: Optional[bool] = None
76
    view_guild_insights: Optional[bool] = None
77
    connect: Optional[bool] = None
78
    speak: Optional[bool] = None
79
    mute_members: Optional[bool] = None
80
    deafen_members: Optional[bool] = None
81
    move_members: Optional[bool] = None
82
    use_vad: Optional[bool] = None
83
    change_nickname: Optional[bool] = None
84
    manage_nicknames: Optional[bool] = None
85
    manage_roles: Optional[bool] = None
86
    manage_webhooks: Optional[bool] = None
87
    manage_emojis_and_stickers: Optional[bool] = None
88
    use_application_commands: Optional[bool] = None
89
    request_to_speak: Optional[bool] = None
90
    manage_events: Optional[bool] = None
91
    manage_threads: Optional[bool] = None
92
    create_public_threads: Optional[bool] = None
93
    create_private_threads: Optional[bool] = None
94
    use_external_stickers: Optional[bool] = None
95
    send_messages_in_threads: Optional[bool] = None
96
    start_embedded_activities: Optional[bool] = None
97
    moderate_members: Optional[bool] = None
98
99
    def __setattr__(self, name: str, value: Optional[bool]) -> None:
100
        if not isinstance(value, bool) and value is not None:
101
            raise ValueError(f"Permission {name!r} must be a boolean or None")
102
        return super().__setattr__(name, value)
103
104
    def __eq__(self, object) -> bool:
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in object.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
105
        """
106
        Permission equality is determined by comparing the integer values of the permissions
107
        """
108
        if isinstance(object, Permission):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
109
            return self.to_int() == object.to_int()
110
        elif isinstance(object, tuple):
111
            return self.to_int() == object
112
113
        return False
114
115
    @classmethod
116
    def from_int(cls, allow: int, deny: int) -> Permission:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
117
        clsobj = cls()
118
119
        for enum in Permissions:
120
            if bool(enum.value & allow):
121
                setattr(clsobj, enum.name.lower(), True)
122
            elif bool(enum.value & deny):
123
                setattr(clsobj, enum.name.lower(), False)
124
            else:
125
                setattr(clsobj, enum.name.lower(), None)
126
127
        return clsobj
128
129
    def to_int(self) -> Tuple[int]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
130
        allow = 0
131
        deny = 0
132
        for enum in Permissions:
133
            if getattr(self, enum.name.lower()):
134
                allow |= enum.value
135
            elif getattr(self, enum.name.lower()) is False:
136
                deny |= enum.value
137
138
        return allow, deny
139
140
    @property
141
    def allow(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
142
        allow = 0
143
        for enum in Permissions:
144
            if getattr(self, enum.name.lower()):
145
                allow |= enum.value
146
147
        return allow
148
149
    @property
150
    def deny(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
151
        deny = 0
152
        for enum in Permissions:
153
            if getattr(self, enum.name.lower()) is False:
154
                deny |= enum.value
155
156
        return deny
157