Ability to ignore unused parameters instead of failing the command. (#915)
* Addition of FailOnTooManyArgs * Correct name & only pass in bool
This commit is contained in:
@@ -14,7 +14,7 @@ namespace Discord.Commands
|
|||||||
QuotedParameter
|
QuotedParameter
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, IServiceProvider services, string input, int startPos)
|
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, bool ignoreExtraArgs, IServiceProvider services, string input, int startPos)
|
||||||
{
|
{
|
||||||
ParameterInfo curParam = null;
|
ParameterInfo curParam = null;
|
||||||
StringBuilder argBuilder = new StringBuilder(input.Length);
|
StringBuilder argBuilder = new StringBuilder(input.Length);
|
||||||
@@ -109,7 +109,12 @@ namespace Discord.Commands
|
|||||||
if (argString != null)
|
if (argString != null)
|
||||||
{
|
{
|
||||||
if (curParam == null)
|
if (curParam == null)
|
||||||
|
{
|
||||||
|
if (ignoreExtraArgs)
|
||||||
|
break;
|
||||||
|
else
|
||||||
return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");
|
return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");
|
||||||
|
}
|
||||||
|
|
||||||
var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false);
|
var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false);
|
||||||
if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches)
|
if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Discord.Commands
|
|||||||
private readonly HashSet<ModuleInfo> _moduleDefs;
|
private readonly HashSet<ModuleInfo> _moduleDefs;
|
||||||
private readonly CommandMap _map;
|
private readonly CommandMap _map;
|
||||||
|
|
||||||
internal readonly bool _caseSensitive, _throwOnError;
|
internal readonly bool _caseSensitive, _throwOnError, _ignoreExtraArgs;
|
||||||
internal readonly char _separatorChar;
|
internal readonly char _separatorChar;
|
||||||
internal readonly RunMode _defaultRunMode;
|
internal readonly RunMode _defaultRunMode;
|
||||||
internal readonly Logger _cmdLogger;
|
internal readonly Logger _cmdLogger;
|
||||||
@@ -42,6 +42,7 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
_caseSensitive = config.CaseSensitiveCommands;
|
_caseSensitive = config.CaseSensitiveCommands;
|
||||||
_throwOnError = config.ThrowOnError;
|
_throwOnError = config.ThrowOnError;
|
||||||
|
_ignoreExtraArgs = config.IgnoreExtraArgs;
|
||||||
_separatorChar = config.SeparatorChar;
|
_separatorChar = config.SeparatorChar;
|
||||||
_defaultRunMode = config.DefaultRunMode;
|
_defaultRunMode = config.DefaultRunMode;
|
||||||
if (_defaultRunMode == RunMode.Default)
|
if (_defaultRunMode == RunMode.Default)
|
||||||
|
|||||||
@@ -15,5 +15,8 @@
|
|||||||
|
|
||||||
/// <summary> Determines whether RunMode.Sync commands should push exceptions up to the caller. </summary>
|
/// <summary> Determines whether RunMode.Sync commands should push exceptions up to the caller. </summary>
|
||||||
public bool ThrowOnError { get; set; } = true;
|
public bool ThrowOnError { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary> Determines whether extra parameters should be ignored. </summary>
|
||||||
|
public bool IgnoreExtraArgs { get; set; } = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace Discord.Commands
|
|||||||
private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
|
private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
|
||||||
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
|
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
|
||||||
|
|
||||||
|
private readonly CommandService _commandService;
|
||||||
private readonly Func<ICommandContext, object[], IServiceProvider, CommandInfo, Task> _action;
|
private readonly Func<ICommandContext, object[], IServiceProvider, CommandInfo, Task> _action;
|
||||||
|
|
||||||
public ModuleInfo Module { get; }
|
public ModuleInfo Module { get; }
|
||||||
@@ -64,6 +65,7 @@ namespace Discord.Commands
|
|||||||
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;
|
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;
|
||||||
|
|
||||||
_action = builder.Callback;
|
_action = builder.Callback;
|
||||||
|
_commandService = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null)
|
public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null)
|
||||||
@@ -117,7 +119,7 @@ namespace Discord.Commands
|
|||||||
return ParseResult.FromError(preconditionResult);
|
return ParseResult.FromError(preconditionResult);
|
||||||
|
|
||||||
string input = searchResult.Text.Substring(startIndex);
|
string input = searchResult.Text.Substring(startIndex);
|
||||||
return await CommandParser.ParseArgsAsync(this, context, services, input, 0).ConfigureAwait(false);
|
return await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)
|
public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)
|
||||||
|
|||||||
Reference in New Issue
Block a user