#Dynamic Choice Providers

1 messages · Page 1 of 1 (latest)

stable scroll
#

I have created a choiceprovider following the example in documentation. It works fine.


public class choiceProvider : IChoiceProvider
{
    public async Task<IEnumerable<DiscordApplicationCommandOptionChoice>> Provider()
    {
        DiscordApplicationCommandOptionChoice[] ret = new DiscordApplicationCommandOptionChoice[Program.events.Count];
        for (var i = 0; i < Program.events.Count; i++)
        {
            ret[i] = new DiscordApplicationCommandOptionChoice(Program.events[i].id,Program.events[i].event_name);
        }
        return ret;
    }
} ```

Here's an example header of using choice provider as an option to a slash command.

``` [SlashCommand("Test", "Test command")]
        public async Task DisplayAvail(InteractionContext ctx, [ChoiceProvider(typeof(choiceProvider))][Option("event_id", "event id in question")] string option) ```

Problem is I want to dynamically change the choices offered based on **Program.events**, a static array in another class. But it seems that choice providers are cached when the commands are registered on bot startup. 
Are there any possible workarounds?
solid hatch
#

Take a look at AutocompleteProvider

random carbon
#

Yes, choice providers are cached, and this is by design, as these choices are registered when your commands are registered.

If you wish to have dynamic choices given to your user, consider looking into autocomplete, which allows you to return arbitrary data. This is implemented in the form of autocomplete providers, which I can grab a link to the docs for momentarily.

Discord Developer Portal

Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.

#

(Meant to send this out earlier, got distracted).

stable scroll
#

Gotcha thanks. I can't find much on AutocompleteProvider except the stuff on the api documentation. I follow a similar format to choice provider. When the command is run, the option "event_id" appears but all autocompletes are blank. Sorry to keep posting big blocks of code.

public class eventProvider : IAutocompleteProvider
{
    public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx)
    {
        List<DiscordAutoCompleteChoice> choices = new List<DiscordAutoCompleteChoice>
        {
            new DiscordAutoCompleteChoice("Temp option", "temp descriptipion"),
            new DiscordAutoCompleteChoice("Temp option1", "temp descriptipion1")
        };

        return choices;
    }
}
[SlashCommand("Test", "Test command")]
public async Task DisplayAvail(InteractionContext ctx, [Autocomplete(typeof(eventProvider))][Option("event_id", "event id in question")] string option) 
random carbon
#

Code blocks are fine; preferred over screenshots at least.

#

As for why it's not popping up, that is quite odd. Are you using DSharpPlus.Slashcommands, and if so, which version?

stable scroll
#

4.4.2

random carbon
#

I see. I'm not the one that created the slash commands extension so my knowledge on it isn't the best, but I can look into this regardless.

stable scroll
#

Thank you : )

random carbon
#

Are you creating an interaction response somewhere by chance?

stable scroll
#

yes. A modal and pretty sure this also counts.
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, response);

random carbon
#

I mean in an event handler,

stable scroll
#

okay so that could be the problem. Do I need to take out the interaction responses in the event handler?

random carbon
#

You can check if it's a slash command interaction

stable scroll
#

Not sure if this is what you meant but I took out all the slash command interactions and a bunch of other stuff, my event handler is just this:

[SlashCommand("Test", "This is a test")]
public async Task Test(InteractionContext ctx, [Autocomplete(typeof(eventProvider)), Option("event_id", "event id in question", true)] string option)
{
     Console.WriteLine("test");
}

and I still am getting the funny "Loading options failed" message

random carbon
#

That's the slash command

#

I meant an actual event handler, like

client.InteractionCreated += (s, e) => {}

stable scroll
#

the only event handlers I have are:

_client.Ready += OnClientReady;
_client.ModalSubmitted += ModalEventHandler;

I don't think I have one for interactionCreated

random carbon
#

I see.

#

Can you switch the log level in your client config to trace and ensure your bot is receiving the interactions?

#

Well actually-

#

Did you make sure to type something as well wires

#

Discord is really inconsistent about whether it sends an interaction when you click into the option

stable scroll
#

I tried typing "t", "temp", etc to match the options and a bunch of other random letters. No luck

#

I will trace

#

Seems the some type of interaction is being created

random carbon
#

Do you mind passing along that JSON?

stable scroll
#

sure one sec

#

Sorry. Had to pick up someone.

#

here's the file

random carbon
#

You're fine.

#

Huh. The JSON looks fine too.

stable scroll
#

I took a look at older threads and it seems others are experiencing similar problems.

#

Honestly, functionality wise I don't lose too without autocomplete. Is there any way to restart a bot every 30 minutes or so?

random carbon
stable scroll
#

awesome.

broken shoal
random carbon
#

oh

#

do we know why though

#

Like everything looks fine

broken shoal
random carbon
#

Well that's fun

#

s!remind me on friday at 5PM to look into this

austere edgeBOT
#

I'll remind you <t:1692392402:R>!

random carbon
#

WHAT

#

I THOUGHT IT WAS MONDAY

broken shoal
#

er, no

random carbon
#

Well clearly /lh

#

Next you're gonna tell me it's almost 2024

broken shoal
#

closer to '24 than '22

random carbon
#

Closer to '25 than '15

broken shoal
#

closer to '30 than '15

solid hatch
#

I dont understand why everyone has problems with autocompletion. AFAIK i dont do anything different and it works flawless for me in both 4.4.X and 5.0.0-nightly-1582

austere edgeBOT
random carbon
#

Uhh

#

s!remind 6h SCsleepybb2

austere edgeBOT
#

I'll remind you <t:1692416363:R>!