Passed
Pull Request — main (#389)
by
unknown
01:49
created

pincer.commands.interactable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 22
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A PartialInteractable.__call__() 0 2 1
A PartialInteractable.register() 0 3 1
A PartialInteractable.__init__() 0 4 1
A Interactable.__init__() 0 4 3
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 abc import ABC, abstractmethod
7
from typing import TYPE_CHECKING, Type, TypeVar
8
9
if TYPE_CHECKING:
10
    from typing import Any, Awaitable, Callable
11
12
T = TypeVar("T")
13
14
15
class PartialInteractable(ABC):
16
    """
17
    Represents a command or message component to be registered to a class.
18
19
    Parameters
20
    ----------
21
    func : Callable[..., Awaitable[Any]]
22
        The function to run for this interaction.
23
    args : Any
24
        Args to be stored to used in register.
25
    kwargs : Any
26
        Kwargs to store to be used in register.
27
    """
28
29
    def __init__(self, func: Callable[..., Awaitable[Any]], *args: Any, **kwargs: Any):
30
        self.func = func
31
        self.args = args
32
        self.kwargs = kwargs
33
34
    def __call__(self, *args: Any, **kwds: Any) -> Any:
35
        return self.func(*args, **kwds)
36
37
    @abstractmethod
38
    def register(self, manager: Any) -> Type[T]:
39
        """Registers a command to a command handler to be called later"""
40
41
42
class Interactable:
43
    """
44
    Class that can register :class:`~pincer.commands.interactable.PartialInteractable`
45
    objects. Any class that subclasses this class can register Application Commands and
46
    Message Components.
47
    PartialInteractable objects are registered by running the register function and
48
    setting an attribute of the client to the result.
49
    """
50
51
    def __init__(self):
52
        for key, value in vars(type(self)).items():
53
            if isinstance(value, PartialInteractable):
54
                setattr(self, key, value.register(self))
55