From 568667d933942cef9e48b15fd26c6156d42b75f2 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 25 Dec 2017 13:19:18 -0500 Subject: [PATCH] Addition of FailOnTooManyArgs --- src/Discord.Net.Commands/CommandParser.cs | 9 +++++++-- src/Discord.Net.Commands/CommandService.cs | 3 ++- src/Discord.Net.Commands/CommandServiceConfig.cs | 3 +++ src/Discord.Net.Commands/Info/CommandInfo.cs | 6 ++++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Discord.Net.Commands/CommandParser.cs b/src/Discord.Net.Commands/CommandParser.cs index 28e36d54d..32241ea4b 100644 --- a/src/Discord.Net.Commands/CommandParser.cs +++ b/src/Discord.Net.Commands/CommandParser.cs @@ -14,7 +14,7 @@ namespace Discord.Commands QuotedParameter } - public static async Task ParseArgsAsync(CommandInfo command, ICommandContext context, IServiceProvider services, string input, int startPos) + public static async Task ParseArgsAsync(CommandInfo command, ICommandContext context, CommandService commandService, IServiceProvider services, string input, int startPos) { ParameterInfo curParam = null; StringBuilder argBuilder = new StringBuilder(input.Length); @@ -109,7 +109,12 @@ namespace Discord.Commands if (argString != null) { if (curParam == null) - return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters."); + { + if (commandService._failOnTooManyArgs) + return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters."); + else + break; + } var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false); if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index cf2b93277..a3a5548ff 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -27,7 +27,7 @@ namespace Discord.Commands private readonly HashSet _moduleDefs; private readonly CommandMap _map; - internal readonly bool _caseSensitive, _throwOnError; + internal readonly bool _caseSensitive, _throwOnError, _failOnTooManyArgs; internal readonly char _separatorChar; internal readonly RunMode _defaultRunMode; internal readonly Logger _cmdLogger; @@ -42,6 +42,7 @@ namespace Discord.Commands { _caseSensitive = config.CaseSensitiveCommands; _throwOnError = config.ThrowOnError; + _failOnTooManyArgs = config.FailOnTooManyArgs; _separatorChar = config.SeparatorChar; _defaultRunMode = config.DefaultRunMode; if (_defaultRunMode == RunMode.Default) diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs index b53b0248c..da1cef4bc 100644 --- a/src/Discord.Net.Commands/CommandServiceConfig.cs +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -15,5 +15,8 @@ /// Determines whether RunMode.Sync commands should push exceptions up to the caller. public bool ThrowOnError { get; set; } = true; + + /// Determines whether the command execution should fail if too many parameters are provided. + public bool FailOnTooManyArgs { get; set; } = true; } } diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 6bb621f94..f467bd0d9 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -18,8 +18,9 @@ namespace Discord.Commands private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList)); private static readonly ConcurrentDictionary, object>> _arrayConverters = new ConcurrentDictionary, object>>(); + private readonly CommandService _commandService; private readonly Func _action; - + public ModuleInfo Module { get; } public string Name { get; } public string Summary { get; } @@ -64,6 +65,7 @@ namespace Discord.Commands HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false; _action = builder.Callback; + _commandService = service; } public async Task CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null) @@ -117,7 +119,7 @@ namespace Discord.Commands return ParseResult.FromError(preconditionResult); string input = searchResult.Text.Substring(startIndex); - return await CommandParser.ParseArgsAsync(this, context, services, input, 0).ConfigureAwait(false); + return await CommandParser.ParseArgsAsync(this, context, _commandService, services, input, 0).ConfigureAwait(false); } public Task ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)