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
|
|
|
from typing import List |
8
|
|
|
|
9
|
|
|
from .. import client as _client |
10
|
|
|
from .chat_command_handler import ChatCommandHandler, _hash_interactable_structure |
|
|
|
|
11
|
|
|
from .components.component_handler import ComponentHandler |
12
|
|
|
from ..objects.app.command import AppCommand, InteractableStructure |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
INTERACTION_REGISTERS = ChainMap(ChatCommandHandler.register, ComponentHandler.register) |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class Interactable: |
19
|
|
|
""" |
20
|
|
|
Class that can register :class:`~pincer.commands.interactable.PartialInteractable` |
21
|
|
|
objects. Any class that subclasses this class can register Application Commands and |
22
|
|
|
Message Components. |
23
|
|
|
PartialInteractable objects are registered by running the register function and |
24
|
|
|
setting an attribute of the client to the result. |
25
|
|
|
""" |
26
|
|
|
|
27
|
|
|
def __init__(self): |
28
|
|
|
for value in vars(type(self)).values(): |
29
|
|
|
if isinstance(value, InteractableStructure): |
30
|
|
|
value.manager = self |
31
|
|
|
|
32
|
|
|
def __del__(self): |
33
|
|
|
self.unassign() |
34
|
|
|
|
35
|
|
|
def unassign(self): |
|
|
|
|
36
|
|
|
for value in vars(type(self)).values(): |
37
|
|
|
if isinstance(value, InteractableStructure): |
38
|
|
|
if isinstance(value.metadata, AppCommand): |
39
|
|
|
for key, _value in INTERACTION_REGISTERS.items(): |
40
|
|
|
if value is _value: |
41
|
|
|
INTERACTION_REGISTERS.pop(key) |
42
|
|
|
|
43
|
|
|
key = value.call.__name__.lower() |
44
|
|
|
|
45
|
|
|
event_or_list = _client._events.get(key) |
|
|
|
|
46
|
|
|
if isinstance(event_or_list, List): |
|
|
|
|
47
|
|
|
if value in event_or_list: |
48
|
|
|
event_or_list.remove(value) |
49
|
|
|
else: |
50
|
|
|
_client._events.pop(key, None) |
|
|
|
|
51
|
|
|
|