You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Have a FlagConverter subclass, with a flag whose default value is a callable
classTimestampArgs(commands.FlagConverter):
year: Optional[int] =commands.flag(
name="year",
default=lambdactx: discord.utils.utcnow().year, # default value is a callablemax_args=1,
)
Use this in a hybrid command (this example is in a cog)
importdiscordfromdiscord.extimportcommandsclassTimestampArgs(commands.FlagConverter):
year: Optional[int] =commands.flag(
name="year",
default=lambdactx: discord.utils.utcnow().year, # default value is a callablemax_args=1,
)
classTestCog(commands.Cog): # It is not necessary for this to be in a cog. But this is how I have it in my code.def__init__(self, bot):
self.bot=bot@commands.hybrid_command()asyncdeftimestamp(self, ctx, *, args: TimestampArgs):
awaitctx.send(f"year={args.year}")
asyncdefsetup(bot: commands.Bot):
awaitbot.add_cog(TestCog(bot))
Expected Results
Expected result is that it lets me load the code and call the default function to set args.year to the current year (.utcnow().year) for both message and application commands.
Oh something I should note is that when I use a custom converter (Optional[CustomConverter]) for the flag instead of int (Optional[int]), it doesn't raise the error but it only successfully calls the function to set the value when used as message command, while leaving it as a function when used as application command:
Summary
When using command flags in a hybrid command, if a flag has a callable as its default value, it raises a
TypeError: invalid default parameter type given (<class 'function'>), expected (<class 'int'>, <class 'NoneType'>)
errorReproduction Steps
Minimal Reproducible Code
Expected Results
Expected result is that it lets me load the code and call the default function to set
args.year
to the current year (.utcnow().year
) for both message and application commands.This works fine when used only in a message command. But fails when used in a hybrid command. I believe that the reason for this is because the default value is checked to be callable or not (and called if so) for ext.commands.Command parameters (or perhaps FlagConverter.convert) but not for app_command.Command transformers
Actual Results
TypeError is raised when trying to load the code
Full traceback: https://mystb.in/FavoritesPasoScore
Intents
discord.Intents.all()
System Information
Checklist
Additional Context
I can create a PR for this if this is in fact a bug and not user error and if the location I showed above is in fact the place where the fix goes.
The text was updated successfully, but these errors were encountered: