Passed
Pull Request — main (#226)
by
unknown
01:44
created

pincer.objects.app.command   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 69
dl 0
loc 191
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A AppCommand.__eq__() 0 7 2
A AppCommand.__post_init__() 0 4 2
A AppCommand.__hash__() 0 2 1
A AppCommand.add_option() 0 12 2
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 dataclasses import dataclass
7
from typing import List, Union, TYPE_CHECKING
8
9
from .command_types import AppCommandOptionType, AppCommandType
10
from ...utils.api_object import APIObject
0 ignored issues
show
Bug introduced by
The name api_object does not seem to exist in module pincer.utils.
Loading history...
introduced by
Cannot import 'utils.api_object' due to syntax error 'invalid syntax (<unknown>, line 80)'
Loading history...
11
from ...utils.snowflake import Snowflake
12
from ...utils.types import Coro, choice_value_types
13
from ...utils.types import MISSING
14
15
if TYPE_CHECKING:
16
    from ...utils.types import APINullable
17
    from ..app.throttle_scope import ThrottleScope
18
19
20
@dataclass
21
class AppCommandInteractionDataOption(APIObject):
22
    """Represents a Discord Application Command Interaction Data Option
23
24
    Attributes
25
    ----------
26
    name: :class:`str`
27
        The name of the parameter
28
    value: :class:`str`
29
        The value of the pair
30
    type: APINullable[:class:`str`]
31
        Value of application command option type
32
    options: APINullable[List[:data:`~pincer.objects.app.command.AppCommandInteractionDataOption`]]
33
        Present if this option is a group or subcommand
34
    """
35
    # noqa: E501
36
    name: str
37
    value: APINullable[str] = MISSING
38
    type: APINullable[AppCommandOptionType] = MISSING
39
    options: APINullable[
40
        List[AppCommandInteractionDataOption]] = MISSING
41
42
43
@dataclass
44
class AppCommandOptionChoice(APIObject):
45
    """Represents a Discord Application Command Option Choice object
46
47
    Attributes
48
    ----------
49
    name: :class:`str`
50
        1-100 character choice name
51
    value: Union[:data:`~pincer.utils.types.choice_value_types`]
52
        Value of the choice, up to 100 characters if string
53
    """
54
    name: str
55
    value: choice_value_types
56
57
58
@dataclass
59
class AppCommandOption(APIObject):
60
    """Represents a Discord Application Command Option object
61
62
    Attributes
63
    ----------
64
    type: :class:`~pincer.objects.AppCommandOptionType`
65
        The type of option
66
    name: :class:`str`
67
        1-32 lowercase character name matching `^[\\w-]{1,32}$`
68
    description: :class:`str`
69
        1-100 character description
70
    required: APINullable[:class:`bool`]
71
        If the parameter is required or optional |default| :data:`False`
72
    choices: APINullable[List[:class:`~pincer.objects.app.command.AppCommandOptionChoice`]]
73
        Choices for `STRING`, `INTEGER`, and `NUMBER`
74
        types for the user to pick from, max 25
75
    options: APINullable[List[:class:`~pincer.objects.app.command.AppCommandOptionChoice`]]
76
        If the option is a subcommand or subcommand group type,
77
        this nested options will be the parameters
78
    """
79
    # noqa: E501
80
    type: AppCommandOptionType
81
    name: str
82
    description: str
83
84
    required: APINullable[bool] = False
85
    choices: APINullable[List[AppCommandOptionChoice]] = MISSING
86
    options: APINullable[List[AppCommandOption]] = MISSING
87
88
89
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (12/7)
Loading history...
90
class AppCommand(APIObject):
91
    """Represents a Discord Application Command object
92
93
    Attributes
94
    ----------
95
    type: :class:`~pincer.objects.app.command.AppCommandType`
96
        The type of command, defaults ``1`` if not set
97
    name: :class:`str`
98
        1-32 character name
99
    description: :class:`str`
100
        1-100 character description for ``CHAT_INPUT`` commands,
101
        empty string for ``USER`` and ``MESSAGE`` commands
102
    id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
103
        Unique id of the command
104
    version: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
105
        Auto-incrementing version identifier updated during substantial
106
        record changes
107
    application_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
108
        Unique id of the parent application
109
    options: APINullable[List[:class:`~pincer.objects.app.command.AppCommandOption`]]
110
        The parameters for the command, max 25
111
    guild_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
112
        Guild id of the command, if not global
113
    default_permission: APINullable[:class:`bool`]
114
        Whether the command is enabled by default
115
        when the app is added to a guild
116
    """
117
    # noqa: E501
118
    type: AppCommandType
119
    name: str
120
    description: str
121
122
    id: APINullable[Snowflake] = MISSING
123
    version: APINullable[Snowflake] = MISSING
124
    application_id: APINullable[Snowflake] = MISSING
125
    options: APINullable[List[AppCommandOption]] = MISSING
126
    guild_id: APINullable[Snowflake] = MISSING
127
    default_permission: APINullable[bool] = True
128
    default_member_permissions: APINullable[None] = None
129
    dm_permission: APINullable[None] = None
130
131
    _eq_props = [
132
        "type", "name", "description", "guild_id", "default_permission",
133
        "options"
134
    ]
135
136
    def __post_init__(self):
137
        super().__post_init__()
138
139
        self.options = [] if self.options is MISSING else self.options
140
141
    def __eq__(self, other: Union[AppCommand, ClientCommandStructure]):
142
        if isinstance(other, ClientCommandStructure):
143
            other = other.app
144
145
        return all(
146
            self.__getattribute__(prop) == other.__getattribute__(prop)
147
            for prop in self._eq_props
148
        )
149
150
    def __hash__(self):
151
        return hash((self.id, self.name, self.description, self.guild_id))
152
153
    def add_option(self, option: AppCommandOption):
154
        """Add a new option field to the current application command.
155
156
        Parameters
157
        ----------
158
        option : :class:`~pincer.objects.app.command.AppCommandOption`
159
            The option which will be appended.
160
        """
161
        if self.options:
162
            self.options.append(option)
163
        else:
164
            self.options = [option]
165
166
167
@dataclass
168
class ClientCommandStructure:
169
    """Represents the structure of how the client saves the existing
170
    commands in the register.
171
172
    Attributes
173
    ----------
174
    app: :class:`~pincer.objects.app.command.AppCommand`
175
        The command application.
176
    call: :class:`~pincer.utils.types.Coro`
177
        The coroutine which should be called when the command gets
178
        executed.
179
    cooldown: :class:`int`
180
        Amount of times for cooldown
181
    cooldown_scale: :class:`float`
182
        Search time for cooldown
183
    cooldown_scope: :class:`~pincer.objects.app.throttle_scope.ThrottleScope`
184
        The type of cooldown
185
    """
186
    app: AppCommand
187
    call: Coro
188
    cooldown: int
189
    cooldown_scale: float
190
    cooldown_scope: ThrottleScope
191