using Discord.Commands.Builders; using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Threading.Tasks; namespace Discord.Commands { /// /// Provides the information of a parameter. /// [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class ParameterInfo { private readonly TypeReader _reader; /// /// Gets the command that associates with this parameter. /// public CommandInfo Command { get; } /// /// Gets the name of this parameter. /// public string Name { get; } /// /// Gets the summary of this parameter. /// public string Summary { get; } /// /// Gets a value that indicates whether this parameter is optional or not. /// public bool IsOptional { get; } /// /// Gets a value that indicates whether this parameter is a remainder parameter or not. /// public bool IsRemainder { get; } public bool IsMultiple { get; } /// /// Gets the type of the parameter. /// public Type Type { get; } /// /// Gets the default value for this optional parameter if applicable. /// public object DefaultValue { get; } /// /// Gets a read-only list of precondition that apply to this parameter. /// public IReadOnlyList Preconditions { get; } /// /// Gets a read-only list of attributes that apply to this parameter. /// public IReadOnlyList Attributes { get; } internal ParameterInfo(ParameterBuilder builder, CommandInfo command, CommandService service) { Command = command; Name = builder.Name; Summary = builder.Summary; IsOptional = builder.IsOptional; IsRemainder = builder.IsRemainder; IsMultiple = builder.IsMultiple; Type = builder.ParameterType; DefaultValue = builder.DefaultValue; Preconditions = builder.Preconditions.ToImmutableArray(); Attributes = builder.Attributes.ToImmutableArray(); _reader = builder.TypeReader; } public async Task CheckPreconditionsAsync(ICommandContext context, object arg, IServiceProvider services = null) { services = services ?? EmptyServiceProvider.Instance; foreach (var precondition in Preconditions) { var result = await precondition.CheckPermissionsAsync(context, this, arg, services).ConfigureAwait(false); if (!result.IsSuccess) return result; } return PreconditionResult.FromSuccess(); } public async Task ParseAsync(ICommandContext context, string input, IServiceProvider services = null) { services = services ?? EmptyServiceProvider.Instance; return await _reader.ReadAsync(context, input, services).ConfigureAwait(false); } public override string ToString() => Name; private string DebuggerDisplay => $"{Name}{(IsOptional ? " (Optional)" : "")}{(IsRemainder ? " (Remainder)" : "")}"; } }