|
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.snowflake import Snowflake |
|
33
|
|
|
from pincer.utils.types import MISSING, APINullable |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class ApplicationCommandType(Enum): |
|
37
|
|
|
""" |
|
38
|
|
|
Defines the different types of application commands. |
|
39
|
|
|
|
|
40
|
|
|
:param CHAT_INPUT: |
|
41
|
|
|
Slash commands; a text-based command that shows up when a user |
|
42
|
|
|
types / |
|
43
|
|
|
|
|
44
|
|
|
:param USER: |
|
45
|
|
|
A UI-based command that shows up when you right click or tap on |
|
46
|
|
|
a user |
|
47
|
|
|
|
|
48
|
|
|
:param MESSAGE: |
|
49
|
|
|
A UI-based command that shows up when you right click or tap on |
|
50
|
|
|
a message |
|
51
|
|
|
""" |
|
52
|
|
|
CHAT_INPUT = 1 |
|
53
|
|
|
USER = 2 |
|
54
|
|
|
MESSAGE = 3 |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
class ApplicationCommandOptionType(Enum): |
|
58
|
|
|
""" |
|
59
|
|
|
Represents a parameter type. |
|
60
|
|
|
|
|
61
|
|
|
:param SUB_COMMAND: |
|
62
|
|
|
The parameter will be a subcommand. |
|
63
|
|
|
|
|
64
|
|
|
:param SUB_COMMAND_GROUP: |
|
65
|
|
|
The parameter will be a group of subcommands. |
|
66
|
|
|
|
|
67
|
|
|
:param STRING: |
|
68
|
|
|
The parameter will be a string. |
|
69
|
|
|
|
|
70
|
|
|
:param INTEGER: |
|
71
|
|
|
The parameter will be an integer/number. (-2^53 and 2^53) |
|
72
|
|
|
|
|
73
|
|
|
:param BOOLEAN: |
|
74
|
|
|
The parameter will be a boolean. |
|
75
|
|
|
|
|
76
|
|
|
:param USER: |
|
77
|
|
|
The parameter will be a Discord user object. |
|
78
|
|
|
|
|
79
|
|
|
:param CHANNEL: |
|
80
|
|
|
The parameter will be a Discord channel object. |
|
81
|
|
|
|
|
82
|
|
|
:param ROLE: |
|
83
|
|
|
The parameter will be a Discord role object. |
|
84
|
|
|
|
|
85
|
|
|
:param MENTIONABLE: |
|
86
|
|
|
The parameter will be mentionable. |
|
87
|
|
|
|
|
88
|
|
|
:param NUMBER: |
|
89
|
|
|
The parameter will be a float. (-2^53 and 2^53) |
|
90
|
|
|
""" |
|
91
|
|
|
SUB_COMMAND = 1 |
|
92
|
|
|
SUB_COMMAND_GROUP = 2 |
|
93
|
|
|
STRING = 3 |
|
94
|
|
|
INTEGER = 4 # 54-bit |
|
95
|
|
|
BOOLEAN = 5 |
|
96
|
|
|
USER = 6 |
|
97
|
|
|
CHANNEL = 7 |
|
98
|
|
|
ROLE = 8 |
|
99
|
|
|
MENTIONABLE = 9 |
|
100
|
|
|
NUMBER = 10 # 54-bit |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
@dataclass |
|
104
|
|
|
class ApplicationCommandInteractionDataOption(APIObject): |
|
105
|
|
|
""" |
|
106
|
|
|
Represents a Discord Application Command Interaction Data Option |
|
107
|
|
|
|
|
108
|
|
|
:param name: |
|
109
|
|
|
the name of the parameter |
|
110
|
|
|
|
|
111
|
|
|
:param type: |
|
112
|
|
|
value of application command option type |
|
113
|
|
|
|
|
114
|
|
|
:param value: |
|
115
|
|
|
the value of the pair |
|
116
|
|
|
|
|
117
|
|
|
:param options: |
|
118
|
|
|
present if this option is a group or subcommand |
|
119
|
|
|
""" |
|
120
|
|
|
name: str |
|
121
|
|
|
type: int |
|
122
|
|
|
value: APINullable[ApplicationCommandOptionType] = MISSING |
|
123
|
|
|
options: APINullable[ |
|
124
|
|
|
List[ApplicationCommandInteractionDataOption]] = MISSING |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
@dataclass |
|
128
|
|
|
class ApplicationCommandOptionChoice(APIObject): |
|
129
|
|
|
""" |
|
130
|
|
|
Represents a Discord Application Command Option Choice object |
|
131
|
|
|
|
|
132
|
|
|
:param name: |
|
133
|
|
|
1-100 character choice name |
|
134
|
|
|
|
|
135
|
|
|
:param value: |
|
136
|
|
|
value of the choice, up to 100 characters if string |
|
137
|
|
|
""" |
|
138
|
|
|
name: str |
|
139
|
|
|
value: Union[str, int, float] |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
@dataclass |
|
143
|
|
|
class ApplicationCommandOption(APIObject): |
|
144
|
|
|
""" |
|
|
|
|
|
|
145
|
|
|
Represents a Discord Application Command Option object |
|
146
|
|
|
|
|
147
|
|
|
:param type: |
|
148
|
|
|
the type of option |
|
149
|
|
|
|
|
150
|
|
|
:param name: |
|
151
|
|
|
1-32 lowercase character name matching `^[\w-]{1,32}$` |
|
152
|
|
|
|
|
153
|
|
|
:param description: |
|
154
|
|
|
1-100 character description |
|
155
|
|
|
|
|
156
|
|
|
:param required: |
|
157
|
|
|
if the parameter is required or optional--default `False` |
|
158
|
|
|
|
|
159
|
|
|
:param choices: |
|
160
|
|
|
choices for `STRING`, `INTEGER`, and `NUMBER` |
|
161
|
|
|
types for the user to pick from, max 25 |
|
162
|
|
|
|
|
163
|
|
|
:param options: |
|
164
|
|
|
if the option is a subcommand or subcommand group type, |
|
165
|
|
|
this nested options will be the parameters |
|
166
|
|
|
""" |
|
167
|
|
|
type: ApplicationCommandOptionType |
|
168
|
|
|
name: str |
|
169
|
|
|
description: str |
|
170
|
|
|
|
|
171
|
|
|
required: APINullable[bool] = False |
|
172
|
|
|
choices: APINullable[List[ApplicationCommandOptionChoice]] = MISSING |
|
173
|
|
|
options: APINullable[List[ApplicationCommandOption]] = MISSING |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
@dataclass |
|
|
|
|
|
|
177
|
|
|
class ApplicationCommand(APIObject): |
|
178
|
|
|
""" |
|
179
|
|
|
Represents a Discord Application Command object |
|
180
|
|
|
|
|
181
|
|
|
:param id: |
|
182
|
|
|
unique id of the command |
|
183
|
|
|
|
|
184
|
|
|
:param type: |
|
185
|
|
|
the type of command, defaults `1` if not set |
|
186
|
|
|
|
|
187
|
|
|
:param application_id: |
|
188
|
|
|
unique id of the parent application |
|
189
|
|
|
|
|
190
|
|
|
:param guild_id: |
|
191
|
|
|
guild id of the command, if not global |
|
192
|
|
|
|
|
193
|
|
|
:param name: |
|
194
|
|
|
1-32 character name |
|
195
|
|
|
|
|
196
|
|
|
:param description: |
|
197
|
|
|
1-100 character description for `CHAT_INPUT` commands, |
|
198
|
|
|
empty string for `USER` and `MESSAGE` commands |
|
199
|
|
|
|
|
200
|
|
|
:param options: |
|
201
|
|
|
the parameters for the command, max 25 |
|
202
|
|
|
|
|
203
|
|
|
:param default_permission: |
|
204
|
|
|
whether the command is enabled by default |
|
205
|
|
|
when the app is added to a guild |
|
206
|
|
|
""" |
|
207
|
|
|
type: ApplicationCommandType |
|
208
|
|
|
name: str |
|
209
|
|
|
description: str |
|
210
|
|
|
|
|
211
|
|
|
id: APINullable[Snowflake] = MISSING |
|
|
|
|
|
|
212
|
|
|
application_id: APINullable[Snowflake] = MISSING |
|
213
|
|
|
options: APINullable[List[ApplicationCommandOption]] = MISSING |
|
214
|
|
|
guild_id: APINullable[Snowflake] = MISSING |
|
215
|
|
|
default_permission: APINullable[bool] = True |
|
216
|
|
|
|
|
217
|
|
|
def add_option(self, option: ApplicationCommandOption): |
|
218
|
|
|
""" |
|
219
|
|
|
Add a new option field to the current application command. |
|
220
|
|
|
|
|
221
|
|
|
:param option: The option which will be appended. |
|
222
|
|
|
""" |
|
223
|
|
|
if self.options: |
|
224
|
|
|
self.options.append(option) |
|
225
|
|
|
else: |
|
226
|
|
|
self.options = [option] |
|
227
|
|
|
|