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
This commit is contained in:
committed by
RogueException
parent
96a377a258
commit
2160e5dac8
@@ -128,9 +128,11 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
object[] args = GenerateArgs(argList, paramList);
|
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)
|
if (!result.IsSuccess)
|
||||||
return ExecuteResult.FromError(result);
|
return ExecuteResult.FromError(result);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Collections.Immutable;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Discord.Commands.Builders;
|
using Discord.Commands.Builders;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Discord.Commands
|
namespace Discord.Commands
|
||||||
{
|
{
|
||||||
@@ -40,19 +41,14 @@ namespace Discord.Commands
|
|||||||
_reader = builder.TypeReader;
|
_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)
|
if (map == null)
|
||||||
map = DependencyMap.Empty;
|
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)
|
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)
|
if (!result.IsSuccess)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user