Browse Source

Merge pull request #387 from james7132/timespan-reader

Add TimeSpan TypeReader
tags/1.0-rc
RogueException GitHub 8 years ago
parent
commit
aceac76d1d
2 changed files with 10 additions and 9 deletions
  1. +9
    -9
      src/Discord.Net.Commands/CommandService.cs
  2. +1
    -0
      src/Discord.Net.Commands/PrimitiveParsers.cs

+ 9
- 9
src/Discord.Net.Commands/CommandService.cs View File

@@ -14,7 +14,7 @@ namespace Discord.Commands
public class CommandService public class CommandService
{ {
private readonly SemaphoreSlim _moduleLock; private readonly SemaphoreSlim _moduleLock;
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
private readonly ConcurrentDictionary<Type, ModuleInfo> _typedModuleDefs;
private readonly ConcurrentDictionary<Type, TypeReader> _typeReaders; private readonly ConcurrentDictionary<Type, TypeReader> _typeReaders;
private readonly ConcurrentBag<ModuleInfo> _moduleDefs; private readonly ConcurrentBag<ModuleInfo> _moduleDefs;
private readonly CommandMap _map; private readonly CommandMap _map;
@@ -50,7 +50,7 @@ namespace Discord.Commands
[typeof(decimal)] = new SimpleTypeReader<decimal>(), [typeof(decimal)] = new SimpleTypeReader<decimal>(),
[typeof(DateTime)] = new SimpleTypeReader<DateTime>(), [typeof(DateTime)] = new SimpleTypeReader<DateTime>(),
[typeof(DateTimeOffset)] = new SimpleTypeReader<DateTimeOffset>(), [typeof(DateTimeOffset)] = new SimpleTypeReader<DateTimeOffset>(),
[typeof(TimeSpan)] = new SimpleTypeReader<TimeSpan>(),
[typeof(IMessage)] = new MessageTypeReader<IMessage>(), [typeof(IMessage)] = new MessageTypeReader<IMessage>(),
[typeof(IUserMessage)] = new MessageTypeReader<IUserMessage>(), [typeof(IUserMessage)] = new MessageTypeReader<IUserMessage>(),
[typeof(IChannel)] = new ChannelTypeReader<IChannel>(), [typeof(IChannel)] = new ChannelTypeReader<IChannel>(),
@@ -105,7 +105,7 @@ namespace Discord.Commands
throw new InvalidOperationException($"Could not build the module {typeof(T).FullName}, did you pass an invalid type?"); throw new InvalidOperationException($"Could not build the module {typeof(T).FullName}, did you pass an invalid type?");


_typedModuleDefs[module.Key] = module.Value; _typedModuleDefs[module.Key] = module.Value;
return LoadModuleInternal(module.Value); return LoadModuleInternal(module.Value);
} }
finally finally
@@ -143,7 +143,7 @@ namespace Discord.Commands


foreach (var submodule in module.Submodules) foreach (var submodule in module.Submodules)
LoadModuleInternal(submodule); LoadModuleInternal(submodule);
return module; return module;
} }


@@ -168,7 +168,7 @@ namespace Discord.Commands
_typedModuleDefs.TryGetValue(typeof(T), out module); _typedModuleDefs.TryGetValue(typeof(T), out module);
if (module == default(ModuleInfo)) if (module == default(ModuleInfo))
return false; return false;
return RemoveModuleInternal(module); return RemoveModuleInternal(module);
} }
finally finally
@@ -181,7 +181,7 @@ namespace Discord.Commands
var defsRemove = module; var defsRemove = module;
if (!_moduleDefs.TryTake(out defsRemove)) if (!_moduleDefs.TryTake(out defsRemove))
return false; return false;
foreach (var cmd in module.Commands) foreach (var cmd in module.Commands)
_map.RemoveCommand(cmd); _map.RemoveCommand(cmd);


@@ -216,14 +216,14 @@ namespace Discord.Commands
{ {
input = _caseSensitive ? input : input.ToLowerInvariant(); input = _caseSensitive ? input : input.ToLowerInvariant();
var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray(); var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray();
if (matches.Length > 0) if (matches.Length > 0)
return SearchResult.FromSuccess(input, matches); return SearchResult.FromSuccess(input, matches);
else else
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command."); return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
} }


public Task<IResult> ExecuteAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
public Task<IResult> ExecuteAsync(CommandContext context, int argPos, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
=> ExecuteAsync(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling); => ExecuteAsync(context, context.Message.Content.Substring(argPos), dependencyMap, multiMatchHandling);
public async Task<IResult> ExecuteAsync(CommandContext context, string input, IDependencyMap dependencyMap = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) public async Task<IResult> 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 await commands[i].Execute(context, parseResult, dependencyMap).ConfigureAwait(false);
} }
return SearchResult.FromError(CommandError.UnknownCommand, "This input does not match any overload."); return SearchResult.FromError(CommandError.UnknownCommand, "This input does not match any overload.");
} }
} }


+ 1
- 0
src/Discord.Net.Commands/PrimitiveParsers.cs View File

@@ -27,6 +27,7 @@ namespace Discord.Commands
parserBuilder[typeof(decimal)] = (TryParseDelegate<decimal>)decimal.TryParse; parserBuilder[typeof(decimal)] = (TryParseDelegate<decimal>)decimal.TryParse;
parserBuilder[typeof(DateTime)] = (TryParseDelegate<DateTime>)DateTime.TryParse; parserBuilder[typeof(DateTime)] = (TryParseDelegate<DateTime>)DateTime.TryParse;
parserBuilder[typeof(DateTimeOffset)] = (TryParseDelegate<DateTimeOffset>)DateTimeOffset.TryParse; parserBuilder[typeof(DateTimeOffset)] = (TryParseDelegate<DateTimeOffset>)DateTimeOffset.TryParse;
parserBuilder[typeof(TimeSpan)] = (TryParseDelegate<TimeSpan>)TimeSpan.TryParse;
parserBuilder[typeof(char)] = (TryParseDelegate<char>)char.TryParse; parserBuilder[typeof(char)] = (TryParseDelegate<char>)char.TryParse;
parserBuilder[typeof(string)] = (TryParseDelegate<string>)delegate (string str, out string value) parserBuilder[typeof(string)] = (TryParseDelegate<string>)delegate (string str, out string value)
{ {


Loading…
Cancel
Save