Skip to content

Commit 1fe24c9

Browse files
sgtlaggykhazhyk
authored andcommitted
[commands] add CustomDefault.converters to allow implicit conversions
[khazhyk: rebased on latest and resolved conflicts in _get_converter]
1 parent fec34e0 commit 1fe24c9

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

discord/ext/commands/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,10 @@ async def do_conversion(self, ctx, converter, argument, param):
633633
def _get_converter(self, param):
634634
converter = param.annotation
635635
if converter is param.empty:
636-
if param.default is param.empty or param.default is None or (inspect.isclass(param.default) and issubclass(param.default, defaults.CustomDefault)) or isinstance(param.default, defaults.CustomDefault):
636+
if param.default is param.empty or param.default is None:
637637
converter = str
638+
elif (inspect.isclass(param.default) and issubclass(param.default, defaults.CustomDefault)) or isinstance(param.default, defaults.CustomDefault):
639+
converter = typing.Union[param.default.converters]
638640
else:
639641
converter = type(param.default)
640642
return converter

discord/ext/commands/default.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
DEALINGS IN THE SOFTWARE.
2525
"""
2626

27+
import discord
28+
2729
from .errors import MissingRequiredArgument
2830

2931
__all__ = (
@@ -49,9 +51,11 @@ def __str__(cls):
4951
class CustomDefault(metaclass=CustomDefaultMeta):
5052
"""The base class of custom defaults that require the :class:`.Context`.
5153
52-
Classes that derive from this should override the :meth:`~.CustomDefault.default`
53-
method to do its conversion logic. This method must be a coroutine.
54+
Classes that derive from this should override the :attr:`~.CustomDefault.converters` attribute to specify
55+
converters to use and the :meth:`~.CustomDefault.default` method to do its conversion logic.
56+
This method must be a coroutine.
5457
"""
58+
converters = (str,)
5559

5660
async def default(self, ctx, param):
5761
"""|coro|
@@ -72,12 +76,14 @@ async def default(self, ctx, param):
7276

7377
class Author(CustomDefault):
7478
"""Default parameter which returns the author for this context."""
79+
converters = (discord.Member, discord.User)
7580

7681
async def default(self, ctx, param):
7782
return ctx.author
7883

7984
class CurrentChannel(CustomDefault):
8085
"""Default parameter which returns the channel for this context."""
86+
converters = (discord.TextChannel,)
8187

8288
async def default(self, ctx, param):
8389
return ctx.channel

docs/ext/commands/commands.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ A DefaultParam returning ``None`` is valid - if this should be an error, raise :
656656
657657
class LastImage(CustomDefault):
658658
"""Default param which finds the last image in chat."""
659+
converters = (AnyImage,)
659660
660661
async def default(self, ctx, param):
661662
for attachment in message.attachments:
@@ -672,7 +673,7 @@ A DefaultParam returning ``None`` is valid - if this should be an error, raise :
672673
raise errors.MissingRequiredArgument(param)
673674
674675
@bot.command()
675-
async def echo_image(ctx, *, image: Image = LastImage):
676+
async def echo_image(ctx, *, image=LastImage):
676677
async with aiohttp.ClientSession() as sess:
677678
async with sess.get(image) as resp:
678679
resp.raise_for_status()

0 commit comments

Comments
 (0)