Browse Source

[commands] Allow Greedy to potentially maintain state between calls

pull/8484/head
Rapptz 3 years ago
parent
commit
c35ff4cfc6
  1. 11
      discord/ext/commands/converter.py
  2. 6
      discord/ext/commands/core.py

11
discord/ext/commands/converter.py

@ -1039,6 +1039,17 @@ class Greedy(List[T]):
return cls(converter=converter)
@property
def constructed_converter(self) -> Any:
# Only construct a converter once in order to maintain state between convert calls
if (
inspect.isclass(self.converter)
and issubclass(self.converter, Converter)
and not inspect.ismethod(self.converter.convert)
):
return self.converter()
return self.converter
if TYPE_CHECKING:
from typing_extensions import Annotated as Range

6
discord/ext/commands/core.py

@ -657,14 +657,14 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
return list(attachments)
if param.kind in (param.POSITIONAL_OR_KEYWORD, param.POSITIONAL_ONLY):
return await self._transform_greedy_pos(ctx, param, param.required, converter.converter)
return await self._transform_greedy_pos(ctx, param, param.required, converter.constructed_converter)
elif param.kind == param.VAR_POSITIONAL:
return await self._transform_greedy_var_pos(ctx, param, converter.converter)
return await self._transform_greedy_var_pos(ctx, param, converter.constructed_converter)
else:
# if we're here, then it's a KEYWORD_ONLY param type
# since this is mostly useless, we'll helpfully transform Greedy[X]
# into just X and do the parsing that way.
converter = converter.converter
converter = converter.constructed_converter
# Try to detect Optional[discord.Attachment] or discord.Attachment special converter
if converter is discord.Attachment:

Loading…
Cancel
Save