Interactable.unassign()   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
rs 7.3333
c 0
b 0
f 0
cc 8
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 collections import ChainMap
7
8
from .. import client as _client
9
from .chat_command_handler import ChatCommandHandler
10
from .components.component_handler import ComponentHandler
11
from ..objects.app.command import AppCommand, InteractableStructure
12
13
14
INTERACTION_REGISTERS = ChainMap(
15
    ChatCommandHandler.register, ComponentHandler.register
16
)
17
18
19
class Interactable:
20
    """
21
    Class that can register :class:`~pincer.commands.interactable.PartialInteractable`
22
    objects. Any class that subclasses this class can register Application Commands and
23
    Message Components.
24
    PartialInteractable objects are registered by running the register function and
25
    setting an attribute of the client to the result.
26
    """
27
28
    def __init__(self):
29
        for value in vars(type(self)).values():
30
            if isinstance(value, InteractableStructure):
31
                value.manager = self
32
33
    def __del__(self):
34
        self.unassign()
35
36
    def unassign(self):
37
        """
38
        Removes this objects loaded commands from ChatCommandHandler and
39
        ComponentHandler and removes loaded events from the client.
40
        """
41
        for value in vars(type(self)).values():
42
            if isinstance(value, InteractableStructure) and isinstance(
43
                value.metadata, AppCommand
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
44
            ):
45
                for key, _value in INTERACTION_REGISTERS.items():
46
                    if value is _value:
47
                        INTERACTION_REGISTERS.pop(key)
48
49
                key = value.call.__name__.lower()
50
51
                event_or_list = _client._events.get(key)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _events was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
52
                if isinstance(event_or_list, list):
53
                    if value in event_or_list:
54
                        event_or_list.remove(value)
55
                else:
56
                    _client._events.pop(key, None)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _events was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
57