mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-21 16:24:14 +00:00
[commands] Allow Greedy to potentially maintain state between calls
This commit is contained in:
parent
686071814b
commit
c35ff4cfc6
@ -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
|
||||
|
@ -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…
x
Reference in New Issue
Block a user