Browse Source

Update TimeSpan Parser

1. If it fails to parse out the already defined formats, fall back to the default, built-in parser.
From my testing, this has greatly improved the ability for this parser to work properly.
pull/1131/head
ericmck2000 GitHub 7 years ago
parent
commit
dc60882577
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 3 deletions
  1. +14
    -3
      src/Discord.Net.Commands/Readers/TimeSpanTypeReader.cs

+ 14
- 3
src/Discord.Net.Commands/Readers/TimeSpanTypeReader.cs View File

@@ -25,11 +25,22 @@ namespace Discord.Commands
"%s's'", // 1s "%s's'", // 1s
}; };



public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{ {
return (TimeSpan.TryParseExact(input.ToLowerInvariant(), _formats, CultureInfo.InvariantCulture, out var timeSpan))
? Task.FromResult(TypeReaderResult.FromSuccess(timeSpan))
: Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to parse TimeSpan"));
//Store as variable, to prevent multiple calls to this method.
string data = input.ToLowerInvariant();

//First, try using the built-in TimeSpan.Parse. (Default Implementation)
if (TimeSpan.TryParseExact(data, _formats, CultureInfo.InvariantCulture, out TimeSpan timeSpan))
return Task.FromResult(TypeReaderResult.FromSuccess(new TimeSpanext(timeSpan)));
//@XtremeOwnage - Try using the regular TimeSpan.TryPrase
else if (TimeSpan.TryParse(data, out TimeSpan Time))
return Task.FromResult(TypeReaderResult.FromSuccess(new TimeSpanext(Time)));
else
{
return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Failed to parse TimeSpan"));
}
} }
} }
} }

Loading…
Cancel
Save