mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-02 00:00:02 +00:00
Validate Option names similar to slash command names
This commit is contained in:
parent
5096846c4e
commit
6ef2043b10
@ -181,9 +181,20 @@ class Choice(Generic[ChoiceT]):
|
|||||||
__slots__ = ('name', 'value')
|
__slots__ = ('name', 'value')
|
||||||
|
|
||||||
def __init__(self, *, name: str, value: ChoiceT):
|
def __init__(self, *, name: str, value: ChoiceT):
|
||||||
self.name: str = name
|
# Circular import avoidance
|
||||||
|
from .commands import validate_name
|
||||||
|
|
||||||
|
self.name: str = validate_name(name)
|
||||||
self.value: ChoiceT = value
|
self.value: ChoiceT = value
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_dict(cls, data: ApplicationCommandOptionChoice) -> Choice[Union[int, float, str]]:
|
||||||
|
# This is to avoid the validation check on Discord provided payloads.
|
||||||
|
self = cls.__new__(cls)
|
||||||
|
self.name = data['name']
|
||||||
|
self.value = data['value']
|
||||||
|
return self # type: ignore # Trick type checker to widen ChoiceT to Union
|
||||||
|
|
||||||
def __eq__(self, o: object) -> bool:
|
def __eq__(self, o: object) -> bool:
|
||||||
return isinstance(o, Choice) and self.name == o.name and self.value == o.value
|
return isinstance(o, Choice) and self.name == o.name and self.value == o.value
|
||||||
|
|
||||||
@ -542,9 +553,7 @@ class Argument:
|
|||||||
self.name: str = data['name']
|
self.name: str = data['name']
|
||||||
self.description: str = data['description']
|
self.description: str = data['description']
|
||||||
self.required: bool = data.get('required', False)
|
self.required: bool = data.get('required', False)
|
||||||
self.choices: List[Choice[Union[int, float, str]]] = [
|
self.choices: List[Choice[Union[int, float, str]]] = [Choice.from_dict(d) for d in data.get('choices', [])]
|
||||||
Choice(name=d['name'], value=d['value']) for d in data.get('choices', [])
|
|
||||||
]
|
|
||||||
|
|
||||||
def to_dict(self) -> ApplicationCommandOption:
|
def to_dict(self) -> ApplicationCommandOption:
|
||||||
return {
|
return {
|
||||||
@ -606,9 +615,7 @@ class AppCommandGroup:
|
|||||||
self.name: str = data['name']
|
self.name: str = data['name']
|
||||||
self.description: str = data['description']
|
self.description: str = data['description']
|
||||||
self.required: bool = data.get('required', False)
|
self.required: bool = data.get('required', False)
|
||||||
self.choices: List[Choice[Union[int, float, str]]] = [
|
self.choices: List[Choice[Union[int, float, str]]] = [Choice.from_dict(d) for d in data.get('choices', [])]
|
||||||
Choice(name=d['name'], value=d['value']) for d in data.get('choices', [])
|
|
||||||
]
|
|
||||||
self.arguments: List[Argument] = [
|
self.arguments: List[Argument] = [
|
||||||
Argument(parent=self, state=self._state, data=d)
|
Argument(parent=self, state=self._state, data=d)
|
||||||
for d in data.get('options', [])
|
for d in data.get('options', [])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user