using System; using System.Diagnostics; namespace Discord.Commands { /// /// Represents a result type for command preconditions. /// [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class PreconditionResult : IResult { /// public CommandError? Error { get; } /// public string ErrorReason { get; } /// public bool IsSuccess => !Error.HasValue; /// /// Initializes a new class with the command type /// and reason. /// /// The type of failure. /// The reason of failure. protected PreconditionResult(CommandError? error, string errorReason) { Error = error; ErrorReason = errorReason; } /// /// Returns a with no errors. /// public static PreconditionResult FromSuccess() => new PreconditionResult(null, null); /// /// Returns a with and the /// specified reason. /// /// The reason of failure. public static PreconditionResult FromError(string reason) => new PreconditionResult(CommandError.UnmetPrecondition, reason); public static PreconditionResult FromError(Exception ex) => new PreconditionResult(CommandError.Exception, ex.Message); /// /// Returns a with the specified type. /// /// The result of failure. public static PreconditionResult FromError(IResult result) => new PreconditionResult(result.Error, result.ErrorReason); /// /// Returns a string indicating whether the is successful. /// public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}"; private string DebuggerDisplay => IsSuccess ? "Success" : $"{Error}: {ErrorReason}"; } }