From 15da93b44a552305175abe27cd1202891a569061 Mon Sep 17 00:00:00 2001 From: Joe4evr Date: Sat, 15 Oct 2016 23:55:44 +0200 Subject: [PATCH 1/2] Allow Preconditions on parameters. --- .../ParameterPreconditionAttribute.cs | 11 ++++++++++ src/Discord.Net.Commands/CommandInfo.cs | 7 ++++++ src/Discord.Net.Commands/CommandParameter.cs | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs diff --git a/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs b/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs new file mode 100644 index 000000000..2dd035107 --- /dev/null +++ b/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs @@ -0,0 +1,11 @@ +using System; +using System.Threading.Tasks; + +namespace Discord.Commands.Attributes +{ + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)] + public abstract class ParameterPreconditionAttribute : Attribute + { + public abstract Task CheckPermissions(CommandContext context, CommandParameter parameter, IDependencyMap map); + } +} diff --git a/src/Discord.Net.Commands/CommandInfo.cs b/src/Discord.Net.Commands/CommandInfo.cs index 75107a80c..caecc250d 100644 --- a/src/Discord.Net.Commands/CommandInfo.cs +++ b/src/Discord.Net.Commands/CommandInfo.cs @@ -101,6 +101,13 @@ namespace Discord.Commands return result; } + foreach (CommandParameter parameter in Parameters) + { + var result = await parameter.CheckPreconditions(context, map).ConfigureAwait(false); + if (!result.IsSuccess) + return result; + } + return PreconditionResult.FromSuccess(); } diff --git a/src/Discord.Net.Commands/CommandParameter.cs b/src/Discord.Net.Commands/CommandParameter.cs index 1edf42bf1..b3b43c15c 100644 --- a/src/Discord.Net.Commands/CommandParameter.cs +++ b/src/Discord.Net.Commands/CommandParameter.cs @@ -1,7 +1,10 @@ using System; +using System.Collections.Generic; +using System.Collections.Immutable; using System.Diagnostics; using System.Reflection; using System.Threading.Tasks; +using Discord.Commands.Attributes; namespace Discord.Commands { @@ -18,6 +21,7 @@ namespace Discord.Commands public bool IsMultiple { get; } public Type ElementType { get; } public object DefaultValue { get; } + public IReadOnlyList Preconditions { get; } public CommandParameter(ParameterInfo source, string name, string summary, Type type, TypeReader reader, bool isOptional, bool isRemainder, bool isMultiple, object defaultValue) { @@ -30,6 +34,7 @@ namespace Discord.Commands IsRemainder = isRemainder; IsMultiple = isMultiple; DefaultValue = defaultValue; + Preconditions = BuildPreconditions(source); } public async Task Parse(CommandContext context, string input) @@ -37,6 +42,23 @@ namespace Discord.Commands return await _reader.Read(context, input).ConfigureAwait(false); } + public async Task CheckPreconditions(CommandContext context, IDependencyMap map = null) + { + foreach (ParameterPreconditionAttribute precondition in Preconditions) + { + var result = await precondition.CheckPermissions(context, this, map).ConfigureAwait(false); + if (!result.IsSuccess) + return result; + } + + return PreconditionResult.FromSuccess(); + } + + private IReadOnlyList BuildPreconditions(ParameterInfo paramInfo) + { + return paramInfo.GetCustomAttributes().ToImmutableArray(); + } + public override string ToString() => Name; private string DebuggerDisplay => $"{Name}{(IsOptional ? " (Optional)" : "")}{(IsRemainder ? " (Remainder)" : "")}"; } From 0ba4d4e901ef998c89c0286348ff933c44f962f3 Mon Sep 17 00:00:00 2001 From: Joe4evr Date: Sun, 16 Oct 2016 11:16:30 +0200 Subject: [PATCH 2/2] Fix namespace --- .../Attributes/ParameterPreconditionAttribute.cs | 2 +- src/Discord.Net.Commands/CommandParameter.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs b/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs index 2dd035107..3df215649 100644 --- a/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs +++ b/src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -namespace Discord.Commands.Attributes +namespace Discord.Commands { [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)] public abstract class ParameterPreconditionAttribute : Attribute diff --git a/src/Discord.Net.Commands/CommandParameter.cs b/src/Discord.Net.Commands/CommandParameter.cs index b3b43c15c..3a652cd5c 100644 --- a/src/Discord.Net.Commands/CommandParameter.cs +++ b/src/Discord.Net.Commands/CommandParameter.cs @@ -4,7 +4,6 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Reflection; using System.Threading.Tasks; -using Discord.Commands.Attributes; namespace Discord.Commands {