diff --git a/docs/guides/interactions_framework/typeconverters.md b/docs/guides/interactions_framework/typeconverters.md index c06f40386..12ca7dab5 100644 --- a/docs/guides/interactions_framework/typeconverters.md +++ b/docs/guides/interactions_framework/typeconverters.md @@ -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: 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`) of the `TypeConverter` base class which implements the following method body for `CanConvertTo()` method diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index 0740f7507..510a5952d 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -631,9 +631,6 @@ namespace Discord.Interactions if (_typeConverters.TryGetValue(type, out var 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))) { services ??= EmptyServiceProvider.Instance; @@ -644,6 +641,9 @@ namespace Discord.Interactions 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"); } @@ -861,16 +861,14 @@ namespace Discord.Interactions private Type GetMostSpecificTypeConverter (Type type) { - var scorePairs = new Dictionary(); - 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()