Browse Source

Merge 0ba4d4e901 into f417c2b516

pull/312/merge
Joe4evr GitHub 8 years ago
parent
commit
9a992b8456
3 changed files with 39 additions and 0 deletions
  1. +11
    -0
      src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs
  2. +7
    -0
      src/Discord.Net.Commands/CommandInfo.cs
  3. +21
    -0
      src/Discord.Net.Commands/CommandParameter.cs

+ 11
- 0
src/Discord.Net.Commands/Attributes/ParameterPreconditionAttribute.cs View File

@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;

namespace Discord.Commands
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)]
public abstract class ParameterPreconditionAttribute : Attribute
{
public abstract Task<PreconditionResult> CheckPermissions(CommandContext context, CommandParameter parameter, IDependencyMap map);
}
}

+ 7
- 0
src/Discord.Net.Commands/CommandInfo.cs View File

@@ -104,6 +104,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();
}



+ 21
- 0
src/Discord.Net.Commands/CommandParameter.cs View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
@@ -18,6 +20,7 @@ namespace Discord.Commands
public bool IsMultiple { get; }
public Type ElementType { get; }
public object DefaultValue { get; }
public IReadOnlyList<ParameterPreconditionAttribute> Preconditions { get; }

public CommandParameter(ParameterInfo source, string name, string summary, Type type, TypeReader reader, bool isOptional, bool isRemainder, bool isMultiple, object defaultValue)
{
@@ -30,6 +33,7 @@ namespace Discord.Commands
IsRemainder = isRemainder;
IsMultiple = isMultiple;
DefaultValue = defaultValue;
Preconditions = BuildPreconditions(source);
}

public async Task<TypeReaderResult> Parse(CommandContext context, string input)
@@ -37,6 +41,23 @@ namespace Discord.Commands
return await _reader.Read(context, input).ConfigureAwait(false);
}

public async Task<PreconditionResult> 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<ParameterPreconditionAttribute> BuildPreconditions(ParameterInfo paramInfo)
{
return paramInfo.GetCustomAttributes<ParameterPreconditionAttribute>().ToImmutableArray();
}

public override string ToString() => Name;
private string DebuggerDisplay => $"{Name}{(IsOptional ? " (Optional)" : "")}{(IsRemainder ? " (Remainder)" : "")}";
}


Loading…
Cancel
Save