Fixed reminder regex to not be as greedy

This commit is contained in:
Deukhoofd 2019-07-25 13:20:30 +02:00
parent 7e6da01a76
commit 66ea585a3c
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 19 additions and 3 deletions

View File

@ -81,7 +81,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
}
catch (Exception e)
{
await Logger.Main.Log("An error occured: \n" + e);
await Logger.Main.LogError(e);
}
break;
case CommandRequest.RequestCode.UnknownCommand:

View File

@ -10,9 +10,25 @@ namespace DeukBot4.MessageHandlers
{
private static readonly Regex ReminderMatcher =
new Regex(
@".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)(\s+to(?<action>.+)|\s+in\s+(?<time>.+)){2}",
@".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)(\s+to(?<action>.+)|\s+in\s+(?<time>((?!to).)*)){2}",
RegexOptions.IgnoreCase);
/*
* Regex explained:
* .*
* grab any token preceding
* (remind\s*
* look for the word "remind", followed by a whitespace
* ((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)
* look for either "me", or a discord ping or id, and save it as "recipient" group
* (\s+to(?<action>.+)
* look for the word "to", then followed by a string of what we should remember to do
* \s+in\s+(?<time>((?!to).)*))
* in a specified time. We do a negative lookup for the word "to" here, so that they don't get eaten
* (\s+to(?<action>.+)|\s+in\s+(?<time>[^to]*)){2}
* The action and time can be in either order.
*/
public static async Task HandleReminder(ReceivedMessage receivedMessage)
{
var match = ReminderMatcher.Match(receivedMessage.Message.Content);
@ -58,7 +74,7 @@ namespace DeukBot4.MessageHandlers
message.Channel.SendMessageAsync(embed: embed.Build());
}
private static Regex TimespanMatcher =
private static readonly Regex TimespanMatcher =
new Regex(@"(?<timeNumber>\d+.?\d*|[\w\s-]+)\s*(?<timeId>minutes*|hours*|days*|weeks*|months*|years*)\W*(and )*",
RegexOptions.IgnoreCase);
private static TimeSpan? ParseTime(string message)