2018-08-13 17:26:11 +00:00
|
|
|
using System;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading.Tasks;
|
2018-10-27 11:57:37 +00:00
|
|
|
using DeukBot4.MessageHandlers.CommandHandler;
|
2018-08-13 17:26:11 +00:00
|
|
|
using DeukBot4.Utilities;
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
|
|
|
namespace DeukBot4.MessageHandlers
|
|
|
|
{
|
|
|
|
public static class ReminderHandler
|
|
|
|
{
|
2018-10-27 11:50:47 +00:00
|
|
|
private static readonly Regex ReminderMatcher =
|
2018-08-13 17:26:11 +00:00
|
|
|
new Regex(
|
2018-10-27 12:12:11 +00:00
|
|
|
@".*(remind\s*((?<recipient>me)|<@!*(?<recipient>\d*)>)\s*)(\s+to(?<action>.+)|\s+in\s+(?<time>.+)){2}",
|
2018-08-13 17:26:11 +00:00
|
|
|
RegexOptions.IgnoreCase);
|
|
|
|
|
2019-01-29 22:28:52 +00:00
|
|
|
public static async Task HandleReminder(ReceivedMessage receivedMessage)
|
2018-08-13 17:26:11 +00:00
|
|
|
{
|
2019-01-29 22:28:52 +00:00
|
|
|
var match = ReminderMatcher.Match(receivedMessage.Message.Content);
|
2018-08-13 17:26:11 +00:00
|
|
|
if (!match.Success)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-01-29 22:28:52 +00:00
|
|
|
if (receivedMessage.IsHandled)
|
|
|
|
return;
|
|
|
|
receivedMessage.IsHandled = true;
|
|
|
|
var message = receivedMessage.Message;
|
2018-08-13 17:26:11 +00:00
|
|
|
|
|
|
|
var recipient = match.Groups["recipient"].Captures[0].Value;
|
2018-10-27 12:12:11 +00:00
|
|
|
var action = match.Groups["action"]?.Value.Trim();
|
2018-08-13 17:26:11 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(action))
|
|
|
|
return;
|
2018-10-27 12:12:11 +00:00
|
|
|
var time = match.Groups["time"]?.Value;
|
|
|
|
if (string.IsNullOrWhiteSpace(time))
|
|
|
|
return;
|
2018-08-13 17:26:11 +00:00
|
|
|
var timespan = ParseTime(time);
|
|
|
|
if (!timespan.HasValue)
|
|
|
|
return;
|
|
|
|
|
2018-10-27 11:57:37 +00:00
|
|
|
var embed = EmbedFactory.GetStandardEmbedBuilder();
|
|
|
|
embed.Title = "Reminder";
|
2018-08-13 17:26:11 +00:00
|
|
|
if (timespan.Value.TotalMinutes < 5)
|
|
|
|
{
|
2018-10-27 11:57:37 +00:00
|
|
|
embed.Description = "A reminder should be at least 5 minutes in the future";
|
|
|
|
message.Channel.SendMessageAsync("", embed: embed.Build());
|
2018-08-13 17:26:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ulong.TryParse(recipient, out var recip))
|
|
|
|
{
|
|
|
|
recip = message.Author.Id;
|
|
|
|
}
|
|
|
|
|
2018-10-27 11:57:37 +00:00
|
|
|
embed.Description = message.Author.Id.Equals(recip)
|
|
|
|
? $"Reminder set! I will remind you in {timespan.Value.ToPrettyFormat()} to {action}"
|
|
|
|
: $"Reminder set! I will remind <@!{recip}> in {timespan.Value.ToPrettyFormat()} to {action}";
|
|
|
|
|
2018-08-13 17:26:11 +00:00
|
|
|
Database.ReminderHandler.Main.AddReminder(timespan.Value, action, message.Channel.Id, message.Author.Id, recip);
|
2018-10-27 11:57:37 +00:00
|
|
|
message.Channel.SendMessageAsync(embed: embed.Build());
|
2018-08-13 17:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Regex TimespanMatcher =
|
2018-10-27 11:50:47 +00:00
|
|
|
new Regex(@"(?<timeNumber>\d+.?\d*|[\w\s-]+)\s*(?<timeId>minutes*|hours*|days*|weeks*|months*|years*)\W*(and )*",
|
2018-08-13 17:26:11 +00:00
|
|
|
RegexOptions.IgnoreCase);
|
|
|
|
private static TimeSpan? ParseTime(string message)
|
|
|
|
{
|
|
|
|
var matches = TimespanMatcher.Matches(message);
|
|
|
|
if (matches.Count == 0)
|
|
|
|
return null;
|
|
|
|
var timespan = new TimeSpan();
|
|
|
|
foreach (Match match in matches)
|
|
|
|
{
|
|
|
|
if (!match.Success)
|
|
|
|
continue;
|
|
|
|
var timeId = match.Groups["timeId"].Value.ToLowerInvariant();
|
2018-10-27 11:50:47 +00:00
|
|
|
var amountString = match.Groups["timeNumber"].Value;
|
|
|
|
if (!double.TryParse(amountString, out var timeAmount))
|
|
|
|
{
|
|
|
|
if (!EnglishNumberParser.ConvertNumberString(amountString, out timeAmount))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 17:26:11 +00:00
|
|
|
if (timeId.StartsWith("minute"))
|
|
|
|
timespan += TimeSpan.FromMinutes(timeAmount);
|
|
|
|
else if (timeId.StartsWith("hour"))
|
|
|
|
timespan += TimeSpan.FromHours(timeAmount);
|
|
|
|
else if (timeId.StartsWith("day"))
|
|
|
|
timespan += TimeSpan.FromDays(timeAmount);
|
|
|
|
else if (timeId.StartsWith("week"))
|
|
|
|
timespan += TimeSpan.FromDays(timeAmount * 7);
|
|
|
|
else if (timeId.StartsWith("month"))
|
|
|
|
{
|
|
|
|
var target = DateTime.UtcNow.AddMonths((int) timeAmount);
|
|
|
|
target = target.AddDays(timeAmount % 1 * 30);
|
|
|
|
timespan += (target - DateTime.UtcNow);
|
|
|
|
}
|
|
|
|
else if (timeId.StartsWith("month"))
|
|
|
|
{
|
|
|
|
var target = DateTime.UtcNow.AddMonths((int) timeAmount);
|
|
|
|
target = target.AddDays(timeAmount % 1 * 30);
|
|
|
|
timespan += (target - DateTime.UtcNow);
|
|
|
|
}
|
|
|
|
else if (timeId.StartsWith("year"))
|
|
|
|
{
|
|
|
|
var target = DateTime.UtcNow.AddYears((int) timeAmount);
|
|
|
|
target = target.AddDays(timeAmount % 1 * 365);
|
|
|
|
timespan += (target - DateTime.UtcNow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return timespan;
|
|
|
|
}
|
2018-10-27 11:50:47 +00:00
|
|
|
|
2018-08-13 17:26:11 +00:00
|
|
|
}
|
|
|
|
}
|