-
-
Notifications
You must be signed in to change notification settings - Fork 457
/
options.py
256 lines (215 loc) · 10.6 KB
/
options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
The MIT License (MIT)
Copyright (c) 2021-present Pycord Development
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from typing import Any, Dict, List, Literal, Optional, Union
from ..enums import ChannelType, SlashCommandOptionType
__all__ = (
"ThreadOption",
"Option",
"OptionChoice",
"option",
)
channel_type_map = {
"TextChannel": ChannelType.text,
"VoiceChannel": ChannelType.voice,
"StageChannel": ChannelType.stage_voice,
"CategoryChannel": ChannelType.category,
"Thread": ChannelType.public_thread,
}
class ThreadOption:
def __init__(self, thread_type: Literal["public", "private", "news"]):
type_map = {
"public": ChannelType.public_thread,
"private": ChannelType.private_thread,
"news": ChannelType.news_thread,
}
self._type = type_map[thread_type]
@property
def __name__(self):
return "ThreadOption"
class Option:
"""Represents a selectable option for a slash command.
Examples
--------
Basic usage: ::
@bot.slash_command(guild_ids=[...])
async def hello(
ctx: discord.ApplicationContext,
name: Option(str, "Enter your name"),
age: Option(int, "Enter your age", min_value=1, max_value=99, default=18)
# passing the default value makes an argument optional
# you also can create optional argument using:
# age: Option(int, "Enter your age") = 18
):
await ctx.respond(f"Hello! Your name is {name} and you are {age} years old.")
.. versionadded:: 2.0
Attributes
----------
input_type: :class:`Any`
The type of input that is expected for this option.
name: :class:`str`
The name of this option visible in the UI.
Inherits from the variable name if not provided as a parameter.
description: Optional[:class:`str`]
The description of this option.
Must be 100 characters or fewer.
choices: Optional[List[Union[:class:`Any`, :class:`OptionChoice`]]]
The list of available choices for this option.
Can be a list of values or :class:`OptionChoice` objects (which represent a name:value pair).
If provided, the input from the user must match one of the choices in the list.
required: Optional[:class:`bool`]
Whether this option is required.
default: Optional[:class:`Any`]
The default value for this option. If provided, ``required`` will be considered ``False``.
min_value: Optional[:class:`int`]
The minimum value that can be entered.
Only applies to Options with an input_type of ``int`` or ``float``.
max_value: Optional[:class:`int`]
The maximum value that can be entered.
Only applies to Options with an input_type of ``int`` or ``float``.
autocomplete: Optional[:class:`Any`]
The autocomplete handler for the option. Accepts an iterable of :class:`str`, a callable (sync or async) that takes a
single argument of :class:`AutocompleteContext`, or a coroutine. Must resolve to an iterable of :class:`str`.
.. note::
Does not validate the input value against the autocomplete results.
name_localizations: Optional[Dict[:class:`str`, :class:`str`]]
The name localizations for this option. The values of this should be ``"locale": "name"``.
See `here <https://discord.com/developers/docs/reference#locales>`_ for a list of valid locales.
description_localizations: Optional[Dict[:class:`str`, :class:`str`]]
The description localizations for this option. The values of this should be ``"locale": "description"``.
See `here <https://discord.com/developers/docs/reference#locales>`_ for a list of valid locales.
"""
def __init__(self, input_type: Any = str, /, description: Optional[str] = None, **kwargs) -> None:
self.name: Optional[str] = kwargs.pop("name", None)
if self.name is not None:
self.name = str(self.name)
self._parameter_name = self.name # default
self.description = description or "No description provided"
self.converter = None
self._raw_type = input_type
self.channel_types: List[ChannelType] = kwargs.pop("channel_types", [])
if not isinstance(input_type, SlashCommandOptionType):
if hasattr(input_type, "convert"):
self.converter = input_type
input_type = SlashCommandOptionType.string
else:
try:
_type = SlashCommandOptionType.from_datatype(input_type)
except TypeError as exc:
from ..ext.commands.converter import CONVERTER_MAPPING
if input_type not in CONVERTER_MAPPING:
raise exc
self.converter = CONVERTER_MAPPING[input_type]
input_type = SlashCommandOptionType.string
else:
if _type == SlashCommandOptionType.channel:
if not isinstance(input_type, tuple):
if hasattr(input_type, "__args__"): # Union
input_type = input_type.__args__
else:
input_type = (input_type,)
for i in input_type:
if i.__name__ == "GuildChannel":
continue
if isinstance(i, ThreadOption):
self.channel_types.append(i._type)
continue
channel_type = channel_type_map[i.__name__]
self.channel_types.append(channel_type)
input_type = _type
self.input_type = input_type
self.required: bool = kwargs.pop("required", True) if "default" not in kwargs else False
self.default = kwargs.pop("default", None)
self.choices: List[OptionChoice] = [
o if isinstance(o, OptionChoice) else OptionChoice(o) for o in kwargs.pop("choices", list())
]
if self.input_type == SlashCommandOptionType.integer:
minmax_types = (int, type(None))
elif self.input_type == SlashCommandOptionType.number:
minmax_types = (int, float, type(None))
else:
minmax_types = (type(None),)
minmax_typehint = Optional[Union[minmax_types]] # type: ignore
self.min_value: minmax_typehint = kwargs.pop("min_value", None)
self.max_value: minmax_typehint = kwargs.pop("max_value", None)
if not isinstance(self.min_value, minmax_types) and self.min_value is not None:
raise TypeError(f'Expected {minmax_typehint} for min_value, got "{type(self.min_value).__name__}"')
if not (isinstance(self.max_value, minmax_types) or self.min_value is None):
raise TypeError(f'Expected {minmax_typehint} for max_value, got "{type(self.max_value).__name__}"')
self.autocomplete = kwargs.pop("autocomplete", None)
self.name_localizations = kwargs.pop("name_localizations", None)
self.description_localizations = kwargs.pop("description_localizations", None)
def to_dict(self) -> Dict:
as_dict = {
"name": self.name,
"description": self.description,
"type": self.input_type.value,
"required": self.required,
"choices": [c.to_dict() for c in self.choices],
"autocomplete": bool(self.autocomplete),
}
if self.name_localizations is not None:
as_dict["name_localizations"] = self.name_localizations
if self.description_localizations is not None:
as_dict["description_localizations"] = self.description_localizations
if self.channel_types:
as_dict["channel_types"] = [t.value for t in self.channel_types]
if self.min_value is not None:
as_dict["min_value"] = self.min_value
if self.max_value is not None:
as_dict["max_value"] = self.max_value
return as_dict
def __repr__(self):
return f"<discord.commands.{self.__class__.__name__} name={self.name}>"
class OptionChoice:
"""
Represents a name:value pairing for a selected :class:`Option`.
.. versionadded:: 2.0
Attributes
----------
name: :class:`str`
The name of the choice. Shown in the UI when selecting an option.
value: Optional[Union[:class:`str`, :class:`int`, :class:`float`]]
The value of the choice. If not provided, will use the value of ``name``.
name_localizations: Optional[Dict[:class:`str`, :class:`str`]]
The name localizations for this choice. The values of this should be ``"locale": "name"``.
See `here <https://discord.com/developers/docs/reference#locales>`_ for a list of valid locales.
"""
def __init__(
self,
name: str,
value: Optional[Union[str, int, float]] = None,
name_localizations: Optional[Dict[str, str]] = None,
):
self.name = str(name)
self.value = value if value is not None else name
self.name_localizations = name_localizations
def to_dict(self) -> Dict[str, Union[str, int, float]]:
as_dict = {"name": self.name, "value": self.value}
if self.name_localizations is not None:
as_dict["name_localizations"] = self.name_localizations
return as_dict
def option(name, type=None, **kwargs):
"""A decorator that can be used instead of typehinting Option"""
def decorator(func):
nonlocal type
type = type or func.__annotations__.get(name, str)
func.__annotations__[name] = Option(type, **kwargs)
return func
return decorator