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