Browse Source

Merge branch 'release/3.x' of https://github.com/Discord-Net-Labs/Discord.Net-Labs into release/3.x

pull/1958/head
quin lynch 3 years ago
parent
commit
fcbb6a1647
2 changed files with 11 additions and 13 deletions
  1. +2
    -2
      docs/guides/interactions_framework/typeconverters.md
  2. +9
    -11
      src/Discord.Net.Interactions/InteractionService.cs

+ 2
- 2
docs/guides/interactions_framework/typeconverters.md View File

@@ -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<T>`) of the `TypeConverter` base class which implements the following method body for `CanConvertTo()` method



+ 9
- 11
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -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<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()


Loading…
Cancel
Save