Browse Source

Fix sub commands being interpreted as a parameter for autocomplete

pull/1923/head
quin lynch 3 years ago
parent
commit
4b1f875f43
2 changed files with 23 additions and 4 deletions
  1. +5
    -2
      src/Discord.Net.Rest/API/Common/AutocompleteInteractionDataOption.cs
  2. +18
    -2
      src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteractionData.cs

+ 5
- 2
src/Discord.Net.Rest/API/Common/AutocompleteInteractionDataOption.cs View File

@@ -15,10 +15,13 @@ namespace Discord.API
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("options")]
public Optional<AutocompleteInteractionDataOption[]> Options { get; set; }

[JsonProperty("value")]
public object Value { get; set; }
public Optional<object> Value { get; set; }

[JsonProperty("focused")]
public bool Focused { get; set; }
public Optional<bool> Focused { get; set; }
}
}

+ 18
- 2
src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteractionData.cs View File

@@ -46,12 +46,12 @@ namespace Discord.WebSocket

internal SocketAutocompleteInteractionData(DataModel model)
{
var options = model.Options.Select(x => new AutocompleteOption(x.Type, x.Name, x.Value, x.Focused));
var options = model.Options.SelectMany(x => GetOptions(x));

Current = options.FirstOrDefault(x => x.Focused);
Options = options.ToImmutableArray();

if (Options?.Count == 1 && Current == null)
if (options != null && options.Count() == 1 && Current == null)
Current = Options.FirstOrDefault();

CommandName = model.Name;
@@ -59,5 +59,21 @@ namespace Discord.WebSocket
Type = model.Type;
Version = model.Version;
}

private List<AutocompleteOption> GetOptions(API.AutocompleteInteractionDataOption model)
{
List<AutocompleteOption> options = new List<AutocompleteOption>();

if (model.Options.IsSpecified)
{
options.AddRange(model.Options.Value.SelectMany(x => GetOptions(x)));
}
else if(model.Focused.IsSpecified)
{
options.Add(new AutocompleteOption(model.Type, model.Name, model.Value, model.Focused.Value));
}

return options;
}
}
}

Loading…
Cancel
Save