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 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 |
|
|
|
|
12
|
|
|
from ...utils.api_object import APIObject |
|
|
|
|
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): |
|
|
|
|
60
|
|
|
# Default serialization causes too many issues with Union |
61
|
|
|
# It isn't needed here anyway |
62
|
|
|
return |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
@dataclass |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
if self.options is MISSING and self.type == AppCommandType.MESSAGE: |
151
|
|
|
self.options = [] |
152
|
|
|
|
153
|
|
|
def __eq__(self, other: Union[AppCommand, ClientCommandStructure]): |
154
|
|
|
if isinstance(other, ClientCommandStructure): |
155
|
|
|
other = other.app |
156
|
|
|
|
157
|
|
|
return all( |
158
|
|
|
self.__getattribute__(prop) == other.__getattribute__(prop) |
159
|
|
|
for prop in self._eq_props |
160
|
|
|
) |
161
|
|
|
|
162
|
|
|
def __hash__(self): |
163
|
|
|
return hash((self.id, self.name, self.description, self.guild_id)) |
164
|
|
|
|
165
|
|
|
def add_option(self, option: AppCommandOption): |
166
|
|
|
"""Add a new option field to the current application command. |
167
|
|
|
|
168
|
|
|
Parameters |
169
|
|
|
---------- |
170
|
|
|
option : :class:`~pincer.objects.app.command.AppCommandOption` |
171
|
|
|
The option which will be appended. |
172
|
|
|
""" |
173
|
|
|
if self.options: |
174
|
|
|
self.options.append(option) |
175
|
|
|
else: |
176
|
|
|
self.options = [option] |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
@dataclass |
180
|
|
|
class ClientCommandStructure: |
181
|
|
|
"""Represents the structure of how the client saves the existing |
182
|
|
|
commands in the register. |
183
|
|
|
|
184
|
|
|
Attributes |
185
|
|
|
---------- |
186
|
|
|
app: :class:`~pincer.objects.app.command.AppCommand` |
187
|
|
|
The command application. |
188
|
|
|
call: :class:`~pincer.utils.types.Coro` |
189
|
|
|
The coroutine which should be called when the command gets |
190
|
|
|
executed. |
191
|
|
|
cooldown: :class:`int` |
192
|
|
|
Amount of times for cooldown |
193
|
|
|
cooldown_scale: :class:`float` |
194
|
|
|
Search time for cooldown |
195
|
|
|
cooldown_scope: :class:`~pincer.objects.app.throttle_scope.ThrottleScope` |
196
|
|
|
The type of cooldown |
197
|
|
|
""" |
198
|
|
|
app: AppCommand |
199
|
|
|
call: Coro |
200
|
|
|
cooldown: int |
201
|
|
|
cooldown_scale: float |
202
|
|
|
cooldown_scope: ThrottleScope |
203
|
|
|
|