diff --git a/src/Discord.Net.Commands/Attributes/IgnoreExtraArgsAttribute.cs b/src/Discord.Net.Commands/Attributes/IgnoreExtraArgsAttribute.cs new file mode 100644 index 000000000..c359e8287 --- /dev/null +++ b/src/Discord.Net.Commands/Attributes/IgnoreExtraArgsAttribute.cs @@ -0,0 +1,17 @@ +using System; + +namespace Discord.Commands +{ + /// Set whether or not to ignore extra arguments for an individual command method or module, + /// overriding the setting in if necessary. + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public sealed class IgnoreExtraArgsAttribute : Attribute + { + public bool IgnoreValue { get; } + + public IgnoreExtraArgsAttribute(bool ignoreValue) + { + IgnoreValue = ignoreValue; + } + } +} diff --git a/src/Discord.Net.Commands/Builders/CommandBuilder.cs b/src/Discord.Net.Commands/Builders/CommandBuilder.cs index b6d002c70..7911e0703 100644 --- a/src/Discord.Net.Commands/Builders/CommandBuilder.cs +++ b/src/Discord.Net.Commands/Builders/CommandBuilder.cs @@ -1,8 +1,7 @@ -using System; +using System; using System.Linq; using System.Threading.Tasks; using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; namespace Discord.Commands.Builders { @@ -22,6 +21,7 @@ namespace Discord.Commands.Builders public string PrimaryAlias { get; set; } public RunMode RunMode { get; set; } public int Priority { get; set; } + public bool IgnoreExtraArgs { get; set; } public IReadOnlyList Preconditions => _preconditions; public IReadOnlyList Parameters => _parameters; @@ -32,6 +32,7 @@ namespace Discord.Commands.Builders internal CommandBuilder(ModuleBuilder module) { Module = module; + IgnoreExtraArgs = module.IgnoreExtraArgs; _preconditions = new List(); _parameters = new List(); @@ -140,4 +141,4 @@ namespace Discord.Commands.Builders return new CommandInfo(this, info, service); } } -} \ No newline at end of file +} diff --git a/src/Discord.Net.Commands/Builders/ModuleBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleBuilder.cs index 1809c2c63..d774de716 100644 --- a/src/Discord.Net.Commands/Builders/ModuleBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleBuilder.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; namespace Discord.Commands.Builders { @@ -20,6 +19,7 @@ namespace Discord.Commands.Builders public string Summary { get; set; } public string Remarks { get; set; } public string Group { get; set; } + public bool IgnoreExtraArgs { get; set; } public IReadOnlyList Commands => _commands; public IReadOnlyList Modules => _submodules; @@ -34,6 +34,7 @@ namespace Discord.Commands.Builders { Service = service; Parent = parent; + IgnoreExtraArgs = service._ignoreExtraArgs; _commands = new List(); _submodules = new List(); diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs index e9ce9eb86..58e55d76d 100644 --- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs @@ -123,6 +123,9 @@ namespace Discord.Commands case PreconditionAttribute precondition: builder.AddPrecondition(precondition); break; + case IgnoreExtraArgsAttribute ignoreExtra: + builder.IgnoreExtraArgs = ignoreExtra.IgnoreValue; + break; default: builder.AddAttributes(attribute); break; @@ -177,6 +180,9 @@ namespace Discord.Commands case PreconditionAttribute precondition: builder.AddPrecondition(precondition); break; + case IgnoreExtraArgsAttribute ignoreExtra: + builder.IgnoreExtraArgs = ignoreExtra.IgnoreValue; + break; default: builder.AddAttributes(attribute); break; diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index f4fbcf8b2..611693cc1 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; using Discord.Commands.Builders; using Discord.Logging; diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs index 77c5b2262..f5cd14fef 100644 --- a/src/Discord.Net.Commands/CommandServiceConfig.cs +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -20,11 +20,5 @@ namespace Discord.Commands /// Determines whether extra parameters should be ignored. public bool IgnoreExtraArgs { get; set; } = false; - - ///// Gets or sets the to use. - //public IServiceProvider ServiceProvider { get; set; } = null; - - ///// Gets or sets a factory function for the to use. - //public Func ServiceProviderFactory { get; set; } = null; } } diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index f0d406e8d..91305239c 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -27,6 +27,7 @@ namespace Discord.Commands public string Remarks { get; } public int Priority { get; } public bool HasVarArgs { get; } + public bool IgnoreExtraArgs { get; } public RunMode RunMode { get; } public IReadOnlyList Aliases { get; } @@ -63,6 +64,7 @@ namespace Discord.Commands Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray(); HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false; + IgnoreExtraArgs = builder.IgnoreExtraArgs; _action = builder.Callback; _commandService = service; @@ -119,7 +121,7 @@ namespace Discord.Commands return ParseResult.FromError(preconditionResult); string input = searchResult.Text.Substring(startIndex); - return await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0).ConfigureAwait(false); + return await CommandParser.ParseArgsAsync(this, context, IgnoreExtraArgs, services, input, 0).ConfigureAwait(false); } public Task ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services) diff --git a/src/Discord.Net.Commands/Info/ModuleInfo.cs b/src/Discord.Net.Commands/Info/ModuleInfo.cs index 5a7f9208e..02cac00a6 100644 --- a/src/Discord.Net.Commands/Info/ModuleInfo.cs +++ b/src/Discord.Net.Commands/Info/ModuleInfo.cs @@ -2,7 +2,6 @@ using System; using System.Linq; using System.Collections.Generic; using System.Collections.Immutable; -using System.Reflection; using Discord.Commands.Builders; namespace Discord.Commands @@ -14,6 +13,7 @@ namespace Discord.Commands public string Summary { get; } public string Remarks { get; } public string Group { get; } + public bool IgnoreExtraArgs { get; } public IReadOnlyList Aliases { get; } public IReadOnlyList Commands { get; } @@ -23,8 +23,6 @@ namespace Discord.Commands public ModuleInfo Parent { get; } public bool IsSubmodule => Parent != null; - //public TypeInfo TypeInfo { get; } - internal ModuleInfo(ModuleBuilder builder, CommandService service, IServiceProvider services, ModuleInfo parent = null) { Service = service; @@ -33,10 +31,9 @@ namespace Discord.Commands Summary = builder.Summary; Remarks = builder.Remarks; Group = builder.Group; + IgnoreExtraArgs = builder.IgnoreExtraArgs; Parent = parent; - //TypeInfo = builder.TypeInfo; - Aliases = BuildAliases(builder, service).ToImmutableArray(); Commands = builder.Commands.Select(x => x.Build(this, service)).ToImmutableArray(); Preconditions = BuildPreconditions(builder).ToImmutableArray();