1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
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 |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
57
|
|
|
|