[commands] Allow converters to be instantiated.
This allows for you to create converters that can have varying behaviour using the converter's __init__ instead of having to do a meta-class based approach to get around the fact that __init__ is part of the interface. To make up for the lack of __init__, a new method Converter.prepare was added to do the work that __init__ used to do.
This commit is contained in:
@@ -203,11 +203,15 @@ class Command:
|
||||
converter = getattr(converters, converter.__name__ + 'Converter')
|
||||
|
||||
if inspect.isclass(converter) and issubclass(converter, converters.Converter):
|
||||
instance = converter(ctx, argument)
|
||||
if asyncio.iscoroutinefunction(instance.convert):
|
||||
return (yield from instance.convert())
|
||||
else:
|
||||
return instance.convert()
|
||||
instance = converter()
|
||||
instance.prepare(ctx, argument)
|
||||
ret = yield from discord.utils.maybe_coroutine(instance.convert)
|
||||
return ret
|
||||
|
||||
if isinstance(converter, converters.Converter):
|
||||
converter.prepare(ctx, argument)
|
||||
ret = yield from discord.utils.maybe_coroutine(converter.convert)
|
||||
return ret
|
||||
|
||||
return converter(argument)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user