-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nullable type converter to Interaction service (#1996)
* Add nullable type converter to Interaction service * update NullableConverter CanConvertTo body
- Loading branch information
Showing
2 changed files
with
36 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Discord.Net.Interactions/TypeConverters/NullableConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Discord.Interactions | ||
{ | ||
internal class NullableConverter<T> : TypeConverter<T> | ||
{ | ||
private readonly TypeConverter _typeConverter; | ||
|
||
public NullableConverter(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.GetTypeConverter(type, services); | ||
} | ||
|
||
public override ApplicationCommandOptionType GetDiscordType() | ||
=> _typeConverter.GetDiscordType(); | ||
|
||
public override Task<TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services) | ||
=> _typeConverter.ReadAsync(context, option, services); | ||
|
||
public override void Write(ApplicationCommandOptionProperties properties, IParameterInfo parameter) | ||
=> _typeConverter.Write(properties, parameter); | ||
} | ||
} |