From edd8f6772b66b3f67b62f6231bea80155eab3685 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sat, 16 Apr 2022 19:47:54 -0400 Subject: [PATCH 01/10] More l10 fixes --- discord/commands/core.py | 49 ++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index e63906117a..978568e031 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -146,6 +146,20 @@ def unwrap_function(function: Callable[..., Any]) -> Callable[..., Any]: return function +def _validate_names(obj): + validate_chat_input_name(obj.name) + if obj.name_localizations: + for locale, string in obj.name_localizations.items(): + validate_chat_input_name(string, locale=locale) + + +def _validate_descriptions(obj): + validate_chat_input_description(obj.description) + if obj.description_localizations: + for locale, string in obj.description_localizations.items(): + validate_chat_input_description(string, locale=locale) + + class _BaseCommand: __slots__ = () @@ -601,21 +615,16 @@ def __init__(self, func: Callable, *args, **kwargs) -> None: raise TypeError("Callback must be a coroutine.") self.callback = func - validate_chat_input_name(self.name) self.name_localizations: Optional[Dict[str, str]] = kwargs.get("name_localizations", None) - if self.name_localizations: - for locale, string in self.name_localizations.items(): - validate_chat_input_name(string, locale=locale) + _validate_names(self) description = kwargs.get("description") or ( inspect.cleandoc(func.__doc__).splitlines()[0] if func.__doc__ is not None else "No description provided" ) - validate_chat_input_description(description) + self.description: str = description self.description_localizations: Optional[Dict[str, str]] = kwargs.get("description_localizations", None) - if self.description_localizations: - for locale, string in self.description_localizations.items(): - validate_chat_input_description(string, locale=locale) + _validate_descriptions(self) self.attached_to_group: bool = False @@ -686,8 +695,8 @@ def _parse_options(self, params) -> List[Option]: option.name = p_name option._parameter_name = p_name - validate_chat_input_name(option.name) - validate_chat_input_description(option.description) + _validate_names(option) + _validate_descriptions(option) final_options.append(option) @@ -715,8 +724,8 @@ def _match_option_param_names(self, params, options): lambda o, a: inspect.isclass(a) and issubclass(a, o._raw_type), # 'normal' types ] for o in options: - validate_chat_input_name(o.name) - validate_chat_input_description(o.description) + _validate_names(o) + _validate_descriptions(o) try: p_name, p_obj = next(params) except StopIteration: # not enough params for all the options @@ -1610,18 +1619,18 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): # Must meet the regex ^[\w-]{1,32}$ if locale not in valid_locales and locale is not None: raise ValidationError( - f"Locale {locale} is not a valid locale, in command names, " + f"Locale '{locale}' is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) if not isinstance(name, str): raise TypeError( - f"Chat input command names and options must be of type str." f"Received {name}" + f" in locale {locale}" + f"Command names and options must be of type str." f"Received {name}" + f" in locale {locale}" if locale else "" ) if not re.match(r"^[\w-]{1,32}$", name): raise ValidationError( - "Chat input command names and options must follow the regex " + "Command names and options must follow the regex " r'"^[\w-]{1,32}$". For more information, see ' f"{docs}/interactions/application-commands#application-command-object-application-command-naming. " f"Received {name}" + f" in locale {locale}" @@ -1630,14 +1639,14 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): ) if not 1 <= len(name) <= 32: raise ValidationError( - "Chat input command names and options must be 1-32 characters long. " + "Command names and options must be 1-32 characters long. " f"Received {name}" + f" in locale {locale}" if locale else "" ) if not name.lower() == name: # Can't use islower() as it fails if none of the chars can be lower. See #512. raise ValidationError( - "Chat input command names and options must be lowercase. " f"Received {name}" + f" in locale {locale}" + "Command names and options must be lowercase. " f"Received {name}" + f" in locale {locale}" if locale else "" ) @@ -1646,18 +1655,18 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): def validate_chat_input_description(description: Any, locale: Optional[str] = None): if locale not in valid_locales and locale is not None: raise ValidationError( - f"Locale {locale} is not a valid locale, in command descriptions, " + f"Locale {locale} is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) if not isinstance(description, str): raise TypeError( - f"Command description must be of type str. Received {description} " + f" in locale {locale}" + f"Command and option description must be of type str. Received {description} " + f" in locale {locale}" if locale else "" ) if not 1 <= len(description) <= 100: raise ValidationError( - "Command description must be 1-100 characters long. " f"Received {description}" + f" in locale {locale}" + "Command and option description must be 1-100 characters long. " f"Received {description}" + f" in locale {locale}" if locale else "" ) From b13851800cd91b7ddf619ca17be454e6d251a894 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sat, 16 Apr 2022 20:25:43 -0400 Subject: [PATCH 02/10] missing quotes --- discord/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 978568e031..1f17a6770e 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1655,7 +1655,7 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): def validate_chat_input_description(description: Any, locale: Optional[str] = None): if locale not in valid_locales and locale is not None: raise ValidationError( - f"Locale {locale} is not a valid locale, " + f"Locale '{locale}' is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) if not isinstance(description, str): From d353396ae8d365a778b2c803945c87f0dd793caa Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 18 Apr 2022 00:04:30 +0200 Subject: [PATCH 03/10] Update discord/commands/core.py Co-authored-by: krittick --- discord/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 1f17a6770e..b495cbe623 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1624,7 +1624,7 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): ) if not isinstance(name, str): raise TypeError( - f"Command names and options must be of type str." f"Received {name}" + f" in locale {locale}" + f"Command names and options must be of type str." f"Received {name} in locale {locale}" if locale else "" ) From 9098f45a0ba3c80dd9899abfa7312d39088ba092 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 18 Apr 2022 00:04:43 +0200 Subject: [PATCH 04/10] Update discord/commands/core.py Co-authored-by: krittick --- discord/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index b495cbe623..89e8f8a98e 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1646,7 +1646,7 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): ) if not name.lower() == name: # Can't use islower() as it fails if none of the chars can be lower. See #512. raise ValidationError( - "Command names and options must be lowercase. " f"Received {name}" + f" in locale {locale}" + f"Command names and options must be lowercase. Received {name} in locale {locale}" if locale else "" ) From bfb75031b8ea367f7bba643a59eb6f1b368324a9 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 18 Apr 2022 00:04:54 +0200 Subject: [PATCH 05/10] Update discord/commands/core.py Co-authored-by: krittick --- discord/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 89e8f8a98e..1acd5a6801 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1640,7 +1640,7 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): if not 1 <= len(name) <= 32: raise ValidationError( "Command names and options must be 1-32 characters long. " - f"Received {name}" + f" in locale {locale}" + f"Received {name} in locale {locale}" if locale else "" ) From 11ce3397b3ef578172a73539d460e77d929d9a27 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 18 Apr 2022 00:05:05 +0200 Subject: [PATCH 06/10] Update discord/commands/core.py Co-authored-by: krittick --- discord/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 1acd5a6801..efae8e4dfc 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1660,7 +1660,7 @@ def validate_chat_input_description(description: Any, locale: Optional[str] = No ) if not isinstance(description, str): raise TypeError( - f"Command and option description must be of type str. Received {description} " + f" in locale {locale}" + f"Command and option description must be of type str. Received {description} in locale {locale}" if locale else "" ) From d934f7fbb8b019c83a95e959dad7a1a44e1ccc8e Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 18 Apr 2022 00:05:26 +0200 Subject: [PATCH 07/10] Update discord/commands/core.py Co-authored-by: krittick --- discord/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index efae8e4dfc..f7f4448400 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1666,7 +1666,7 @@ def validate_chat_input_description(description: Any, locale: Optional[str] = No ) if not 1 <= len(description) <= 100: raise ValidationError( - "Command and option description must be 1-100 characters long. " f"Received {description}" + f" in locale {locale}" + "Command and option description must be 1-100 characters long. " f"Received {description} in locale {locale}" if locale else "" ) From aa4fade032cd780141b7475fcd90d34391c99343 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sun, 17 Apr 2022 19:28:28 -0400 Subject: [PATCH 08/10] apply code suggestions Co-Authored-By: krittick <935885+krittick@users.noreply.github.com> --- discord/commands/core.py | 62 ++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index f7f4448400..5c1c5ea2e3 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1617,56 +1617,50 @@ def command(**kwargs): # Validation def validate_chat_input_name(name: Any, locale: Optional[str] = None): # Must meet the regex ^[\w-]{1,32}$ - if locale not in valid_locales and locale is not None: + if locale is not None and locale not in valid_locales: raise ValidationError( f"Locale '{locale}' is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) if not isinstance(name, str): - raise TypeError( - f"Command names and options must be of type str." f"Received {name} in locale {locale}" - if locale - else "" - ) + msg = f"Command names and options must be of type str. Received {name}" + if locale: + msg += f" in locale {locale}" + raise TypeError(msg) if not re.match(r"^[\w-]{1,32}$", name): - raise ValidationError( - "Command names and options must follow the regex " - r'"^[\w-]{1,32}$". For more information, see ' + msg = ( + r'Command names and options must follow the regex "^[\w-]{1,32}$". For more information, see ' f"{docs}/interactions/application-commands#application-command-object-application-command-naming. " - f"Received {name}" + f" in locale {locale}" - if locale - else "" + f"Received {name}" ) + if locale: + msg += f" in locale {locale}" + raise ValidationError(msg) if not 1 <= len(name) <= 32: - raise ValidationError( - "Command names and options must be 1-32 characters long. " - f"Received {name} in locale {locale}" - if locale - else "" - ) + msg = f"Command names and options must be 1-32 characters long. Received {name}" + if locale: + msg += f" in locale {locale}" + raise ValidationError(msg) if not name.lower() == name: # Can't use islower() as it fails if none of the chars can be lower. See #512. - raise ValidationError( - f"Command names and options must be lowercase. Received {name} in locale {locale}" - if locale - else "" - ) + msg = f"Command names and options must be lowercase. Received {name}" + if locale: + msg += f" in locale {locale}" + raise ValidationError(msg) def validate_chat_input_description(description: Any, locale: Optional[str] = None): - if locale not in valid_locales and locale is not None: + if locale is not None and locale not in valid_locales: raise ValidationError( f"Locale '{locale}' is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) if not isinstance(description, str): - raise TypeError( - f"Command and option description must be of type str. Received {description} in locale {locale}" - if locale - else "" - ) + msg = f"Command and option description must be of type str. Received {description}" + if locale: + msg += f" in locale {locale}" + raise TypeError(msg) if not 1 <= len(description) <= 100: - raise ValidationError( - "Command and option description must be 1-100 characters long. " f"Received {description} in locale {locale}" - if locale - else "" - ) + msg = f"Command and option description must be 1-100 characters long. Received {description}" + if locale: + msg += f" in locale {locale}" + raise ValidationError(msg) From 085ad600d6dcb21e6491e27d22041d5d891f82d6 Mon Sep 17 00:00:00 2001 From: Middledot Date: Mon, 18 Apr 2022 14:08:25 -0400 Subject: [PATCH 09/10] refactor validation seems to work --- discord/commands/core.py | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 5c1c5ea2e3..c60172d48a 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1622,30 +1622,24 @@ def validate_chat_input_name(name: Any, locale: Optional[str] = None): f"Locale '{locale}' is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) + error = None if not isinstance(name, str): - msg = f"Command names and options must be of type str. Received {name}" - if locale: - msg += f" in locale {locale}" - raise TypeError(msg) - if not re.match(r"^[\w-]{1,32}$", name): - msg = ( - r'Command names and options must follow the regex "^[\w-]{1,32}$". For more information, see ' + error = TypeError(f"Command names and options must be of type str. Received \"{name}\"") + elif not re.match(r"^[\w-]{1,32}$", name): + error = ValidationError( + r"Command names and options must follow the regex \"^[\w-]{1,32}$\". For more information, see " f"{docs}/interactions/application-commands#application-command-object-application-command-naming. " - f"Received {name}" + f"Received \"{name}\"" ) + elif not 1 <= len(name) <= 32: + error = ValidationError(f"Command names and options must be 1-32 characters long. Received \"{name}\"") + elif not name.lower() == name: # Can't use islower() as it fails if none of the chars can be lower. See #512. + error = ValidationError(f"Command names and options must be lowercase. Received \"{name}\"") + + if error: if locale: - msg += f" in locale {locale}" - raise ValidationError(msg) - if not 1 <= len(name) <= 32: - msg = f"Command names and options must be 1-32 characters long. Received {name}" - if locale: - msg += f" in locale {locale}" - raise ValidationError(msg) - if not name.lower() == name: # Can't use islower() as it fails if none of the chars can be lower. See #512. - msg = f"Command names and options must be lowercase. Received {name}" - if locale: - msg += f" in locale {locale}" - raise ValidationError(msg) + error.args = (error.args[0]+f" in locale {locale}",) + raise error def validate_chat_input_description(description: Any, locale: Optional[str] = None): @@ -1654,13 +1648,13 @@ def validate_chat_input_description(description: Any, locale: Optional[str] = No f"Locale '{locale}' is not a valid locale, " f"see {docs}/reference#locales for list of supported locales." ) + error = None if not isinstance(description, str): msg = f"Command and option description must be of type str. Received {description}" - if locale: msg += f" in locale {locale}" raise TypeError(msg) if not 1 <= len(description) <= 100: msg = f"Command and option description must be 1-100 characters long. Received {description}" if locale: - msg += f" in locale {locale}" - raise ValidationError(msg) + error.args = (error.args[0]+f" in locale {locale}",) + raise error From 57aa1196fcdbd1753904e5e3086bd9d158e0c415 Mon Sep 17 00:00:00 2001 From: Middledot Date: Mon, 18 Apr 2022 14:11:29 -0400 Subject: [PATCH 10/10] Missing code change ?? --- discord/commands/core.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index c60172d48a..3dd2185206 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1650,11 +1650,11 @@ def validate_chat_input_description(description: Any, locale: Optional[str] = No ) error = None if not isinstance(description, str): - msg = f"Command and option description must be of type str. Received {description}" - msg += f" in locale {locale}" - raise TypeError(msg) - if not 1 <= len(description) <= 100: - msg = f"Command and option description must be 1-100 characters long. Received {description}" + error = TypeError(f"Command and option description must be of type str. Received \"{description}\"") + elif not 1 <= len(description) <= 100: + error = ValidationError(f"Command and option description must be 1-100 characters long. Received \"{description}\"") + + if error: if locale: error.args = (error.args[0]+f" in locale {locale}",) raise error