1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
# MIT License |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2021 Pincer |
5
|
|
|
# |
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining |
7
|
|
|
# a copy of this software and associated documentation files |
8
|
|
|
# (the "Software"), to deal in the Software without restriction, |
9
|
|
|
# including without limitation the rights to use, copy, modify, merge, |
10
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software, |
11
|
|
|
# and to permit persons to whom the Software is furnished to do so, |
12
|
|
|
# subject to the following conditions: |
13
|
|
|
# |
14
|
|
|
# The above copyright notice and this permission notice shall be |
15
|
|
|
# included in all copies or substantial portions of the Software. |
16
|
|
|
# |
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
20
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
21
|
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
22
|
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
23
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24
|
|
|
|
25
|
|
|
from __future__ import annotations |
26
|
|
|
|
27
|
|
|
from dataclasses import dataclass |
28
|
|
|
from enum import Enum |
29
|
|
|
from typing import List, Union |
30
|
|
|
|
31
|
|
|
from pincer.utils.api_object import APIObject |
32
|
|
|
from pincer.utils.conversion import convert |
33
|
|
|
from pincer.utils.snowflake import Snowflake |
34
|
|
|
from pincer.utils.types import MISSING, APINullable, Coro |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class ApplicationCommandType(Enum): |
38
|
|
|
""" |
39
|
|
|
Defines the different types of application commands. |
40
|
|
|
|
41
|
|
|
:param CHAT_INPUT: |
42
|
|
|
Slash commands; a text-based command that shows up when a user |
43
|
|
|
types / |
44
|
|
|
|
45
|
|
|
:param USER: |
46
|
|
|
A UI-based command that shows up when you right click or tap on |
47
|
|
|
a user |
48
|
|
|
|
49
|
|
|
:param MESSAGE: |
50
|
|
|
A UI-based command that shows up when you right click or tap on |
51
|
|
|
a message |
52
|
|
|
""" |
53
|
|
|
CHAT_INPUT = 1 |
54
|
|
|
USER = 2 |
55
|
|
|
MESSAGE = 3 |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
class ApplicationCommandOptionType(Enum): |
59
|
|
|
""" |
60
|
|
|
Represents a parameter type. |
61
|
|
|
|
62
|
|
|
:param SUB_COMMAND: |
63
|
|
|
The parameter will be a subcommand. |
64
|
|
|
|
65
|
|
|
:param SUB_COMMAND_GROUP: |
66
|
|
|
The parameter will be a group of subcommands. |
67
|
|
|
|
68
|
|
|
:param STRING: |
69
|
|
|
The parameter will be a string. |
70
|
|
|
|
71
|
|
|
:param INTEGER: |
72
|
|
|
The parameter will be an integer/number. (-2^53 and 2^53) |
73
|
|
|
|
74
|
|
|
:param BOOLEAN: |
75
|
|
|
The parameter will be a boolean. |
76
|
|
|
|
77
|
|
|
:param USER: |
78
|
|
|
The parameter will be a Discord user object. |
79
|
|
|
|
80
|
|
|
:param CHANNEL: |
81
|
|
|
The parameter will be a Discord channel object. |
82
|
|
|
|
83
|
|
|
:param ROLE: |
84
|
|
|
The parameter will be a Discord role object. |
85
|
|
|
|
86
|
|
|
:param MENTIONABLE: |
87
|
|
|
The parameter will be mentionable. |
88
|
|
|
|
89
|
|
|
:param NUMBER: |
90
|
|
|
The parameter will be a float. (-2^53 and 2^53) |
91
|
|
|
""" |
92
|
|
|
SUB_COMMAND = 1 |
93
|
|
|
SUB_COMMAND_GROUP = 2 |
94
|
|
|
STRING = 3 |
95
|
|
|
INTEGER = 4 # 54-bit |
96
|
|
|
BOOLEAN = 5 |
97
|
|
|
USER = 6 |
98
|
|
|
CHANNEL = 7 |
99
|
|
|
ROLE = 8 |
100
|
|
|
MENTIONABLE = 9 |
101
|
|
|
NUMBER = 10 # 54-bit |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
@dataclass |
105
|
|
|
class ApplicationCommandInteractionDataOption(APIObject): |
106
|
|
|
""" |
107
|
|
|
Represents a Discord Application Command Interaction Data Option |
108
|
|
|
|
109
|
|
|
:param name: |
110
|
|
|
the name of the parameter |
111
|
|
|
|
112
|
|
|
:param type: |
113
|
|
|
value of application command option type |
114
|
|
|
|
115
|
|
|
:param value: |
116
|
|
|
the value of the pair |
117
|
|
|
|
118
|
|
|
:param options: |
119
|
|
|
present if this option is a group or subcommand |
120
|
|
|
""" |
121
|
|
|
name: str |
122
|
|
|
type: int |
123
|
|
|
value: APINullable[ApplicationCommandOptionType] = MISSING |
124
|
|
|
options: APINullable[ |
125
|
|
|
List[ApplicationCommandInteractionDataOption]] = MISSING |
126
|
|
|
|
127
|
|
|
def __post_init__(self): |
128
|
|
|
self.value = convert(self.value, ApplicationCommandOptionType) |
129
|
|
|
self.options = convert( |
130
|
|
|
self.options, |
131
|
|
|
ApplicationCommandInteractionDataOption.from_dict, |
132
|
|
|
ApplicationCommandInteractionDataOption |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
@dataclass |
137
|
|
|
class ApplicationCommandOptionChoice(APIObject): |
138
|
|
|
""" |
139
|
|
|
Represents a Discord Application Command Option Choice object |
140
|
|
|
|
141
|
|
|
:param name: |
142
|
|
|
1-100 character choice name |
143
|
|
|
|
144
|
|
|
:param value: |
145
|
|
|
value of the choice, up to 100 characters if string |
146
|
|
|
""" |
147
|
|
|
name: str |
148
|
|
|
value: Union[str, int, float] |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
@dataclass |
152
|
|
|
class ApplicationCommandOption(APIObject): |
153
|
|
|
""" |
|
|
|
|
154
|
|
|
Represents a Discord Application Command Option object |
155
|
|
|
|
156
|
|
|
:param type: |
157
|
|
|
the type of option |
158
|
|
|
|
159
|
|
|
:param name: |
160
|
|
|
1-32 lowercase character name matching `^[\w-]{1,32}$` |
161
|
|
|
|
162
|
|
|
:param description: |
163
|
|
|
1-100 character description |
164
|
|
|
|
165
|
|
|
:param required: |
166
|
|
|
if the parameter is required or optional--default `False` |
167
|
|
|
|
168
|
|
|
:param choices: |
169
|
|
|
choices for `STRING`, `INTEGER`, and `NUMBER` |
170
|
|
|
types for the user to pick from, max 25 |
171
|
|
|
|
172
|
|
|
:param options: |
173
|
|
|
if the option is a subcommand or subcommand group type, |
174
|
|
|
this nested options will be the parameters |
175
|
|
|
""" |
176
|
|
|
type: ApplicationCommandOptionType |
177
|
|
|
name: str |
178
|
|
|
description: str |
179
|
|
|
|
180
|
|
|
required: APINullable[bool] = False |
181
|
|
|
choices: APINullable[List[ApplicationCommandOptionChoice]] = MISSING |
182
|
|
|
options: APINullable[List[ApplicationCommandOption]] = MISSING |
183
|
|
|
|
184
|
|
|
def __post_init__(self): |
185
|
|
|
self.choices = convert( |
186
|
|
|
self.choices, |
187
|
|
|
ApplicationCommandOptionChoice.from_dict, |
188
|
|
|
ApplicationCommandOptionChoice |
189
|
|
|
) |
190
|
|
|
self.options = convert( |
191
|
|
|
self.options, |
192
|
|
|
ApplicationCommandOption.from_dict, |
193
|
|
|
ApplicationCommandOption |
194
|
|
|
) |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
@dataclass |
|
|
|
|
198
|
|
|
class ApplicationCommand(APIObject): |
199
|
|
|
""" |
200
|
|
|
Represents a Discord Application Command object |
201
|
|
|
|
202
|
|
|
:param id: |
203
|
|
|
unique id of the command |
204
|
|
|
|
205
|
|
|
:param type: |
206
|
|
|
the type of command, defaults `1` if not set |
207
|
|
|
|
208
|
|
|
:param application_id: |
209
|
|
|
unique id of the parent application |
210
|
|
|
|
211
|
|
|
:param guild_id: |
212
|
|
|
guild id of the command, if not global |
213
|
|
|
|
214
|
|
|
:param name: |
215
|
|
|
1-32 character name |
216
|
|
|
|
217
|
|
|
:param description: |
218
|
|
|
1-100 character description for `CHAT_INPUT` commands, |
219
|
|
|
empty string for `USER` and `MESSAGE` commands |
220
|
|
|
|
221
|
|
|
:param options: |
222
|
|
|
the parameters for the command, max 25 |
223
|
|
|
|
224
|
|
|
:param default_permission: |
225
|
|
|
whether the command is enabled by default |
226
|
|
|
when the app is added to a guild |
227
|
|
|
|
228
|
|
|
:param version: |
229
|
|
|
autoincrementing version identifier updated during substantial |
230
|
|
|
record changes |
231
|
|
|
""" |
232
|
|
|
type: ApplicationCommandType |
233
|
|
|
name: str |
234
|
|
|
description: str |
235
|
|
|
|
236
|
|
|
id: APINullable[Snowflake] = MISSING |
|
|
|
|
237
|
|
|
version: APINullable[Snowflake] = MISSING |
238
|
|
|
application_id: APINullable[Snowflake] = MISSING |
239
|
|
|
options: APINullable[List[ApplicationCommandOption]] = MISSING |
240
|
|
|
guild_id: APINullable[Snowflake] = MISSING |
241
|
|
|
default_permission: APINullable[bool] = True |
242
|
|
|
|
243
|
|
|
_eq_props = ["type", "name", "description", "application_id", "options", |
244
|
|
|
"guild_id", "default_permission"] |
245
|
|
|
|
246
|
|
|
# def __post_init__(self): |
247
|
|
|
# self.id = convert(self.id, Snowflake.from_string) |
248
|
|
|
# self.version = convert(self.version, Snowflake.from_string) |
249
|
|
|
# self.application_id = convert(self.application_id, |
250
|
|
|
# Snowflake.from_string) |
251
|
|
|
# self.options = convert( |
252
|
|
|
# self.options, |
253
|
|
|
# ApplicationCommandOption.from_dict, |
254
|
|
|
# ApplicationCommandOption |
255
|
|
|
# ) |
256
|
|
|
# self.guild_id = convert(self.guild_id, Snowflake.from_string) |
257
|
|
|
|
258
|
|
|
def __eq__(self, other: ApplicationCommand): |
259
|
|
|
return all( |
260
|
|
|
self.__getattribute__(prop) == other.__getattribute__(prop) |
261
|
|
|
for prop in self._eq_props |
262
|
|
|
) |
263
|
|
|
|
264
|
|
|
def add_option(self, option: ApplicationCommandOption): |
265
|
|
|
""" |
266
|
|
|
Add a new option field to the current application command. |
267
|
|
|
|
268
|
|
|
:param option: The option which will be appended. |
269
|
|
|
""" |
270
|
|
|
if self.options: |
271
|
|
|
self.options.append(option) |
272
|
|
|
else: |
273
|
|
|
self.options = [option] |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
@dataclass |
277
|
|
|
class ClientCommandStructure: |
278
|
|
|
""" |
279
|
|
|
Represents the structure of how the client saves the existing |
280
|
|
|
commands in the register. |
281
|
|
|
|
282
|
|
|
:param app: |
283
|
|
|
The command application. |
284
|
|
|
|
285
|
|
|
:param call: |
286
|
|
|
The coroutine which should be called when the command gets |
287
|
|
|
executed. |
288
|
|
|
""" |
289
|
|
|
app: ApplicationCommand |
290
|
|
|
call: Coro |
291
|
|
|
|