Search code examples
pythonpycord

TypeError: (300,600,900,1200) is not a callable object


I am making a timeout command using pycord python, but I am trying to make the time argument a choice instead of an input.

I have created a list called list_time with the times that should be selectable in there.

But when I run the code i get the error `TypeError: (300,600,900,1200) is not a callable object.

CODE:

list_time = [300,600,900,1200]

    @commands.slash_command(name="timeout", description="Times out the member that you specify from the server.", guild_ids=[1049465353245306951])
    @commands.has_guild_permissions(mute_members=True)
    async def timeout(self, ctx, member: discord.SlashCommandOptionType.user, time: discord.Option(int, "Select a time", autocomplete=list_time), reason: discord.SlashCommandOptionType.string):
        embed = discord.Embed(title="Successfully Timed Out.", description="Successfully Timed Out the user from the server.", coolor=discord.Color.green())
        embed.add_field(name="Member Timed Out", value=f"{member.mention}", inline=False)
        embed.add_field(name="Reason", value=f"{reason}", inline=False)
        embed.add_field(name="Time", value=f"{time} seconds", inline=False)
        await ctx.respond(embed=embed)
        embed = discord.Embed(title="Timed Out From Server", description=f"You have been timed out from {ctx.guild.name}", color=discord.Color.red())
        embed.add_field(name="Staff Member", value=f"{ctx.author.mention}", inline=False)
        embed.add_field(name="Reason", value=f"{reason}", inline=False)
        embed.add_field(name="Time", value=f"{time} seconds", inline=False)
        embed.add_field(name="Date", value=f"{today}", inline=False)

ERROR:

Task exception was never retrieved
future: <Task finished name='Task-18' coro=<ApplicationCommandMixin.on_application_command_auto_complete.<locals>.callback() done, defined at C:\Users\Bob Dylan\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\bot.py:853> exception=TypeError('(300, 600, 900, 1200) is not a callable object')>
Traceback (most recent call last):
  File "C:\Users\Bob Dylan\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\bot.py", line 856, in callback
    return await command.invoke_autocomplete_callback(ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bob Dylan\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 996, in invoke_autocomplete_callback
    if len(inspect.signature(option.autocomplete).parameters) == 2:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bob Dylan\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 3272, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bob Dylan\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 3020, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bob Dylan\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 2443, in _signature_from_callable
    raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: (300, 600, 900, 1200) is not a callable object

Solution

  • The autocomplete argument needs to take a callable object.

    So the way to fix this is to create an function where we sort through the list of times and put them as time.

    Then we can set autocomplete to equal the function.

    FUNCTION:

    async def time_searcher(ctx: discord.AutocompleteContext):
        return [
            time for time in list_time
        ]
    

    TIMEOUT COMMAND:

        @commands.slash_command(name="timeout", description="Times out the member that you specify from the server.", guild_ids=[1049465353245306951])
        @commands.has_guild_permissions(mute_members=True)
        async def timeout(self, ctx, member: discord.SlashCommandOptionType.user, time: discord.Option(int, "Select a time", autocomplete=time_searcher), reason: discord.SlashCommandOptionType.string):
            embed = discord.Embed(title="Successfully Timed Out.", description="Successfully Timed Out the user from the server.", coolor=discord.Color.green())
            embed.add_field(name="Member Timed Out", value=f"{member.mention}", inline=False)
            embed.add_field(name="Reason", value=f"{reason}", inline=False)
            embed.add_field(name="Time", value=f"{time} seconds", inline=False)
            await ctx.respond(embed=embed)
            embed = discord.Embed(title="Timed Out From Server", description=f"You have been timed out from {ctx.guild.name}", color=discord.Color.red())
            embed.add_field(name="Staff Member", value=f"{ctx.author.mention}", inline=False)
            embed.add_field(name="Reason", value=f"{reason}", inline=False)
            embed.add_field(name="Time", value=f"{time} seconds", inline=False)
            embed.add_field(name="Date", value=f"{today}", inline=False)
            await member.timeout_for(time)
            await member.send(embed=embed)