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

Autocomplete callbacks can't be methods of classes #392

Closed
3 tasks done
Luc1412 opened this issue Nov 4, 2021 · 5 comments
Closed
3 tasks done

Autocomplete callbacks can't be methods of classes #392

Luc1412 opened this issue Nov 4, 2021 · 5 comments
Labels
bug Something isn't working priority: high High Priority

Comments

@Luc1412
Copy link
Contributor

Luc1412 commented Nov 4, 2021

Summary

If the automcomplete

Reproduction Steps

  1. Create a Cog
  2. Implement a command with an autocomplete option
  3. Implement the callback into the cog or any other class

Minimal Reproducible Code

class Example(commands.Cog):
    async def _example_autocomplete(self, ctx: AutocompleteContext):
        return []

    example_option = discord.Option(str, autocomplete=_player_name_autocomplete)

Expected Results

The class reference should be filled like in command callbacks.

Actual Results

Exception in event: on_interaction

Traceback (most recent call last):
  File "C:\Users\USER\PycharmProjects\EasyFnStatsV3\venv\lib\site-packages\discord\client.py", line 352, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\USER\PycharmProjects\EasyFnStatsV3\bot.py", line 273, in on_interaction
    return await command.invoke_autocomplete_callback(interaction)
  File "C:\Users\USER\PycharmProjects\EasyFnStatsV3\venv\lib\site-packages\discord\commands\commands.py", line 760, in invoke_autocomplete_callback
    await command.invoke_autocomplete_callback(interaction)
  File "C:\Users\USER\PycharmProjects\EasyFnStatsV3\venv\lib\site-packages\discord\commands\commands.py", line 506, in invoke_autocomplete_callback
    result = await option.autocomplete(ctx)
TypeError: _player_name_autocomplete() missing 1 required positional argument: 'ctx'

Intents

guilds, members, emojis, webhooks, voice_states, guild_messages, guild_reactions, dm_messages, dm_reactions

System Information

  • Python v3.9.7-final
  • py-cord v2.0.0-alpha
    • py-cord pkg_resources: v2.0.0a4310+g60082756
  • aiohttp v3.8.0
  • system info: Windows 10 10.0.22000

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

As an alternative, you can put the callback outside of a cog. This would cause that you can't access the bot, cog, or any other useful reference.

@Luc1412 Luc1412 added the unconfirmed bug A bug report that needs triaging label Nov 4, 2021
@izxxr izxxr added bug Something isn't working and removed unconfirmed bug A bug report that needs triaging labels Nov 4, 2021
@izxxr
Copy link
Contributor

izxxr commented Nov 4, 2021

I can reproduce this.

@Luc1412
Copy link
Contributor Author

Luc1412 commented Nov 4, 2021

A possible fix would be adding self.callback.__self__ as first parameter if the function is in a class at https://github.com/Pycord-Development/pycord/blob/master/discord/commands/commands.py#L541

@rmenai
Copy link

rmenai commented Nov 4, 2021

I'm not sure if this is related but whenever I try to use an autocomplete I get this error

TypeError: Autocomplete callback must be a coroutine.

Here is my code

@staticmethod
async def num_picker(ctx: ApplicationContext) -> Iterable[int]:
    """Return a list of random integers."""
    return [random.randint(1, 100) for _ in range(25)]

@slash_command(guild_ids=settings.dev_guild_ids)
async def num(
        self, ctx: ApplicationContext,
        extension: Option(int, autocomplete=num_picker)
 -> None:
    await ctx.respond(extension)

@Dorukyum
Copy link
Member

Dorukyum commented Nov 4, 2021

I'm not sure if this is related but whenever I try to use an autocomplete I get this error

TypeError: Autocomplete callback must be a coroutine.

Here is my code

@staticmethod

async def num_picker(ctx: ApplicationContext) -> Iterable[int]:

    """Return a list of random integers."""

    return [random.randint(1, 100) for _ in range(25)]



@slash_command(guild_ids=settings.dev_guild_ids)

async def num(

        self, ctx: ApplicationContext,

        extension: Option(int, autocomplete=num_picker)

 -> None:

    await ctx.respond(extension)

You used a staticmethod, which should run like a normal function. Also we're thinking about making autocomplete accept sync functions.

@izxxr izxxr added the priority: high High Priority label Nov 6, 2021
@CodeWithSwastik
Copy link
Contributor

This has been fixed, let me know if there are any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: high High Priority
Projects
None yet
Development

No branches or pull requests

5 participants