Skip to content

Commit

Permalink
[commands] Add displayed_name to commands.Parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Soheab committed Jun 11, 2023
1 parent d34a884 commit 94bf7d8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
12 changes: 10 additions & 2 deletions discord/ext/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def get_signature_parameters(
parameter._default = default.default
parameter._description = default._description
parameter._displayed_default = default._displayed_default
parameter._displayed_name = default._displayed_name

annotation = parameter.annotation

Expand Down Expand Up @@ -194,8 +195,13 @@ def extract_descriptions_from_docstring(function: Callable[..., Any], params: Di
description, param_docstring = divide
for match in NUMPY_DOCSTRING_ARG_REGEX.finditer(param_docstring):
name = match.group('name')

if name not in params:
continue
is_display_name = discord.utils.get(params.values(), displayed_name=name)
if is_display_name:
name = is_display_name.name
else:
continue

param = params[name]
if param.description is None:
Expand Down Expand Up @@ -1169,7 +1175,9 @@ def signature(self) -> str:
return ''

result = []
for name, param in params.items():
for param in params.values():
name = param.displayed_name or param.name

greedy = isinstance(param.converter, Greedy)
optional = False # postpone evaluation of if it's an optional argument

Expand Down
8 changes: 4 additions & 4 deletions discord/ext/commands/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class MissingRequiredArgument(UserInputError):

def __init__(self, param: Parameter) -> None:
self.param: Parameter = param
super().__init__(f'{param.name} is a required argument that is missing.')
super().__init__(f'{param.displayed_name or param.name} is a required argument that is missing.')


class MissingRequiredAttachment(UserInputError):
Expand All @@ -201,7 +201,7 @@ class MissingRequiredAttachment(UserInputError):

def __init__(self, param: Parameter) -> None:
self.param: Parameter = param
super().__init__(f'{param.name} is a required argument that is missing an attachment.')
super().__init__(f'{param.displayed_name or param.name} is a required argument that is missing an attachment.')


class TooManyArguments(UserInputError):
Expand Down Expand Up @@ -901,7 +901,7 @@ def _get_name(x):
else:
fmt = ' or '.join(to_string)

super().__init__(f'Could not convert "{param.name}" into {fmt}.')
super().__init__(f'Could not convert "{param.displayed_name or param.name}" into {fmt}.')


class BadLiteralArgument(UserInputError):
Expand Down Expand Up @@ -938,7 +938,7 @@ def __init__(self, param: Parameter, literals: Tuple[Any, ...], errors: List[Com
else:
fmt = ' or '.join(to_string)

super().__init__(f'Could not convert "{param.name}" into the literal {fmt}.')
super().__init__(f'Could not convert "{param.displayed_name or param.name}" into the literal {fmt}.')


class ArgumentParsingError(UserInputError):
Expand Down
2 changes: 1 addition & 1 deletion discord/ext/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ def add_command_arguments(self, command: Command[Any, ..., Any], /) -> None:

get_width = discord.utils._string_width
for argument in arguments:
name = argument.name
name = argument.displayed_name or argument.name
width = max_size - (get_width(name) - len(name))
entry = f'{self.indent * " "}{name:<{width}} {argument.description or self.default_argument_description}'
# we do not want to shorten the default value, if any.
Expand Down
27 changes: 24 additions & 3 deletions discord/ext/commands/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Parameter(inspect.Parameter):
.. versionadded:: 2.0
"""

__slots__ = ('_displayed_default', '_description', '_fallback')
__slots__ = ('_displayed_default', '_description', '_fallback', '_displayed_name')

def __init__(
self,
Expand All @@ -97,6 +97,7 @@ def __init__(
annotation: Any = empty,
description: str = empty,
displayed_default: str = empty,
displayed_name: str = empty,
) -> None:
super().__init__(name=name, kind=kind, default=default, annotation=annotation)
self._name = name
Expand All @@ -106,6 +107,7 @@ def __init__(
self._annotation = annotation
self._displayed_default = displayed_default
self._fallback = False
self._displayed_name = displayed_name

def replace(
self,
Expand All @@ -116,6 +118,7 @@ def replace(
annotation: Any = MISSING,
description: str = MISSING,
displayed_default: Any = MISSING,
displayed_name: Any = MISSING,
) -> Self:
if name is MISSING:
name = self._name
Expand All @@ -129,6 +132,8 @@ def replace(
description = self._description
if displayed_default is MISSING:
displayed_default = self._displayed_default
if displayed_name is MISSING:
displayed_name = self._displayed_name

return self.__class__(
name=name,
Expand All @@ -137,6 +142,7 @@ def replace(
annotation=annotation,
description=description,
displayed_default=displayed_default,
displayed_name=displayed_name,
)

if not TYPE_CHECKING: # this is to prevent anything breaking if inspect internals change
Expand Down Expand Up @@ -171,6 +177,14 @@ def displayed_default(self) -> Optional[str]:

return None if self.required else str(self.default)

@property
def displayed_name(self) -> Optional[str]:
"""Optional[:class:`str`]: The name that is displayed to the user.
.. versionadded:: 2.3
"""
return self._displayed_name if self._displayed_name is not empty else None

async def get_default(self, ctx: Context[Any]) -> Any:
"""|coro|
Expand All @@ -193,8 +207,9 @@ def parameter(
default: Any = empty,
description: str = empty,
displayed_default: str = empty,
displayed_name: str = empty,
) -> Any:
r"""parameter(\*, converter=..., default=..., description=..., displayed_default=...)
r"""parameter(\*, converter=..., default=..., description=..., displayed_default=..., displayed_name=...)
A way to assign custom metadata for a :class:`Command`\'s parameter.
Expand All @@ -221,6 +236,10 @@ async def wave(ctx, to: discord.User = commands.parameter(default=lambda ctx: ct
The description of this parameter.
displayed_default: :class:`str`
The displayed default in :attr:`Command.signature`.
displayed_name: :class:`str`
The name that is displayed to the user.
.. versionadded:: 2.3
"""
return Parameter(
name='empty',
Expand All @@ -229,6 +248,7 @@ async def wave(ctx, to: discord.User = commands.parameter(default=lambda ctx: ct
default=default,
description=description,
displayed_default=displayed_default,
displayed_name=displayed_name,
)


Expand All @@ -240,12 +260,13 @@ def __call__(
default: Any = empty,
description: str = empty,
displayed_default: str = empty,
displayed_name: str = empty,
) -> Any:
...


param: ParameterAlias = parameter
r"""param(\*, converter=..., default=..., description=..., displayed_default=...)
r"""param(\*, converter=..., default=..., description=..., displayed_default=..., displayed_name=...)
An alias for :func:`parameter`.
Expand Down

0 comments on commit 94bf7d8

Please sign in to comment.