Skip to content

Commit 0492613

Browse files
IcebluewolfLulalabyDorukyum
authored
Add minmax length (#1463)
* Fix `slash_autocomplete` to not error The command description docstring was to long (over 100 characters). * Add minmax length options to slash commands * Add Checks to minmax functions * Apply suggestions from code review Co-authored-by: Dorukyum <[email protected]> * Update discord/commands/options.py * Apply suggestions from code review * Update discord/commands/options.py * Update options.py * try fix Co-authored-by: Lala Sabathil <[email protected]> Co-authored-by: Dorukyum <[email protected]> Co-authored-by: Lala Sabathil <[email protected]>
1 parent 4dbb11f commit 0492613

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

discord/commands/options.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ async def hello(
108108
max_value: Optional[:class:`int`]
109109
The maximum value that can be entered.
110110
Only applies to Options with an input_type of ``int`` or ``float``.
111+
min_length: Optional[:class:`int`]
112+
The minimum length of the string that can be entered. Must be between 0 and 6000 (inclusive).
113+
Only applies to Options with an input_type of ``str``.
114+
max_length: Optional[:class:`int`]
115+
The maximum length of the string that can be entered. Must be between 1 and 6000 (inclusive).
116+
Only applies to Options with an input_type of ``str``.
111117
autocomplete: Optional[:class:`Any`]
112118
The autocomplete handler for the option. Accepts an iterable of :class:`str`, a callable (sync or async) that takes a
113119
single argument of :class:`AutocompleteContext`, or a coroutine. Must resolve to an iterable of :class:`str`.
@@ -194,14 +200,40 @@ def __init__(self, input_type: Any = str, /, description: Optional[str] = None,
194200
minmax_types = (type(None),)
195201
minmax_typehint = Optional[Union[minmax_types]] # type: ignore
196202

203+
if self.input_type == SlashCommandOptionType.string:
204+
minmax_length_types = (int, type(None))
205+
else:
206+
minmax_length_types = (type(None),)
207+
minmax_length_typehint = Optional[Union[minmax_length_types]] # type: ignore
208+
197209
self.min_value: minmax_typehint = kwargs.pop("min_value", None)
198210
self.max_value: minmax_typehint = kwargs.pop("max_value", None)
211+
self.min_length: minmax_length_typehint = kwargs.pop("min_length", None)
212+
self.max_length: minmax_length_typehint = kwargs.pop("max_length", None)
199213

200-
if not isinstance(self.min_value, minmax_types) and self.min_value is not None:
214+
if (input_type != SlashCommandOptionType.integer and input_type != SlashCommandOptionType.number
215+
and (self.min_value or self.max_value)):
216+
raise AttributeError("Option does not take min_value or max_value if not of type "
217+
"SlashCommandOptionType.integer or SlashCommandOptionType.number")
218+
if input_type != SlashCommandOptionType.string and (self.min_length or self.max_length):
219+
raise AttributeError('Option does not take min_length or max_length if not of type str')
220+
221+
if self.min_value is not None and not isinstance(self.min_value, minmax_types):
201222
raise TypeError(f'Expected {minmax_typehint} for min_value, got "{type(self.min_value).__name__}"')
202-
if not (isinstance(self.max_value, minmax_types) or self.min_value is None):
223+
if self.max_value is not None and not isinstance(self.max_value, minmax_types):
203224
raise TypeError(f'Expected {minmax_typehint} for max_value, got "{type(self.max_value).__name__}"')
204225

226+
if self.min_length is not None:
227+
if not isinstance(self.min_length, minmax_length_types):
228+
raise TypeError(f'Expected {minmax_length_typehint} for min_length, got "{type(self.min_length).__name__}"')
229+
if self.min_length < 0 or self.min_length > 6000:
230+
raise AttributeError("min_length must be between 0 and 6000 (inclusive)")
231+
if self.max_length is not None:
232+
if not isinstance(self.max_length, minmax_length_types):
233+
raise TypeError(f'Expected {minmax_length_typehint} for max_length, got "{type(self.max_length).__name__}"')
234+
if self.max_length < 1 or self.max_length > 6000:
235+
raise AttributeError("max_length must between 1 and 6000 (inclusive)")
236+
205237
self.autocomplete = kwargs.pop("autocomplete", None)
206238

207239
self.name_localizations = kwargs.pop("name_localizations", None)
@@ -226,6 +258,10 @@ def to_dict(self) -> Dict:
226258
as_dict["min_value"] = self.min_value
227259
if self.max_value is not None:
228260
as_dict["max_value"] = self.max_value
261+
if self.min_length is not None:
262+
as_dict["min_length"] = self.min_length
263+
if self.max_length is not None:
264+
as_dict["max_length"] = self.max_length
229265

230266
return as_dict
231267

0 commit comments

Comments
 (0)