mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-08-31 07:21:41 +00:00
parent
377561844d
commit
bbf7a7981b
@ -223,9 +223,9 @@ def _populate_choices(params: Dict[str, CommandParameter], all_choices: Dict[str
|
|||||||
if param.type not in (AppCommandOptionType.string, AppCommandOptionType.number, AppCommandOptionType.integer):
|
if param.type not in (AppCommandOptionType.string, AppCommandOptionType.number, AppCommandOptionType.integer):
|
||||||
raise TypeError('choices are only supported for integer, string, or number option types')
|
raise TypeError('choices are only supported for integer, string, or number option types')
|
||||||
|
|
||||||
# There's a type safety hole if someone does Choice[float] as an annotation
|
if not all(param.type == choice._option_type for choice in choices):
|
||||||
# but the values are actually Choice[int]. Since the input-output is the same this feels
|
raise TypeError('choices must all have the same inner option type as the parameter choice type')
|
||||||
# safe enough to ignore.
|
|
||||||
param.choices = choices
|
param.choices = choices
|
||||||
|
|
||||||
if all_choices:
|
if all_choices:
|
||||||
|
@ -188,6 +188,19 @@ class Choice(Generic[ChoiceT]):
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f'{self.__class__.__name__}(name={self.name!r}, value={self.value!r})'
|
return f'{self.__class__.__name__}(name={self.name!r}, value={self.value!r})'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _option_type(self) -> AppCommandOptionType:
|
||||||
|
if isinstance(self.value, int):
|
||||||
|
return AppCommandOptionType.integer
|
||||||
|
elif isinstance(self.value, float):
|
||||||
|
return AppCommandOptionType.number
|
||||||
|
elif isinstance(self.value, str):
|
||||||
|
return AppCommandOptionType.string
|
||||||
|
else:
|
||||||
|
raise TypeError(
|
||||||
|
f'invalid Choice value type given, expected int, str, or float but received {self.value.__class__!r}'
|
||||||
|
)
|
||||||
|
|
||||||
def to_dict(self) -> ApplicationCommandOptionChoice:
|
def to_dict(self) -> ApplicationCommandOptionChoice:
|
||||||
return {
|
return {
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user