From ecc3d9c729add86cb6bc5acbb61fd01cbd44139f Mon Sep 17 00:00:00 2001 From: Christopher F Date: Fri, 25 Nov 2016 23:56:20 -0500 Subject: [PATCH 1/2] Add configuration option for case insensitive commands Currently, commands are case-sensitive. This PR allows for commands to be case insensitive (which is now the default option). --- src/Discord.Net.Commands/CommandService.cs | 4 +++- src/Discord.Net.Commands/CommandServiceConfig.cs | 2 ++ src/Discord.Net.Commands/Info/CommandInfo.cs | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 9420476d5..26d811440 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -19,6 +19,7 @@ namespace Discord.Commands private readonly ConcurrentBag _moduleDefs; private readonly CommandMap _map; + internal readonly bool _caseSensitive; internal readonly RunMode _defaultRunMode; public IEnumerable Modules => _moduleDefs.Select(x => x); @@ -67,6 +68,7 @@ namespace Discord.Commands [typeof(IGroupUser)] = new UserTypeReader(), [typeof(IGuildUser)] = new UserTypeReader(), }; + _caseSensitive = config.CaseSensitiveCommands; _defaultRunMode = config.DefaultRunMode; } @@ -212,7 +214,7 @@ namespace Discord.Commands public SearchResult Search(CommandContext context, int argPos) => Search(context, context.Message.Content.Substring(argPos)); public SearchResult Search(CommandContext context, string input) { - string lowerInput = input.ToLowerInvariant(); + input = _caseSensitive ? input : input.ToLowerInvariant(); var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray(); if (matches.Length > 0) diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs index 97c98a54c..4ac79fe8f 100644 --- a/src/Discord.Net.Commands/CommandServiceConfig.cs +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -4,5 +4,7 @@ { /// The default RunMode commands should have, if one is not specified on the Command attribute or builder. public RunMode DefaultRunMode { get; set; } = RunMode.Mixed; + /// Should commands be case-sensitive? + public bool CaseSensitiveCommands { get; set; } = false; } } diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 2ca8b1651..b4422e911 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -7,9 +7,11 @@ using System.Threading.Tasks; using System.Reflection; using Discord.Commands.Builders; +using System.Diagnostics; namespace Discord.Commands { + [DebuggerDisplay("{Name,nq}")] public class CommandInfo { private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList)); From d72122eef9d32d8a094effe2cd305ec3196a9e78 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 26 Nov 2016 23:15:36 -0500 Subject: [PATCH 2/2] Resolve conflicts. --- src/Discord.Net.Commands/Info/CommandInfo.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index b4422e911..571c47e13 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -44,13 +44,13 @@ namespace Discord.Commands // both command and module provide aliases if (module.Aliases.Count > 0 && builder.Aliases.Count > 0) - Aliases = module.Aliases.Permutate(builder.Aliases, (first, second) => second != null ? first + " " + second : first).ToImmutableArray(); + Aliases = module.Aliases.Permutate(builder.Aliases, (first, second) => second != null ? first + " " + second : first).Select(x => service._caseSensitive ? x : x.ToLowerInvariant()).ToImmutableArray(); // only module provides aliases else if (module.Aliases.Count > 0) - Aliases = module.Aliases.ToImmutableArray(); + Aliases = module.Aliases.Select(x => service._caseSensitive ? x : x.ToLowerInvariant()).ToImmutableArray(); // only command provides aliases else if (builder.Aliases.Count > 0) - Aliases = builder.Aliases.ToImmutableArray(); + Aliases = builder.Aliases.Select(x => service._caseSensitive ? x : x.ToLowerInvariant()).ToImmutableArray(); // neither provide aliases else throw new InvalidOperationException("Cannot build a command without any aliases");