How To Add Autocompletion To Your Discord Slash Commands

Поділитися
Вставка
  • Опубліковано 13 гру 2024

КОМЕНТАРІ • 19

  • @eymenknx1004
    @eymenknx1004 5 місяців тому +1

    Thanks a lot, I wanted to implement this feature on my bot and I searched a lot but could not find anything about it then I saw your video and oh God, it was a relief!

  • @asoniox
    @asoniox Рік тому +2

    Countless hours spent searching the internet and no results. This video summed up what I wanted to do perfectly. Thank you!

  • @AGuard
    @AGuard Рік тому +4

    Your work helps me a lot thanks! Don't stop this

  • @WilliamWonker
    @WilliamWonker 8 місяців тому +1

    Thanks! The docs aren't the most helpful sometimes, this was excellent.

  • @almuaz
    @almuaz 2 роки тому +1

    Thanks, i was stuck for hours.

  • @narayan8778
    @narayan8778 Рік тому +5

    How would you add 2 arguments where the options displayed for the second one are dependent on the choice of the first. Like first argument could be "state" and depending on that the second one displays the cities in that state .

    • @bubfusion
      @bubfusion 8 місяців тому +1

      Did you ever figure it out? I'm trying to do the same thing

    • @InhaRelic
      @InhaRelic 5 місяців тому

      @@bubfusion HELLO! late reply lmao
      So this depends a LOT on how you want to do it and the ammount of data you want to filter. The basic gist of it is to store outside of the command the `data` variable
      so, for example, lets say that the first field for me is "Character" and the second one is "Item"
      The first autocomplete will list all of the user's characters, and the second one will list the chosen character inventory by Item name, and we will asume that we already have the logic for both autocompletes to query this data.
      at the top of your file, over where your commands are but below where your intents (or cog class) you create a dict, lets call this "chosen_character", so, for example,
      self.chosen_character = {}
      Then, in the function that handles the first autocomplete, define "chosen_character" to the value of data, something like;
      self.chosen_character[user_id] = data[0].value if data else None
      and then in your second autocomplete, you call what you stored before using it;
      character_id = self.chosen_character.get(user_id, "value")
      This requires you to add to both functions the following at the begining: user_id = str(interaction.user.id)
      because otherwise it will store ONE character for everyone and if 2 users use the command at the same time it will break the second autocomplete for one of them.
      Also, you should reset the dict every time the command ends executing so that if someone goes directly to the second field, it doesnt keep the value of the last time they used the command.
      THIS IS JUST PROOF OF CONCEPT, literally did this since i saw this comments. There are probably much better ways to do this, and also it has the problem that when the command is written, chosen_character will always default to the first result on the query or the first item of the list, and if the user clicks on something on the list without writing, the bot has no idea it happened for some reason and the second autocomplete will filter/query based off the first item instead of what the user chose.
      Right now I think there might be a way to solve this listening to the interaction with an event handler? or maybe modifying the autocomplete to not show anything until its written, its up to you to decide what workaround works best

  • @Garlo-h7m
    @Garlo-h7m 4 місяці тому

    I just do "from typing import Literal" and
    "async def server_command(self, interaction: discord.Interaction, option:Literal['Restart', 'Stop']):"
    does the same thing without autocomplete function

  • @ThatLadLegend
    @ThatLadLegend Рік тому

    How could I do this dynamically? Like to get the currently loaded cogs?
    Figured it out like 5 minutes later lmao.

  • @Jonathannavaxd
    @Jonathannavaxd 6 місяців тому

    I'm doing exactly the same thing as you and I can't get autocomplete on discord.

  • @pointlesssssxddxdxd
    @pointlesssssxddxdxd Рік тому

    i have a error "TypeError: issubclass() arg 1 must be a class". how can i fix that?

  • @MrBossclub
    @MrBossclub Рік тому

    Can i use insted of items to load list from enum class event list? And load only 25 first and then of user try to type in something in the option, the bot will give new choice results based on matches in enum list?

    • @kukackis5051
      @kukackis5051 Рік тому

      i belive if u breake the for loop at 25 items that should work

  • @_Astro.Nish_
    @_Astro.Nish_ Рік тому

    Even simple slash commands with Literals do autocomplete automatically without this. Why do we need separate autocomplete command?

    • @cheesecake19763
      @cheesecake19763 Рік тому +2

      for example if you want to load data from a database which may be different every time you run the command or when searching the web via a command or sth

  • @justskiez9584
    @justskiez9584 Рік тому

    Great video. Do you know how to add a description to an option?

    • @richardschwabe
      @richardschwabe  Рік тому +1

      The app_commands.Choice does not allow for a description, only localized names and values.
      However, the SelectOption has a description attribute.

  • @CrunchySpon
    @CrunchySpon Рік тому

    TypeError: unknown parameter given: item