如何使自动完成处理程序在 Discord.Net 中工作?

问题描述 投票:0回答:2

如果这听起来有点新手,我很抱歉,但我一直在尝试使用 AutocompleteHandler。 按照文档,我尝试创建一个非常基本的自动完成系统:

public class ExampleAutocompleteHandler : AutocompleteHandler
{
    public override async Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services)
    {
        // Create a collection with suggestions for autocomplete
        IEnumerable<AutocompleteResult> results = new[]
        {
            new AutocompleteResult("Name1", "value111"),
            new AutocompleteResult("Name2", "value2")
        };

        // max - 25 suggestions at a time (API limit)
        return AutocompletionResult.FromSuccess(results.Take(25));
    }
}

在模块中:

public class ItemModule : InteractionModuleBase
{
    // you need to add `Autocomplete` attribute before parameter to add autocompletion to it
    [SlashCommand("command_name", "command_description")]
    public async Task ExampleCommand([Summary("parameter_name"), Autocomplete(typeof(ExampleAutocompleteHandler))] string parameterWithAutocompletion)
        => await RespondAsync($"Your choice: {parameterWithAutocompletion}");

}

我尝试将处理程序注册为单例,但没有任何效果。当我输入命令时,它不显示列表。

我感谢一些帮助。预先感谢!

discord discord.net
2个回答
0
投票

事实上,文档目前缺少这一点。 就像斜杠命令一样,您需要处理自动完成事件并将其转发到您的交互服务。

在您的 Discord 客户端上处理事件

_client.AutocompleteExecuted += async (SocketAutocompleteInteraction arg) => {
    var context = new Discord.Interactions.InteractionContext(_client, arg, arg.Channel);
    await _interactionService.ExecuteCommandAsync(context,services: _serviceProvider);
};

0
投票

我也尝试集成 AutoCompleteHandler。我有同样的问题......自动完成命题是空的:

我尝试添加句柄 AutoCompleted 事件(就像 Dedmen Miller 所说),还有 InteractionCreated ……但是,窗口仍然是空的。调用了 2 个处理程序,但 ExecuteCommandAsync 不执行任何操作...

© www.soinside.com 2019 - 2024. All rights reserved.