From 3e1268f3bb06a03c6cfbbe3363c9a6117cb85782 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 18 Oct 2022 23:31:34 +0200 Subject: [PATCH] You can now specify time+date (time is optional) in remind instead of relative time, in the format HH:mm dd.MM.YYYY --- .../Modules/Utility/Remind/RemindService.cs | 72 ++++++++++++++----- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/src/NadekoBot/Modules/Utility/Remind/RemindService.cs b/src/NadekoBot/Modules/Utility/Remind/RemindService.cs index 94f348672..f9e87d58a 100644 --- a/src/NadekoBot/Modules/Utility/Remind/RemindService.cs +++ b/src/NadekoBot/Modules/Utility/Remind/RemindService.cs @@ -1,24 +1,24 @@ #nullable disable +using System.Globalization; using LinqToDB; using LinqToDB.EntityFrameworkCore; using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Services.Database.Models; using System.Text.RegularExpressions; -using Nadeko.Common; namespace NadekoBot.Modules.Utility.Services; public class RemindService : INService, IReadyExecutor { private readonly Regex _regex = - new( - @"^(?:in\s?)?\s*(?:(?\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:minutes?|mins?|m),?))?\s+(?:to:?\s+)?(?(?:\r\n|[\r\n]|.)+)", + new(@"^(?:(?:at|on(?:\sthe)?)?\s*(?(?:\d{2}:\d{2}\s)?\d{1,2}\.\d{1,2}(?:\.\d{2,4})?)|(?:in\s?)?\s*(?:(?\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?\d+)(?:\s?(?:minutes?|mins?|m),?))?)\s+(?:to:?\s+)?(?(?:\r\n|[\r\n]|.)+)", RegexOptions.Compiled | RegexOptions.Multiline); - + private readonly DiscordSocketClient _client; private readonly DbService _db; private readonly IBotCredentials _creds; private readonly IEmbedBuilderService _eb; + private readonly CultureInfo _culture; public RemindService( DiscordSocketClient client, @@ -30,6 +30,15 @@ public class RemindService : INService, IReadyExecutor _db = db; _creds = creds; _eb = eb; + + try + { + _culture = new CultureInfo("en-GB"); + } + catch + { + _culture = CultureInfo.InvariantCulture; + } } public async Task OnReadyAsync() @@ -105,32 +114,57 @@ public class RemindService : INService, IReadyExecutor return false; } - foreach (var groupName in _regex.GetGroupNames()) + TimeSpan ts; + + var dateString = m.Groups["date"].Value; + if (!string.IsNullOrWhiteSpace(dateString)) { - if (groupName is "0" or "what") - continue; - if (string.IsNullOrWhiteSpace(m.Groups[groupName].Value)) + var now = DateTime.UtcNow; + + if (!DateTime.TryParse(dateString, _culture, DateTimeStyles.None, out var dt)) { - values[groupName] = 0; - continue; - } - - if (!int.TryParse(m.Groups[groupName].Value, out var value)) - { - Log.Warning("Reminder regex group {GroupName} has invalid value", groupName); + Log.Warning("Invalid remind datetime format"); return false; } - if (value < 1) + if (now >= dt) { - Log.Warning("Reminder time value has to be an integer greater than 0"); + Log.Warning("That remind time has already passed"); return false; } - values[groupName] = value; + ts = dt - now; + } + else + { + foreach (var groupName in _regex.GetGroupNames()) + { + if (groupName is "0" or "what") + continue; + + if (string.IsNullOrWhiteSpace(m.Groups[groupName].Value)) + { + values[groupName] = 0; + continue; + } + + if (!int.TryParse(m.Groups[groupName].Value, out var value)) + { + Log.Warning("Reminder regex group {GroupName} has invalid value", groupName); + return false; + } + + if (value < 1) + { + Log.Warning("Reminder time value has to be an integer greater than 0"); + return false; + } + + values[groupName] = value; + } + ts = new TimeSpan((30 * values["mo"]) + (7 * values["w"]) + values["d"], values["h"], values["m"], 0); } - var ts = new TimeSpan((30 * values["mo"]) + (7 * values["w"]) + values["d"], values["h"], values["m"], 0); obj = new() {