public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IComponentInteractionData option, IServiceProvider services) { try { return(option.Type switch { ComponentType.SelectMenu => Task.FromResult(TypeConverterResult.FromSuccess(Convert.ChangeType(string.Join(",", option.Values), typeof(T)))), ComponentType.TextInput => Task.FromResult(TypeConverterResult.FromSuccess(Convert.ChangeType(option.Value, typeof(T)))), _ => Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"{option.Type} doesn't have a convertible value.")) }); }
public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services) { if (Enum.TryParse <T>((string)option.Value, out var result)) { return(Task.FromResult(TypeConverterResult.FromSuccess(result))); } else { return(Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"Value {option.Value} cannot be converted to {nameof(T)}"))); } }
public override async Task <TypeConverterResult> ReadAsync(IInteractionContext context, string option, IServiceProvider services) { if (!ulong.TryParse(option, out var snowflake)) { return(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"{option} isn't a valid snowflake thus cannot be converted into {typeof(T).Name}")); } var result = await GetEntity(snowflake, context).ConfigureAwait(false); return(result is not null? TypeConverterResult.FromSuccess(result) : TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"{option} must be a valid {typeof(T).Name} snowflake to be parsed.")); }
public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, string option, IServiceProvider services) { try { var converted = Convert.ChangeType(option, typeof(T)); return(Task.FromResult(TypeConverterResult.FromSuccess(converted))); } catch (InvalidCastException castEx) { return(Task.FromResult(TypeConverterResult.FromError(castEx))); } }
public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services) { var value = option.Value as T; if (value is not null) { return(Task.FromResult(TypeConverterResult.FromSuccess(option.Value as T))); } else { return(Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"Provided input cannot be read as {nameof(IChannel)}"))); } }
public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services) { object value; if (option.Value is Optional <object> optional) { value = optional.IsSpecified ? optional.Value : default(T); } else { value = option.Value; } try { var converted = Convert.ChangeType(value, typeof(T)); return(Task.FromResult(TypeConverterResult.FromSuccess(converted))); } catch (InvalidCastException castEx) { return(Task.FromResult(TypeConverterResult.FromError(castEx))); } }
public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, string option, IServiceProvider services) { return(Task.FromResult(Enum.TryParse <T>(option, out var result) ? TypeConverterResult.FromSuccess(result) : TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"Value {option} cannot be converted to {nameof(T)}"))); }
public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services) { return((TimeSpan.TryParseExact((option.Value as string).ToLowerInvariant(), Formats, CultureInfo.InvariantCulture, out var timeSpan)) ? Task.FromResult(TypeConverterResult.FromSuccess(timeSpan)) : Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, "Failed to parse TimeSpan"))); }