From d69b90bbd1b8f8e5ec2ecb47e123ae06fa0739d0 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 13 Aug 2018 19:26:11 +0200 Subject: [PATCH] made reminders a bit cooler --- DeukBot4/Database/ReminderHandler.cs | 8 +- DeukBot4/MessageHandlers/MainHandler.cs | 73 +--------------- DeukBot4/MessageHandlers/ReminderHandler.cs | 97 +++++++++++++++++++++ DeukBot4/Program.cs | 1 + 4 files changed, 103 insertions(+), 76 deletions(-) create mode 100644 DeukBot4/MessageHandlers/ReminderHandler.cs diff --git a/DeukBot4/Database/ReminderHandler.cs b/DeukBot4/Database/ReminderHandler.cs index 8503d4d..2427464 100644 --- a/DeukBot4/Database/ReminderHandler.cs +++ b/DeukBot4/Database/ReminderHandler.cs @@ -11,15 +11,15 @@ namespace DeukBot4.Database public static ConnectionMultiplexer Redis = ConnectionMultiplexer.Connect("127.0.0.1"); - public void AddReminder(TimeSpan time, string message, ulong channel, ulong author, ulong recipient) + public async Task AddReminder(TimeSpan time, string message, ulong channel, ulong author, ulong recipient) { try { var db = Redis.GetDatabase(); var id = Guid.NewGuid().ToString(); var expectedTime = DateTime.UtcNow.Add(time); - db.SortedSetAdd("deukbot_reminders", (RedisValue)id, expectedTime.ToBinary()); - db.HashSet((RedisKey) id, new[] + db.SortedSetAddAsync("deukbot_reminders", (RedisValue)id, expectedTime.ToBinary()); + db.HashSetAsync((RedisKey) id, new[] { new HashEntry("channel", channel), new HashEntry("message", message), @@ -62,7 +62,7 @@ namespace DeukBot4.Database db.KeyDelete(val); } - db.SortedSetRemoveRangeByScore("deukbot_reminders", Double.MinValue, desiredTopScore); + db.SortedSetRemoveRangeByScoreAsync("deukbot_reminders", Double.MinValue, desiredTopScore); await Task.Delay(checkTime); await CheckReminders(); diff --git a/DeukBot4/MessageHandlers/MainHandler.cs b/DeukBot4/MessageHandlers/MainHandler.cs index 358c91d..a49c240 100644 --- a/DeukBot4/MessageHandlers/MainHandler.cs +++ b/DeukBot4/MessageHandlers/MainHandler.cs @@ -1,11 +1,5 @@ using System; -using System.Linq; -using System.Text.RegularExpressions; using System.Threading.Tasks; -using System.Web; -using DeukBot4.APIHandlers; -using DeukBot4.Database; -using DeukBot4.Utilities; using Discord; using Discord.WebSocket; @@ -24,7 +18,7 @@ namespace DeukBot4.MessageHandlers #pragma warning disable 4014 CommandHandler.CommandHandler.HandleMessage(message); HandlePrivateMessage(message); - HandleReminder(message); + ReminderHandler.HandleReminder(message); ImageBackupHandler.Backup(message); JokeHandlers.DeltaHandler(message); JokeHandlers.DadJokeHandler(message); @@ -66,70 +60,5 @@ namespace DeukBot4.MessageHandlers } } - private static Regex ReminderMatcher = - new Regex( - @".*(remind\s*((?me)|<@!*(?\d*)>)\s*to)(?.*)(in\s*)(?\d)\s*(?\w*)", - RegexOptions.IgnoreCase); - private static async Task HandleReminder(SocketMessage message) - { - var match = ReminderMatcher.Match(message.Content); - if (!match.Success) - { - return; - } - var recipient = match.Groups["recipient"].Captures[0].Value; - var action = match.Groups["action"].Value.Trim(); - if (string.IsNullOrWhiteSpace(action)) - return; - var timeNumber = double.Parse(match.Groups["timeNum"].Value); - var timeIdentifier = match.Groups["timeId"].Value.Trim(); - TimeSpan timespan; - if (timeIdentifier.ToLowerInvariant().StartsWith("minu")) - { - timespan = TimeSpan.FromMinutes(timeNumber); - } - else if (timeIdentifier.ToLowerInvariant().StartsWith("hour")) - { - timespan = TimeSpan.FromHours(timeNumber); - } - else if (timeIdentifier.ToLowerInvariant().StartsWith("day")) - { - timespan = TimeSpan.FromDays(timeNumber); - } - else if (timeIdentifier.ToLowerInvariant().StartsWith("month")) - { - var dest = DateTime.UtcNow.AddMonths((int) (timeNumber)); - dest = dest.AddDays(timeNumber % 1 * 30); - timespan = dest - DateTime.UtcNow; - } - else if (timeIdentifier.ToLowerInvariant().StartsWith("year")) - { - var dest = DateTime.UtcNow.AddYears((int) (timeNumber)); - dest = dest.AddDays(timeNumber % 1 * 365); - timespan = dest - DateTime.UtcNow; - } - else - { - Logger.Main.LogError("Unknown timespan identifier: " + timeIdentifier); - return; - } - - if (timespan.TotalMinutes < 5) - { - message.Channel.SendMessageAsync("A reminder should be at least 5 minutes in the future"); - return; - } - - if (!ulong.TryParse(recipient, out var recip)) - { - recip = message.Author.Id; - } - - ReminderHandler.Main.AddReminder(timespan, action, message.Channel.Id, message.Author.Id, recip); - message.Channel.SendMessageAsync( - message.Author.Id == recip - ? $"Reminder set! I will remind you in {timespan.ToPrettyFormat()} to {action}" - : $"Reminder set! I will remind <@!{recip}> in {timespan.ToPrettyFormat()} to {action}"); - } } } \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/ReminderHandler.cs b/DeukBot4/MessageHandlers/ReminderHandler.cs new file mode 100644 index 0000000..5d58d0b --- /dev/null +++ b/DeukBot4/MessageHandlers/ReminderHandler.cs @@ -0,0 +1,97 @@ +using System; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using DeukBot4.Utilities; +using Discord.WebSocket; + +namespace DeukBot4.MessageHandlers +{ + public static class ReminderHandler + { + private static Regex ReminderMatcher = + new Regex( + @".*(remind\s*((?me)|<@!*(?\d*)>)\s*to)(?.*)(in\s+)(?