Skip to content

Commit a266a02

Browse files
magistaukhazhyk
authored andcommitted
[commands] add default __repr__ for CustomDefault
With this change CustomDefaults display nicer in help commands and in command.signature
1 parent 316eb45 commit a266a02

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

discord/ext/commands/default.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@
3434
'Call',
3535
)
3636

37-
class CustomDefault:
37+
class CustomDefaultMeta(type):
38+
def __new__(cls, *args, **kwargs):
39+
name, bases, attrs = args
40+
attrs['display'] = kwargs.pop('display', name)
41+
return super().__new__(cls, name, bases, attrs, **kwargs)
42+
43+
def __repr__(cls):
44+
return str(cls)
45+
46+
def __str__(cls):
47+
return cls.display
48+
49+
class CustomDefault(metaclass=CustomDefaultMeta):
3850
"""The base class of custom defaults that require the :class:`.Context`.
3951
4052
Classes that derive from this should override the :meth:`~.CustomDefault.default`

docs/ext/commands/commands.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ A DefaultParam returning ``None`` is valid - if this should be an error, raise :
640640
my_bytes = io.BytesIO(await resp.content.read())
641641
await ctx.send(file=discord.File(filename="your_image", fp=my_bytes))
642642
643+
.. tip:: You can change the name of a Custom Default that is displayed in help command by passing ``display`` meta option. Using previous example: ``class LastImage(CustomDefault, display='last image from chat')``
644+
645+
646+
643647

644648
Checks
645649
-------

0 commit comments

Comments
 (0)