Skip to content

Commit f301495

Browse files
committed
feat(application_commands): Implemented min_length and max_length fields in SlashCommandOption
1 parent fcb263c commit f301495

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

discord/application_commands.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,12 @@ class SlashCommandOption:
563563
If the :attr:`~SlashCommandOption.option_type` is one of :attr:`~OptionType.integer` or :attr:`~OptionType.number`
564564
this is the minimum value the users input must be of.
565565
max_value: Optional[Union[:class:`int`, :class:`float`]]
566-
If the :attr:`option_type` is one of :class:`OptionType.integer` or :class:`OptionType.number`
566+
If the :attr:`option_type` is one of :class:`~OptionType.integer` or :class:`~OptionType.number`
567567
this is the maximum value the users input could be of.
568+
min_length: :class:`int`
569+
If the :attr:`option_type` is :class:`~OptionType.string`, this is the minimum length (min. of ``0``)
570+
max_length: :class:`int`
571+
If the :attr:`option_type` is :class:`~OptionType.string`, this is the maximum length (min. of ``1``)
568572
channel_types: Optional[List[Union[:class:`abc.GuildChannel`, :class:`ChannelType`, :class:`int`]]]
569573
A list of :class:`ChannelType` or the type itself like ``TextChannel`` or ``StageChannel`` the user could select.
570574
Only valid if :attr:`~SlashCommandOption.option_type` is :class:`OptionType.channel`.
@@ -589,6 +593,8 @@ def __init__(self,
589593
autocomplete: bool = False,
590594
min_value: Optional[Union[int, float]] = None,
591595
max_value: Optional[Union[int, float]] = None,
596+
min_length: int = None,
597+
max_length: int = None,
592598
channel_types: Optional[List[Union[type(GuildChannel), ChannelType, int]]] = None,
593599
default: Optional[Any] = None,
594600
converter: Optional['Converter'] = None,
@@ -626,6 +632,8 @@ def __init__(self,
626632
self.autocomplete: bool = autocomplete
627633
self.min_value: Optional[Union[int, float]] = min_value
628634
self.max_value: Optional[Union[int, float]] = max_value
635+
self.min_length: int = min_length
636+
self.max_length: int = max_length
629637
for index, choice in enumerate(choices): # TODO: find a more efficient way to do this
630638
if not isinstance(choice, SlashCommandOptionChoice):
631639
choices[index] = SlashCommandOptionChoice(choice)
@@ -769,6 +777,13 @@ def to_dict(self) -> dict:
769777
base['min_value'] = self.min_value
770778
if self.max_value is not None:
771779
base['max_value'] = self.max_value
780+
if self.type.string:
781+
min_length = self.min_length
782+
max_length = self.max_length
783+
if min_length:
784+
base['min_length'] = min_length
785+
if max_length:
786+
base['max_length'] = max_length
772787
if self.channel_types:
773788
base['channel_types'] = [int(ch_type) for ch_type in self.channel_types]
774789
return base
@@ -790,7 +805,9 @@ def from_dict(cls, data) -> SlashCommandOption:
790805
choices=[SlashCommandOptionChoice.from_dict(c) for c in data.get('choices', [])],
791806
autocomplete=data.get('autocomplete', False),
792807
min_value=data.get('min_value', None),
793-
max_value=data.get('max_value', None)
808+
max_value=data.get('max_value', None),
809+
min_length=data.get('min_length', None),
810+
max_length=data.get('max_length', None)
794811
)
795812

796813

0 commit comments

Comments
 (0)