Skip to content
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

Callable default value for flags not working in hybrid commands #9641

Open
3 tasks done
WitherredAway opened this issue Nov 7, 2023 · 1 comment
Open
3 tasks done
Labels
unconfirmed bug A bug report that needs triaging

Comments

@WitherredAway
Copy link

WitherredAway commented Nov 7, 2023

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'>) error

Reproduction Steps

  1. Have a FlagConverter subclass, with a flag whose default value is a callable
class TimestampArgs(commands.FlagConverter):
    year: Optional[int] = commands.flag(
        name="year",
        default=lambda ctx: discord.utils.utcnow().year,  # default value is a callable
        max_args=1,
    )
  1. Use this in a hybrid command (this example is in a cog)
    @commands.hybrid_command()
    async def timestamp(self, ctx, *, args: TimestampArgs):
        await ctx.send(f"year={args.year}")
  1. Error is raised when trying to load this code

Minimal Reproducible Code

import discord
from discord.ext import commands

class TimestampArgs(commands.FlagConverter):
    year: Optional[int] = commands.flag(
        name="year",
        default=lambda ctx: discord.utils.utcnow().year,  # default value is a callable
        max_args=1,
    )

class TestCog(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()
    async def timestamp(self, ctx, *, args: TimestampArgs):
        await ctx.send(f"year={args.year}")


async def setup(bot: commands.Bot):
    await bot.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.

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

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ExtensionFailed: Extension 'cogs.bot' raised an error: TypeError: invalid default parameter type given (<class 'function'>), expected (<class 'int'>, <class 'NoneType'>)

Full traceback: https://mystb.in/FavoritesPasoScore

Intents

discord.Intents.all()

System Information

  • Python v3.11.4-final
  • discord.py v2.3.0-final
  • aiohttp v3.8.3
  • system info: Linux 5.15.90.1-microsoft-standard-WSL2 #​1 SMP Fri Jan 27 02:56:13 UTC 2023

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

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.

@WitherredAway WitherredAway added the unconfirmed bug A bug report that needs triaging label Nov 7, 2023
@WitherredAway
Copy link
Author

WitherredAway commented Nov 7, 2023

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:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unconfirmed bug A bug report that needs triaging
Projects
None yet
Development

No branches or pull requests

1 participant