コード例 #1
0
        public override async Task <TypeConverterResult> ReadAsync(IInteractionContext context, IComponentInteractionData option, IServiceProvider services)
        {
            var results = new List <TypeConverterResult>();

            foreach (var value in option.Values)
            {
                var result = await _typeReader.ReadAsync(context, value, services).ConfigureAwait(false);

                if (!result.IsSuccess)
                {
                    return(result);
                }

                results.Add(result);
            }

            var destination = Array.CreateInstance(_underlyingType, results.Count);

            for (var i = 0; i < results.Count; i++)
            {
                destination.SetValue(results[i].Value, i);
            }

            return(TypeConverterResult.FromSuccess(destination));
        }
コード例 #2
0
 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."))
         });
     }
コード例 #3
0
 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)}")));
     }
 }
コード例 #4
0
        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."));
        }
コード例 #5
0
 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)));
     }
 }
コード例 #6
0
        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)}")));
            }
        }
コード例 #7
0
        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)));
            }
        }
コード例 #8
0
 public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IComponentInteractionData option, IServiceProvider services)
 => string.IsNullOrEmpty(option.Value) ? Task.FromResult(TypeConverterResult.FromSuccess(null)) : _typeConverter.ReadAsync(context, option, services);
コード例 #9
0
ファイル: EnumReader.cs プロジェクト: sabihoshi/Discord.Net
 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)}")));
 }
コード例 #10
0
 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")));
 }