Merge pull request #364 from FiniteReality/feature/parameter-tweaks
Parameter preconditions and typereader overriding
This commit is contained in:
@@ -137,7 +137,15 @@ namespace Discord.Commands
|
||||
|
||||
try
|
||||
{
|
||||
var args = GenerateArgs(argList, paramList);
|
||||
object[] args = GenerateArgs(argList, paramList);
|
||||
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false);
|
||||
if (!result.IsSuccess)
|
||||
return ExecuteResult.FromError(result);
|
||||
}
|
||||
|
||||
switch (RunMode)
|
||||
{
|
||||
case RunMode.Sync: //Always sync
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord.Commands.Builders;
|
||||
@@ -10,6 +12,17 @@ namespace Discord.Commands
|
||||
{
|
||||
private readonly TypeReader _reader;
|
||||
|
||||
public CommandInfo Command { get; }
|
||||
public string Name { get; }
|
||||
public string Summary { get; }
|
||||
public bool IsOptional { get; }
|
||||
public bool IsRemainder { get; }
|
||||
public bool IsMultiple { get; }
|
||||
public Type Type { get; }
|
||||
public object DefaultValue { get; }
|
||||
|
||||
public IReadOnlyList<ParameterPreconditionAttribute> Preconditions { get; }
|
||||
|
||||
internal ParameterInfo(ParameterBuilder builder, CommandInfo command, CommandService service)
|
||||
{
|
||||
Command = command;
|
||||
@@ -23,17 +36,30 @@ namespace Discord.Commands
|
||||
Type = builder.ParameterType;
|
||||
DefaultValue = builder.DefaultValue;
|
||||
|
||||
Preconditions = builder.Preconditions.ToImmutableArray();
|
||||
|
||||
_reader = builder.TypeReader;
|
||||
}
|
||||
|
||||
public CommandInfo Command { get; }
|
||||
public string Name { get; }
|
||||
public string Summary { get; }
|
||||
public bool IsOptional { get; }
|
||||
public bool IsRemainder { get; }
|
||||
public bool IsMultiple { get; }
|
||||
public Type Type { get; }
|
||||
public object DefaultValue { get; }
|
||||
public async Task<PreconditionResult> CheckPreconditionsAsync(CommandContext context, object[] args, IDependencyMap map = null)
|
||||
{
|
||||
if (map == null)
|
||||
map = DependencyMap.Empty;
|
||||
|
||||
int position = 0;
|
||||
for(position = 0; position < Command.Parameters.Count; position++)
|
||||
if (Command.Parameters[position] == this)
|
||||
break;
|
||||
|
||||
foreach (var precondition in Preconditions)
|
||||
{
|
||||
var result = await precondition.CheckPermissions(context, this, args[position], map).ConfigureAwait(false);
|
||||
if (!result.IsSuccess)
|
||||
return result;
|
||||
}
|
||||
|
||||
return PreconditionResult.FromSuccess();
|
||||
}
|
||||
|
||||
public async Task<TypeReaderResult> Parse(CommandContext context, string input)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user