-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] [commands] custom default arguments #1849
Conversation
89f537a
to
9ef70d0
Compare
@Rapptz any update/statement on this? Would make some discord.py code far prettier and seems a not-breaking change. async def my_command(ctx, user: discord.Member=None):
user = user or ctx.author which is ugly |
I might have time to finish this up this week/end |
9ef70d0
to
e95c42e
Compare
b36e013
to
79c8cc7
Compare
Would appreciate others testing and comments. |
4540528
to
b113c8c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the job and is a great code readability enhancement
I may have found a bug? from discord.ext.commands.default import Author
@bot.command()
async def testme(ctx, thing: discord.Member = Author):
await ctx.send(thing.joined_at) This raises |
I can't reproduce ( see https://canary.discordapp.com/channels/336642139381301249/381963689470984203/574499828172587038 and following) - the behavior your experiencing seems like the command framework isn't evaluating the customdefaults, does this behavior persist past restart? |
Ah - ctx.invoke() is what isn't working ctx.invoke doesn't call converters at all - so also won't resolve defaults. what you probably want is |
Was an issue on my end - the code proposed in the PR works fine. Thanks! |
Pushed an update to fix a missing |
I also found this to look weird on Is there a way to prettify that or do I have to write my own signature generator? |
The classes should probably define their own |
Yea, I know that. Not sure if they want to write a new repr for the "prepacked" defaults and what it would be, though. |
Does the default HelpCommand already have special casing for Converter? It would make sense to treat it similarly to Converter (which does not override repr just for HelpCommand) |
Type hints (including converters) are not displayed in help lol |
I tested your example from docs and there are some name errors |
I don't like the duplicate name for I don't currently know how or if it would even make sense to attach the defaults to said class, but a simple fix could be to rename them to |
Attaching the defaults to the classes would be far worse IMO, as you'd end up with signatures like "x: discord.Guild = discord.Guild" which to me.is very confusing. I think it would be better to rename them to Current* yes. |
e62b715
to
d774448
Compare
Doing |
d774448
to
a266a02
Compare
Because it needs to be reviewed. The only tag there is my dude |
Is this dead? |
It’s still waiting for review |
Because it has been 1 year |
a266a02
to
be20701
Compare
Rebased this. I recall some people claimed issues between customdefaults and some of the newer argument types (greedy, etc.). Is that the case/still the case? Is there an example that behaves weirdly? |
Let me test the code. |
Just a suggestion, instead of |
I got an error when trying to use Greedy with this. @khazhyk Ignoring exception in command my_command:
Traceback (most recent call last):
File "F:\PyCharm Python Works\OpenCityBot\discord.py\discord\ext\commands\core.py", line 86, in wrapped
ret = await coro(*args, **kwargs)
File "F:\PyCharm Python Works\OpenCityBot\discord.py\test.py", line 14, in my_command
await ctx.send(member.display_name)
AttributeError: type object 'Author' has no attribute 'display_name' Here's the code I used. @bot.command()
async def my_command(ctx, member: commands.Greedy[discord.Member] = Author, channel: discord.TextChannel = CurrentChannel, guild = CurrentGuild):
await ctx.send(member.display_name)
await ctx.send(channel.name)
await ctx.send(guild.name) Without Greedy it works super fine, so, check why the Greedy isn't working, I'll check the others as well, like Optional and Channel. Tested To fix:
|
The reason I went for "Author" instead of "CurrentAuthor" is that Author I think should be unambiguous, vs. Channel/Guild, which would be ambiguous. And stuff like CurrentUser also could be misunderstood. So CurrentAuthor seems too wordy, imo, but does look slightly more consistent. I'm indifferent, do others have thoughts on this? |
be20701
to
9677dcb
Compare
Modeled slightly after Converters, allow specifying Converter-like class for context-based default parameters. e.g. ```py class Author(CustomDefault): async def default(self, ctx): return ctx.author async def my_command(ctx, user: discord.Member=Author): ... ``` Also adds a few common cases (Author, Channel, Guild) for current author, ...
With this change CustomDefaults display nicer in help commands and in command.signature
[khazhyk: rebased on latest and resolved conflicts in _get_converter]
[khazhyk: removed list handling, adjusted message]
9677dcb
to
40c234c
Compare
@sairam4123 figured out and fixed the issue with Greedy, thanks! That should work now. |
I also added a commit from @sgtlaggy which I thought was useful, although there could be some bikeshedding to do here. In particular, I'm not so sure that having a member variable to override is the best api. one option, which I don't think works in this current PR, is if the defaultargument was both a converter and a default, then detect if the default is (also) a converter, and use that, e.g. class MyDefault(DefaultArgument, MyConverter):
... |
If we are going to add this couldn't we support attachment with ext.commands? |
Sorry, as evidenced by my lack of touching this for a while, I'm not really motivated to push this to completion. I still think something like this is a good idea, and if anyone else wants to implement something like this (or polish this code up for the lib), I would be happy to see it. |
Modeled slightly after Converters, allow specifying Converter-like class
for context-based default parameters. e.g.