From f41a7dbcbf6f1af1eb79d3204c1223af128b168e Mon Sep 17 00:00:00 2001 From: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Date: Fri, 3 Dec 2021 13:23:37 +0300 Subject: [PATCH] fix autocomplete command traversal and use IList in command map instead of stirng[] (#342) --- .../Extensions/WebSocketExtensions.cs | 8 ++++---- .../Info/Commands/AutocompleteCommandInfo.cs | 19 +++++++++++-------- .../Parameters/SlashCommandParameterInfo.cs | 3 ++- .../InteractionService.cs | 6 ++++-- .../Map/CommandMap.cs | 12 ++++++------ .../Map/CommandMapNode.cs | 14 +++++++------- 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/Discord.Net.Interactions/Extensions/WebSocketExtensions.cs b/src/Discord.Net.Interactions/Extensions/WebSocketExtensions.cs index 85d8ee6a8..388efcbf9 100644 --- a/src/Discord.Net.Interactions/Extensions/WebSocketExtensions.cs +++ b/src/Discord.Net.Interactions/Extensions/WebSocketExtensions.cs @@ -13,7 +13,7 @@ namespace Discord.WebSocket /// /// The name of the executed command and its parents in hierarchical order. /// - public static string[] GetCommandKeywords(this IApplicationCommandInteractionData data) + public static IList GetCommandKeywords(this IApplicationCommandInteractionData data) { var keywords = new List { data.Name }; @@ -25,7 +25,7 @@ namespace Discord.WebSocket child = child.Options?.ElementAtOrDefault(0); } - return keywords.ToArray(); + return keywords; } /// @@ -35,7 +35,7 @@ namespace Discord.WebSocket /// /// The name of the executed command and its parents in hierarchical order. /// - public static string[] GetCommandKeywords(this IAutocompleteInteractionData data) + public static IList GetCommandKeywords(this IAutocompleteInteractionData data) { var keywords = new List { data.CommandName }; @@ -47,7 +47,7 @@ namespace Discord.WebSocket if (subcommand is not null) keywords.Add(subcommand.Name); - return keywords.ToArray(); + return keywords; } } } diff --git a/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs index 9e3ba4d50..712b058a3 100644 --- a/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs @@ -64,23 +64,26 @@ namespace Discord.Interactions return $"Autocomplete Command: \"{base.ToString()}\" for {context.User} in {context.Channel}"; } - internal string[] GetCommandKeywords() + internal IList GetCommandKeywords() { var keywords = new List() { ParameterName, CommandName }; - var currentParent = Module; - - while (currentParent != null) + if(!IgnoreGroupNames) { - if (!string.IsNullOrEmpty(currentParent.SlashGroupName)) - keywords.Add(currentParent.SlashGroupName); + var currentParent = Module; + + while (currentParent != null) + { + if (!string.IsNullOrEmpty(currentParent.SlashGroupName)) + keywords.Add(currentParent.SlashGroupName); - currentParent = currentParent.Parent; + currentParent = currentParent.Parent; + } } keywords.Reverse(); - return keywords.ToArray(); + return keywords; } } } diff --git a/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs b/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs index af65ea2e5..68b63c806 100644 --- a/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs +++ b/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs @@ -40,7 +40,7 @@ namespace Discord.Interactions /// /// Gets whether this parameter is configured for Autocomplete Interactions. /// - public bool IsAutocomplete => AutocompleteHandler is not null; + public bool IsAutocomplete { get; } /// /// Gets the Discord option type this parameter represents. @@ -64,6 +64,7 @@ namespace Discord.Interactions Description = builder.Description; MaxValue = builder.MaxValue; MinValue = builder.MinValue; + IsAutocomplete = builder.Autocomplete; Choices = builder.Choices.ToImmutableArray(); ChannelTypes = builder.ChannelTypes.ToImmutableArray(); } diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index 8e2c672a2..6c2a70f16 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -505,7 +505,7 @@ namespace Discord.Interactions _componentCommandMap.AddCommand(interaction, interaction.IgnoreGroupNames); foreach (var command in module.AutocompleteCommands) - _autocompleteCommandMap.AddCommand(command, command.IgnoreGroupNames); + _autocompleteCommandMap.AddCommand(command.GetCommandKeywords(), command); foreach (var subModule in module.SubModules) LoadModuleInternal(subModule); @@ -675,11 +675,13 @@ namespace Discord.Interactions { var parameter = autocompleteHandlerResult.Command.Parameters.FirstOrDefault(x => string.Equals(x.Name, interaction.Data.Current.Name, StringComparison.Ordinal)); - if(parameter is not null) + if(parameter?.AutocompleteHandler is not null) return await parameter.AutocompleteHandler.ExecuteAsync(context, interaction, parameter, services).ConfigureAwait(false); } } + keywords.Add(interaction.Data.Current.Name); + var commandResult = _autocompleteCommandMap.GetCommand(keywords); if(!commandResult.IsSuccess) diff --git a/src/Discord.Net.Interactions/Map/CommandMap.cs b/src/Discord.Net.Interactions/Map/CommandMap.cs index aa5c7fcfc..2e7bf5368 100644 --- a/src/Discord.Net.Interactions/Map/CommandMap.cs +++ b/src/Discord.Net.Interactions/Map/CommandMap.cs @@ -35,14 +35,14 @@ namespace Discord.Interactions _root.AddCommand(key, 0, command); } - public void AddCommand(string[] input, T command) + public void AddCommand(IList input, T command) { _root.AddCommand(input, 0, command); } public void RemoveCommand(T command) { - string[] key = ParseCommandName(command); + var key = ParseCommandName(command); _root.RemoveCommand(key, 0); } @@ -55,17 +55,17 @@ namespace Discord.Interactions return GetCommand(new string[] { input }); } - public SearchResult GetCommand(string[] input) => + public SearchResult GetCommand(IList input) => _root.GetCommand(input, 0); private void AddCommand(T command) { - string[] key = ParseCommandName(command); + var key = ParseCommandName(command); _root.AddCommand(key, 0, command); } - private string[] ParseCommandName(T command) + private IList ParseCommandName(T command) { var keywords = new List() { command.Name }; @@ -81,7 +81,7 @@ namespace Discord.Interactions keywords.Reverse(); - return keywords.ToArray(); + return keywords; } } } diff --git a/src/Discord.Net.Interactions/Map/CommandMapNode.cs b/src/Discord.Net.Interactions/Map/CommandMapNode.cs index 1cb93b4ac..c866fe00e 100644 --- a/src/Discord.Net.Interactions/Map/CommandMapNode.cs +++ b/src/Discord.Net.Interactions/Map/CommandMapNode.cs @@ -31,9 +31,9 @@ namespace Discord.Interactions _wildCardStr = wildCardExp; } - public void AddCommand (string[] keywords, int index, T commandInfo) + public void AddCommand (IList keywords, int index, T commandInfo) { - if (keywords.Length == index + 1) + if (keywords.Count == index + 1) { if (commandInfo.SupportsWildCards && commandInfo.Name.Contains(_wildCardStr)) { @@ -46,7 +46,7 @@ namespace Discord.Interactions } else { - if (!_commands.TryAdd(commandInfo.Name, commandInfo)) + if (!_commands.TryAdd(keywords[index], commandInfo)) throw new InvalidOperationException($"A {typeof(T).FullName} already exists with the same name: {string.Join(" ", keywords)}"); } } @@ -57,9 +57,9 @@ namespace Discord.Interactions } } - public bool RemoveCommand (string[] keywords, int index) + public bool RemoveCommand (IList keywords, int index) { - if (keywords.Length == index + 1) + if (keywords.Count == index + 1) return _commands.TryRemove(keywords[index], out var _); else { @@ -70,11 +70,11 @@ namespace Discord.Interactions } } - public SearchResult GetCommand (string[] keywords, int index) + public SearchResult GetCommand (IList keywords, int index) { string name = string.Join(" ", keywords); - if (keywords.Length == index + 1) + if (keywords.Count == index + 1) { if (_commands.TryGetValue(keywords[index], out var cmd)) return SearchResult.FromSuccess(name, cmd);