Passed
Pull Request — main (#246)
by
unknown
01:55
created

AppCommandOptionChoice.__post_init__()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 1
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
10
from .command_types import AppCommandOptionType, AppCommandType
11
from ...objects.guild.channel import ChannelType
0 ignored issues
show
introduced by
Cannot import 'objects.guild.channel' due to syntax error 'invalid syntax (<unknown>, line 269)'
Loading history...
Bug introduced by
The name channel does not seem to exist in module pincer.objects.guild.
Loading history...
12
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...
13
from ...utils.snowflake import Snowflake
14
from ...utils.types import Coro, choice_value_types
15
from ...utils.types import MISSING
16
17
if TYPE_CHECKING:
18
    from ...utils.types import APINullable
19
    from ..app.throttle_scope import ThrottleScope
20
21
22
@dataclass
23
class AppCommandInteractionDataOption(APIObject):
24
    """Represents a Discord Application Command Interaction Data Option
25
26
    Attributes
27
    ----------
28
    name: :class:`str`
29
        The name of the parameter
30
    value: :class:`str`
31
        The value of the pair
32
    type: APINullable[:class:`str`]
33
        Value of application command option type
34
    options: APINullable[List[:data:`~pincer.objects.app.command.AppCommandInteractionDataOption`]]
35
        Present if this option is a group or subcommand
36
    """
37
    # noqa: E501
38
    name: str
39
    value: APINullable[str] = MISSING
40
    type: APINullable[AppCommandOptionType] = MISSING
41
    options: APINullable[
42
        List[AppCommandInteractionDataOption]] = MISSING
43
44
45
@dataclass
46
class AppCommandOptionChoice(APIObject):
47
    """Represents a Discord Application Command Option Choice object
48
49
    Attributes
50
    ----------
51
    name: :class:`str`
52
        1-100 character choice name
53
    value: Union[:data:`~pincer.utils.types.choice_value_types`]
54
        Value of the choice, up to 100 characters if string
55
    """
56
    name: str
57
    value: choice_value_types
58
59
    def __post_init__(self):
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...
60
        # Default serialization causes too many issues with Union
61
        # It isn't needed here anyway
62
        return
63
64
65
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (10/7)
Loading history...
66
class AppCommandOption(APIObject):
67
    """Represents a Discord Application Command Option object
68
69
    Attributes
70
    ----------
71
    type: :class:`~pincer.objects.AppCommandOptionType`
72
        The type of option
73
    name: :class:`str`
74
        1-32 lowercase character name matching `^[\\w-]{1,32}$`
75
    description: :class:`str`
76
        1-100 character description
77
    required: APINullable[:class:`bool`]
78
        If the parameter is required or optional |default| :data:`False`
79
    choices: APINullable[List[:class:`~pincer.objects.app.command.AppCommandOptionChoice`]]
80
        Choices for `STRING`, `INTEGER`, and `NUMBER`
81
        types for the user to pick from, max 25
82
    options: APINullable[List[:class:`~pincer.objects.app.command.AppCommandOptionChoice`]]
83
        If the option is a subcommand or subcommand group type,
84
        this nested options will be the parameters
85
    """
86
    # noqa: E501
87
    type: AppCommandOptionType
88
    name: str
89
    description: str
90
91
    required: APINullable[bool] = MISSING
92
    autocomplete: APINullable[bool] = MISSING
93
    choices: APINullable[List[AppCommandOptionChoice]] = MISSING
94
    options: APINullable[List[AppCommandOption]] = MISSING
95
    channel_types: APINullable[List[ChannelType]] = MISSING
96
    min_value: APINullable[Union[int, float]] = MISSING
97
    max_value: APINullable[Union[int, float]] = MISSING
98
99
100
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (12/7)
Loading history...
101
class AppCommand(APIObject):
102
    """Represents a Discord Application Command object
103
104
    Attributes
105
    ----------
106
    type: :class:`~pincer.objects.app.command.AppCommandType`
107
        The type of command, defaults ``1`` if not set
108
    name: :class:`str`
109
        1-32 character name
110
    description: :class:`str`
111
        1-100 character description for ``CHAT_INPUT`` commands,
112
        empty string for ``USER`` and ``MESSAGE`` commands
113
    id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
114
        Unique id of the command
115
    version: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
116
        Auto-incrementing version identifier updated during substantial
117
        record changes
118
    application_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
119
        Unique id of the parent application
120
    options: APINullable[List[:class:`~pincer.objects.app.command.AppCommandOption`]]
121
        The parameters for the command, max 25
122
    guild_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
123
        Guild id of the command, if not global
124
    default_permission: APINullable[:class:`bool`]
125
        Whether the command is enabled by default
126
        when the app is added to a guild
127
    """
128
    # noqa: E501
129
    type: AppCommandType
130
    name: str
131
    description: str
132
133
    id: APINullable[Snowflake] = MISSING
134
    version: APINullable[Snowflake] = MISSING
135
    application_id: APINullable[Snowflake] = MISSING
136
    options: APINullable[List[AppCommandOption]] = MISSING
137
    guild_id: APINullable[Snowflake] = MISSING
138
    default_permission: APINullable[bool] = True
139
    default_member_permissions: APINullable[None] = None
140
    dm_permission: APINullable[None] = None
141
142
    _eq_props = [
143
        "type", "name", "description", "guild_id", "default_permission",
144
        "options"
145
    ]
146
147
    def __post_init__(self):
148
        super().__post_init__()
149
150
        self.options = [] if self.options is MISSING and self.type == AppCommandType.MESSAGE else self.options
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (110/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
151
152
    def __eq__(self, other: Union[AppCommand, ClientCommandStructure]):
153
        if isinstance(other, ClientCommandStructure):
154
            other = other.app
155
156
        return all(
157
            self.__getattribute__(prop) == other.__getattribute__(prop)
158
            for prop in self._eq_props
159
        )
160
161
    def __hash__(self):
162
        return hash((self.id, self.name, self.description, self.guild_id))
163
164
    def add_option(self, option: AppCommandOption):
165
        """Add a new option field to the current application command.
166
167
        Parameters
168
        ----------
169
        option : :class:`~pincer.objects.app.command.AppCommandOption`
170
            The option which will be appended.
171
        """
172
        if self.options:
173
            self.options.append(option)
174
        else:
175
            self.options = [option]
176
177
178
@dataclass
179
class ClientCommandStructure:
180
    """Represents the structure of how the client saves the existing
181
    commands in the register.
182
183
    Attributes
184
    ----------
185
    app: :class:`~pincer.objects.app.command.AppCommand`
186
        The command application.
187
    call: :class:`~pincer.utils.types.Coro`
188
        The coroutine which should be called when the command gets
189
        executed.
190
    cooldown: :class:`int`
191
        Amount of times for cooldown
192
    cooldown_scale: :class:`float`
193
        Search time for cooldown
194
    cooldown_scope: :class:`~pincer.objects.app.throttle_scope.ThrottleScope`
195
        The type of cooldown
196
    """
197
    app: AppCommand
198
    call: Coro
199
    cooldown: int
200
    cooldown_scale: float
201
    cooldown_scope: ThrottleScope
202