Browse Source

Improve parameter precondition type safety (#532)

* Improve parameter precondition type safety

Also removes some terrible code which was left over when I first
implemented parameter preconditions. I don't know why that was there.

With this commit, parameter preconditions should be much safer as they
use generic methods instead of janky casting of objects.

* Remove generic CheckPreconditions method
tags/1.0-rc
Finite Reality RogueException 8 years ago
parent
commit
2160e5dac8
2 changed files with 7 additions and 9 deletions
  1. +4
    -2
      src/Discord.Net.Commands/Info/CommandInfo.cs
  2. +3
    -7
      src/Discord.Net.Commands/Info/ParameterInfo.cs

+ 4
- 2
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -128,9 +128,11 @@ namespace Discord.Commands
{
object[] args = GenerateArgs(argList, paramList);

foreach (var parameter in Parameters)
for (int position = 0; position < Parameters.Count; position++)
{
var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false);
var parameter = Parameters[position];
var argument = args[position];
var result = await parameter.CheckPreconditionsAsync(context, argument, map).ConfigureAwait(false);
if (!result.IsSuccess)
return ExecuteResult.FromError(result);
}


+ 3
- 7
src/Discord.Net.Commands/Info/ParameterInfo.cs View File

@@ -4,6 +4,7 @@ using System.Collections.Immutable;
using System.Threading.Tasks;

using Discord.Commands.Builders;
using System.Reflection;

namespace Discord.Commands
{
@@ -40,19 +41,14 @@ namespace Discord.Commands
_reader = builder.TypeReader;
}

public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, object[] args, IDependencyMap map = null)
public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, object arg, 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);
var result = await precondition.CheckPermissions(context, this, arg, map).ConfigureAwait(false);
if (!result.IsSuccess)
return result;
}


Loading…
Cancel
Save