From 3e3566618647a0d00c91cae78bbe4bd2d42c9c4f Mon Sep 17 00:00:00 2001 From: james7132 Date: Sun, 27 Nov 2016 01:57:12 -0800 Subject: [PATCH] Add TimeSpan TypeReader --- src/Discord.Net.Commands/CommandService.cs | 18 +++++++++--------- src/Discord.Net.Commands/PrimitiveParsers.cs | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 26d811440..b6659fea3 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -14,7 +14,7 @@ namespace Discord.Commands public class CommandService { private readonly SemaphoreSlim _moduleLock; - private readonly ConcurrentDictionary _typedModuleDefs; + private readonly ConcurrentDictionary _typedModuleDefs; private readonly ConcurrentDictionary _typeReaders; private readonly ConcurrentBag _moduleDefs; private readonly CommandMap _map; @@ -50,7 +50,7 @@ namespace Discord.Commands [typeof(decimal)] = new SimpleTypeReader(), [typeof(DateTime)] = new SimpleTypeReader(), [typeof(DateTimeOffset)] = new SimpleTypeReader(), - + [typeof(TimeSpan)] = new SimpleTypeReader(), [typeof(IMessage)] = new MessageTypeReader(), [typeof(IUserMessage)] = new MessageTypeReader(), [typeof(IChannel)] = new ChannelTypeReader(), @@ -105,7 +105,7 @@ namespace Discord.Commands throw new InvalidOperationException($"Could not build the module {typeof(T).FullName}, did you pass an invalid type?"); _typedModuleDefs[module.Key] = module.Value; - + return LoadModuleInternal(module.Value); } finally @@ -143,7 +143,7 @@ namespace Discord.Commands foreach (var submodule in module.Submodules) LoadModuleInternal(submodule); - + return module; } @@ -168,7 +168,7 @@ namespace Discord.Commands _typedModuleDefs.TryGetValue(typeof(T), out module); if (module == default(ModuleInfo)) return false; - + return RemoveModuleInternal(module); } finally @@ -181,7 +181,7 @@ namespace Discord.Commands var defsRemove = module; if (!_moduleDefs.TryTake(out defsRemove)) return false; - + foreach (var cmd in module.Commands) _map.RemoveCommand(cmd); @@ -216,14 +216,14 @@ namespace Discord.Commands { input = _caseSensitive ? input : input.ToLowerInvariant(); var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray(); - + if (matches.Length > 0) return SearchResult.FromSuccess(input, matches); else return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command."); } - public Task ExecuteAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) + public Task ExecuteAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) => ExecuteAsync(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling); public async Task ExecuteAsync(CommandContext context, string input, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) { @@ -272,7 +272,7 @@ namespace Discord.Commands return await commands[i].Execute(context, parseResult, dependencyMap).ConfigureAwait(false); } - + return SearchResult.FromError(CommandError.UnknownCommand, "This input does not match any overload."); } } diff --git a/src/Discord.Net.Commands/PrimitiveParsers.cs b/src/Discord.Net.Commands/PrimitiveParsers.cs index 5e3dcd68a..0b00a90e0 100644 --- a/src/Discord.Net.Commands/PrimitiveParsers.cs +++ b/src/Discord.Net.Commands/PrimitiveParsers.cs @@ -27,6 +27,7 @@ namespace Discord.Commands parserBuilder[typeof(decimal)] = (TryParseDelegate)decimal.TryParse; parserBuilder[typeof(DateTime)] = (TryParseDelegate)DateTime.TryParse; parserBuilder[typeof(DateTimeOffset)] = (TryParseDelegate)DateTimeOffset.TryParse; + parserBuilder[typeof(TimeSpan)] = (TryParseDelegate)TimeSpan.TryParse; parserBuilder[typeof(char)] = (TryParseDelegate)char.TryParse; parserBuilder[typeof(string)] = (TryParseDelegate)delegate (string str, out string value) {