如果这听起来有点新手,我很抱歉,但我一直在尝试使用 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 客户端上处理事件
_client.AutocompleteExecuted += async (SocketAutocompleteInteraction arg) => {
var context = new Discord.Interactions.InteractionContext(_client, arg, arg.Channel);
await _interactionService.ExecuteCommandAsync(context,services: _serviceProvider);
};