Browse Source

add nullable ComponentTypeConverter and TypeReader

pull/2307/head
Cenngo 3 years ago
parent
commit
7017f011f7
2 changed files with 46 additions and 0 deletions
  1. +23
    -0
      src/Discord.Net.Interactions/TypeConverters/ComponentInteractions/NullableComponentConverter.cs
  2. +23
    -0
      src/Discord.Net.Interactions/TypeReaders/NullableReader.cs

+ 23
- 0
src/Discord.Net.Interactions/TypeConverters/ComponentInteractions/NullableComponentConverter.cs View File

@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;

namespace Discord.Interactions
{
internal class NullableComponentConverter<T> : ComponentTypeConverter<T>
{
private readonly ComponentTypeConverter _typeConverter;

public NullableComponentConverter(InteractionService interactionService, IServiceProvider services)
{
var type = Nullable.GetUnderlyingType(typeof(T));

if (type is null)
throw new ArgumentException($"No type {nameof(TypeConverter)} is defined for this {type.FullName}", "type");

_typeConverter = interactionService.GetComponentTypeConverter(type, services);
}

public override Task<TypeConverterResult> ReadAsync(IInteractionContext context, IComponentInteractionData option, IServiceProvider services)
=> _typeConverter.ReadAsync(context, option, services);
}
}

+ 23
- 0
src/Discord.Net.Interactions/TypeReaders/NullableReader.cs View File

@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;

namespace Discord.Interactions
{
internal class NullableReader<T> : TypeReader<T>
{
private readonly TypeReader _typeReader;

public NullableReader(InteractionService interactionService, IServiceProvider services)
{
var type = Nullable.GetUnderlyingType(typeof(T));

if (type is null)
throw new ArgumentException($"No type {nameof(TypeConverter)} is defined for this {type.FullName}", "type");

_typeReader = interactionService.GetTypeReader(type, services);
}

public override Task<TypeConverterResult> ReadAsync(IInteractionContext context, string option, IServiceProvider services)
=> _typeReader.ReadAsync(context, option, services);
}
}

Loading…
Cancel
Save