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

pincer.objects.guild.permissions   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 84
dl 0
loc 104
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Permission.__init__() 0 8 3
A Permission.__setattr__() 0 4 3
A Permission.from_int() 0 13 4
A Permission.__eq__() 0 10 3
A Permission.to_int() 0 10 4
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 enum import Enum
7
from typing import Tuple, Optional
8
9
10
class PermissionEnums(Enum):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
11
    CREATE_INSTANT_INVITE = 1 << 0
12
    KICK_MEMBERS = 1 << 1
13
    BAN_MEMBERS = 1 << 2
14
    ADMINISTRATOR = 1 << 3
15
    MANAGE_CHANNELS = 1 << 4
16
    MANAGE_GUIlD = 1 << 5
17
    ADD_REACTIONS = 1 << 6
18
    VIEW_AUDIT_LOG = 1 << 7
19
    PRIORITY_SPEAKER = 1 << 8
20
    STREAM = 1 << 9
21
    VIEW_CHANNEL = 1 << 10
22
    SEND_MESSAGES = 1 << 11
23
    SEND_TTS_MESSAGES = 1 << 12
24
    MANAGE_MESSAGES = 1 << 13
25
    EMBED_LINKS = 1 << 14
26
    ATTACH_FILES = 1 << 15
27
    READ_MESSAGE_HISTORY = 1 << 16
28
    MENTION_EVERYONE = 1 << 17
29
    USE_EXTERNAL_EMOJIS = 1 << 18
30
    VIEW_GUILD_INSIGHTS = 1 << 19
31
    CONNECT = 1 << 20
32
    SPEAK = 1 << 21
33
    MUTE_MEMBERS = 1 << 22
34
    DEAFEN_MEMBERS = 1 << 23
35
    MOVE_MEMBERS = 1 << 24
36
    USE_VAD = 1 << 25
37
    CHANGE_NICKNAME = 1 << 26
38
    MANAGE_NICKNAMES = 1 << 27
39
    MANAGE_ROLES = 1 << 28
40
    MANAGE_WEBHOOKS = 1 << 29
41
    MANAGE_EMOJIS_AND_STICKERS = 1 << 30
42
    USE_APPLICATION_COMMANDS = 1 << 31
43
    REQUEST_TO_SPEAK = 1 << 32
44
    MANAGE_EVENTS = 1 << 33
45
    MANAGE_THREADS = 1 << 34
46
    CREATE_PUBLIC_THREADS = 1 << 35
47
    CREATE_PRIVATE_THREADS = 1 << 36
48
    USE_EXTERNAL_STICKERS = 1 << 37
49
    SEND_MESSAGES_IN_THREADS = 1 << 38
50
    START_EMBEDDED_ACTIVITIES = 1 << 39
51
    MODERATE_MEMBERS = 1 << 40
52
53
54
class Permission:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
55
    def __init__(self, **kwargs: Optional[bool]) -> None:
56
        valid_perms = (enum.name.lower() for enum in PermissionEnums)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable enum does not seem to be defined.
Loading history...
57
        for perm in valid_perms:
58
            setattr(self, perm, kwargs.pop(perm, None))
59
60
        if kwargs:
61
            invalid_perms = ', '.join(kwargs.keys())
62
            raise ValueError(f"Invalid permissions were passed in: {invalid_perms}")
63
64
    def __setattr__(self, name: str, value: Optional[bool]) -> None:
65
        if not (value is None or isinstance(value, bool)):
66
            raise ValueError(f"Permission {name!r} must be a boolean or None")
67
        return super().__setattr__(name, value)
68
69
    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...
70
        """
71
        Permission equality is determined by comparing the integer values of the permissions
72
        """
73
        if isinstance(object, Permission):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
74
            return self.to_int() == object.to_int()
75
        elif isinstance(object, tuple):
76
            return self.to_int() == object
77
78
        return False
79
80
    @classmethod
81
    def from_int(cls, allow: int, deny: int) -> Permission:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
82
        clsobj = cls()
83
84
        for enum in PermissionEnums:
85
            if bool(enum.value & allow):
86
                setattr(clsobj, enum.name.lower(), True)
87
            elif bool(enum.value & deny):
88
                setattr(clsobj, enum.name.lower(), False)
89
            else:
90
                setattr(clsobj, enum.name.lower(), None)
91
92
        return clsobj
93
94
    def to_int(self) -> Tuple[int]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
95
        allow = 0
96
        deny = 0
97
        for enum in PermissionEnums:
98
            if getattr(self, enum.name.lower()):
99
                allow |= enum.value
100
            elif getattr(self, enum.name.lower()) is False:
101
                deny |= enum.value
102
103
        return allow, deny
104