* added interaction specific interfaces * fix build error * implement change requests * add autocomplete respond method to IAutocompleteInteraction * fix sharded client current user * fix generic typeconverter picking priority * Revert "fix sharded client current user" This reverts commitpull/1958/heada9c15ffd6a
. * Revert "add autocomplete respond method to IAutocompleteInteraction" This reverts commitf2fc50f1f1
.
@@ -40,8 +40,8 @@ This method is used by Interaction Service to search for alternative Type Conver | |||||
Interaction Services determines the most suitable `TypeConverter` for a parameter type in the following order: | Interaction Services determines the most suitable `TypeConverter` for a parameter type in the following order: | ||||
1. It searches for a `TypeConverter` that is registered to specifically target that parameter type | 1. It searches for a `TypeConverter` that is registered to specifically target that parameter type | ||||
2. It searches for a `TypeConverter` that returns `true` when its `CanConvertTo()` method is invoked for thaty parameter type. | |||||
3. It searches for a generic `TypeConverter` with a matching type constraint. If there are more multiple matches, the one whose type constraint is the most specialized will be chosen. | |||||
2. It searches for a generic `TypeConverter` with a matching type constraint. If there are more multiple matches, the one whose type constraint is the most specialized will be chosen. | |||||
3. It searches for a `TypeConverter` that returns `true` when its `CanConvertTo()` method is invoked for thaty parameter type. | |||||
> Alternatively, you can use the generic variant (`TypeConverter<T>`) of the `TypeConverter` base class which implements the following method body for `CanConvertTo()` method | > Alternatively, you can use the generic variant (`TypeConverter<T>`) of the `TypeConverter` base class which implements the following method body for `CanConvertTo()` method | ||||
@@ -631,9 +631,6 @@ namespace Discord.Interactions | |||||
if (_typeConverters.TryGetValue(type, out var specific)) | if (_typeConverters.TryGetValue(type, out var specific)) | ||||
return specific; | return specific; | ||||
else if (_typeConverters.Any(x => x.Value.CanConvertTo(type))) | |||||
return _typeConverters.First(x => x.Value.CanConvertTo(type)).Value; | |||||
else if (_genericTypeConverters.Any(x => x.Key.IsAssignableFrom(type))) | else if (_genericTypeConverters.Any(x => x.Key.IsAssignableFrom(type))) | ||||
{ | { | ||||
services ??= EmptyServiceProvider.Instance; | services ??= EmptyServiceProvider.Instance; | ||||
@@ -644,6 +641,9 @@ namespace Discord.Interactions | |||||
return converter; | return converter; | ||||
} | } | ||||
else if (_typeConverters.Any(x => x.Value.CanConvertTo(type))) | |||||
return _typeConverters.First(x => x.Value.CanConvertTo(type)).Value; | |||||
throw new ArgumentException($"No type {nameof(TypeConverter)} is defined for this {type.FullName}", "type"); | throw new ArgumentException($"No type {nameof(TypeConverter)} is defined for this {type.FullName}", "type"); | ||||
} | } | ||||
@@ -861,16 +861,14 @@ namespace Discord.Interactions | |||||
private Type GetMostSpecificTypeConverter (Type type) | private Type GetMostSpecificTypeConverter (Type type) | ||||
{ | { | ||||
var scorePairs = new Dictionary<Type, int>(); | |||||
var validConverters = _genericTypeConverters.Where(x => x.Key.IsAssignableFrom(type)); | |||||
if (_genericTypeConverters.TryGetValue(type, out var matching)) | |||||
return matching; | |||||
foreach (var typeConverterPair in validConverters) | |||||
{ | |||||
var score = validConverters.Count(x => typeConverterPair.Key.IsAssignableFrom(x.Key)); | |||||
scorePairs.Add(typeConverterPair.Value, score); | |||||
} | |||||
var typeInterfaces = type.GetInterfaces(); | |||||
var candidates = _genericTypeConverters.Where(x => x.Key.IsAssignableFrom(type)) | |||||
.OrderByDescending(x => typeInterfaces.Count(y => y.IsAssignableFrom(x.Key))); | |||||
return scorePairs.OrderBy(x => x.Value).ElementAt(0).Key; | |||||
return candidates.First().Value; | |||||
} | } | ||||
private void EnsureClientReady() | private void EnsureClientReady() | ||||