Skip to content

Commit 00fdb05

Browse files
committed
Fix #512
1 parent 36d2cd5 commit 00fdb05

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

discord/commands/commands.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29-
import types
3029
import functools
3130
import inspect
31+
import re
32+
import types
3233
from collections import OrderedDict
3334
from typing import Any, Callable, Dict, List, Optional, Union, TYPE_CHECKING
3435

36+
from .context import ApplicationContext, AutocompleteContext
37+
from .errors import ApplicationCommandError, CheckFailure, ApplicationCommandInvokeError
38+
from .permissions import Permission
3539
from ..enums import SlashCommandOptionType, ChannelType
40+
from ..errors import ValidationError, ClientException
3641
from ..member import Member
37-
from ..user import User
3842
from ..message import Message
39-
from .context import ApplicationContext, AutocompleteContext
43+
from ..user import User
4044
from ..utils import find, get_or_fetch, async_all
41-
from ..errors import ValidationError, ClientException
42-
from .errors import ApplicationCommandError, CheckFailure, ApplicationCommandInvokeError
43-
from .permissions import Permission
4445

4546
__all__ = (
4647
"_BaseCommand",
@@ -60,8 +61,8 @@
6061
"MessageCommand",
6162
)
6263

63-
if TYPE_CHECKING:
64-
from ..interactions import Interaction
64+
if TYPE_CHECKING:
65+
pass
6566

6667
def wrap_callback(coro):
6768
@functools.wraps(coro)
@@ -459,6 +460,9 @@ def _parse_options(self, params) -> List[Option]:
459460
option.name = p_name
460461
option._parameter_name = p_name
461462

463+
validate_chat_input_name(option.name)
464+
validate_chat_input_description(option.description)
465+
462466
final_options.append(option)
463467

464468
return final_options
@@ -640,7 +644,7 @@ def __init__(
640644
minmax_types = (int, float, type(None))
641645
else:
642646
minmax_types = (type(None),)
643-
minmax_typehint = Optional[Union[minmax_types]] # type: ignore
647+
minmax_typehint = Optional[Union[minmax_types]] # type: ignore
644648

645649
self.min_value: minmax_typehint = kwargs.pop("min_value", None)
646650
self.max_value: minmax_typehint = kwargs.pop("max_value", None)
@@ -1123,24 +1127,33 @@ def command(**kwargs):
11231127
"""
11241128
return application_command(**kwargs)
11251129

1130+
1131+
docs = "https://discord.com/developers/docs"
1132+
1133+
11261134
# Validation
11271135
def validate_chat_input_name(name: Any):
1136+
# Must meet the regex ^[\w-]{1,32}$
11281137
if not isinstance(name, str):
1129-
raise TypeError("Name of a command must be a string.")
1130-
if " " in name:
1131-
raise ValidationError("Name of a chat input command cannot have spaces.")
1132-
if not name.islower():
1133-
raise ValidationError("Name of a chat input command must be lowercase.")
1134-
if len(name) > 32 or len(name) < 1:
1138+
raise TypeError(f"Chat input command names and options must be of type str. Recieved {name}")
1139+
if not re.match(r"^[\w-]{1,32}$", name):
1140+
raise ValidationError(
1141+
r'Chat input command names and options must follow the regex "^[\w-]{1,32}$". For more information, see '
1142+
f"{docs}/interactions/application-commands#application-command-object-application-command-naming. Recieved "
1143+
f"{name}"
1144+
)
1145+
if not 1 <= len(name) <= 32:
11351146
raise ValidationError(
1136-
"Name of a chat input command must be less than 32 characters and non empty."
1147+
f"Chat input command names and options must be 1-32 characters long. Recieved {name}"
11371148
)
1149+
if not name.lower() == name: # Can't use islower() as it fails if none of the chars can be lower. See #512.
1150+
raise ValidationError(f"Chat input command names and options must be lowercase. Recieved {name}")
11381151

11391152

11401153
def validate_chat_input_description(description: Any):
11411154
if not isinstance(description, str):
1142-
raise TypeError("Description of a command must be a string.")
1143-
if len(description) > 100 or len(description) < 1:
1155+
raise TypeError(f"Command description must be of type str. Recieved {description}")
1156+
if not 1 <= len(description) <= 100:
11441157
raise ValidationError(
1145-
"Description of a chat input command must be less than 100 characters and non empty."
1158+
f"Command description must be 1-100 characters long. Recieved {description}"
11461159
)

0 commit comments

Comments
 (0)