Passed
Push — main ( 8eecb9...04a768 )
by
unknown
02:39 queued 12s
created

ThrottleInterface.handle()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nop 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
from abc import ABC, abstractmethod
4
from enum import Enum, auto
5
from typing import Dict, Any, Optional
6
7
from .context import Context
8
from ..utils.slidingwindow import SlidingWindow
9
10
11
class ThrottleScope(Enum):
12
    """
13
    On what the cooldown should be set/on what should the cooldown be
14
    set.
15
16
    :param GUILD:
17
        The cooldown is per guild.
18
19
    :param CHANNEL:
20
        The cooldown is per channel.
21
22
    :param USER:
23
        The cooldown is per user.
24
25
    :param GLOBAL:
26
        The cooldown is global.
27
    """
28
    GUILD = auto()
29
    CHANNEL = auto()
30
    USER = auto()
31
    GLOBAL = auto()
32
33
34
class ThrottleInterface(ABC):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
35
    @abstractmethod
36
    def __init__(self, client, **kwargs):
37
        """
38
        Initializes a throttler.
39
40
        :param client:
41
            The current connected client, aka your bot.
42
43
        :param kwargs:
44
            Optional extra arguments.
45
        """
46
        self.client = client
47
48
    @abstractmethod
49
    async def handle(self, ctx: Context, **kwargs):
50
        """
51
        Handles a context. This method is executed before the command is.
52
53
        :param ctx:
54
            The context of the command.
55
56
        :param kwargs:
57
            The extra kwargs passed for the cooldown.
58
        """
59
        raise NotImplementedError
60
61
62
class DefaultThrottleHandler(ThrottleInterface, ABC):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
63
    default_throttle_type = ThrottleScope.GLOBAL
64
    throttle: Dict[Any, SlidingWindow] = {}
65
66
    __throttle_scopes = {
67
        ThrottleScope.GLOBAL: None,
68
        ThrottleScope.GUILD: "guild.id",
69
        ThrottleScope.CHANNEL: "channel.id",
70
        ThrottleScope.USER: "author.id"
71
    }
72
73
    def get_key_from_scope(
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
74
            self,
75
            ctx: Context,
0 ignored issues
show
Unused Code introduced by
The argument ctx seems to be unused.
Loading history...
76
            scope: ThrottleScope
0 ignored issues
show
Unused Code introduced by
The argument scope seems to be unused.
Loading history...
77
    ) -> Optional[int]:
78
        """
79
        Retrieve the the appropriate key from the context through the
80
        throttle scope.
81
        """
82
        ...
83
84
    async def handle(self, ctx: Context, **kwargs):
85
        ...
86