Add grouping of preconditions to allow for flexible precondition logic. (#672)
* Add grouping of preconditions to allow for flexible precondition logic. * Fix checking Module Preconditions twice (and none of the command's own) * Fix command preconditions group 0 looping over every other precondition anyway #whoopsies * Use custom message when a non-zero Precondition Group fails. * Fix doc comment rendering. * Refactor loops into local function * Considering a new result type * Switch to IReadOnlyCollection<T> and fix compiler errors * Revert PreconditionResult -> IResult in return types - Change PreconditionResult to a class that PreconditionGroupResult inherits. * Feedback on property name. * Change grouping type int -> string * Explicitly use an ordinal StringComparer * Full stops on error messages * Remove some sillyness. * Remove unneeded using.
This commit is contained in:
27
src/Discord.Net.Commands/Results/PreconditionGroupResult.cs
Normal file
27
src/Discord.Net.Commands/Results/PreconditionGroupResult.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public class PreconditionGroupResult : PreconditionResult
|
||||
{
|
||||
public IReadOnlyCollection<PreconditionResult> PreconditionResults { get; }
|
||||
|
||||
protected PreconditionGroupResult(CommandError? error, string errorReason, ICollection<PreconditionResult> preconditions)
|
||||
: base(error, errorReason)
|
||||
{
|
||||
PreconditionResults = (preconditions ?? new List<PreconditionResult>(0)).ToReadOnlyCollection();
|
||||
}
|
||||
|
||||
public static new PreconditionGroupResult FromSuccess()
|
||||
=> new PreconditionGroupResult(null, null, null);
|
||||
public static PreconditionGroupResult FromError(string reason, ICollection<PreconditionResult> preconditions)
|
||||
=> new PreconditionGroupResult(CommandError.UnmetPrecondition, reason, preconditions);
|
||||
public static new PreconditionGroupResult FromError(IResult result) //needed?
|
||||
=> new PreconditionGroupResult(result.Error, result.ErrorReason, null);
|
||||
|
||||
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
private string DebuggerDisplay => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,14 @@
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public struct PreconditionResult : IResult
|
||||
public class PreconditionResult : IResult
|
||||
{
|
||||
public CommandError? Error { get; }
|
||||
public string ErrorReason { get; }
|
||||
|
||||
public bool IsSuccess => !Error.HasValue;
|
||||
|
||||
private PreconditionResult(CommandError? error, string errorReason)
|
||||
protected PreconditionResult(CommandError? error, string errorReason)
|
||||
{
|
||||
Error = error;
|
||||
ErrorReason = errorReason;
|
||||
|
||||
Reference in New Issue
Block a user