diff --git a/locale/Messages.resx b/locale/Messages.resx index b7421f6..adc9f6d 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -586,7 +586,7 @@ Report an issue - Incorrect time specified. + Time specified incorrectly! Kicked diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index 4b3541a..de2158d 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -586,7 +586,7 @@ Сообщить о проблеме - Указано некорректное время. + Неправильно указано время! Выгнан diff --git a/src/Commands/RemindCommandGroup.cs b/src/Commands/RemindCommandGroup.cs index 5a8c3fb..57ae656 100644 --- a/src/Commands/RemindCommandGroup.cs +++ b/src/Commands/RemindCommandGroup.cs @@ -111,7 +111,7 @@ public class RemindCommandGroup : CommandGroup /// /// A slash command that schedules a reminder with the specified text. /// - /// The period of time which must pass before the reminder will be sent. + /// The period of time which must pass before the reminder will be sent. /// The text of the reminder. /// A feedback sending result which may or may not have succeeded. [Command("remind")] @@ -122,7 +122,7 @@ public class RemindCommandGroup : CommandGroup public async Task ExecuteReminderAsync( [Description("After what period of time mention the reminder")] [Option("in")] - string stringIn, + string stringTimeSpan, [Description("Reminder text")] [MaxLength(512)] string text) { @@ -146,30 +146,25 @@ public class RemindCommandGroup : CommandGroup var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); - return await AddReminderAsync(stringIn, text, data, channelId, bot, executor, CancellationToken); - } - - private async Task AddReminderAsync(string stringIn, string text, GuildData data, - Snowflake channelId, IUser bot, IUser executor, CancellationToken ct = default) - { - var parseResult = TimeSpanParser.TryParse(stringIn, ct); - if (!parseResult.IsDefined(out var timeSpanIn)) - { - return Result.FromError(parseResult); - } - - if (timeSpanIn == TimeSpan.Zero) + var timeSpan = TimeSpanParser.TryParse(stringTimeSpan, CancellationToken); + if (timeSpan.Equals(TimeSpan.Zero)) { var failedEmbed = new EmbedBuilder() .WithSmallTitle(Messages.InvalidTimeSpan, bot) .WithColour(ColorsList.Red) .Build(); - return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct); + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken); } + return await AddReminderAsync(timeSpan, text, data, channelId, executor, CancellationToken); + } + + private async Task AddReminderAsync(TimeSpan timeSpan, string text, GuildData data, + Snowflake channelId, IUser executor, CancellationToken ct = default) + { var memberData = data.GetOrCreateMemberData(executor.ID); - var remindAt = DateTimeOffset.UtcNow.Add(timeSpanIn); + var remindAt = DateTimeOffset.UtcNow.Add(timeSpan); var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct); if (!responseResult.IsDefined(out var response)) { diff --git a/src/Parsers/TimeSpanParser.cs b/src/Parsers/TimeSpanParser.cs index a8d2aa5..e65ac08 100644 --- a/src/Parsers/TimeSpanParser.cs +++ b/src/Parsers/TimeSpanParser.cs @@ -2,33 +2,33 @@ using System.Text.RegularExpressions; using JetBrains.Annotations; using Remora.Commands.Parsers; -using Remora.Results; namespace Octobot.Parsers; /// -/// Parses from . +/// Parses s. /// -/// -/// Parsed . -/// -/// -/// If parse wasn't successful, will return . -/// [PublicAPI] public partial class TimeSpanParser : AbstractTypeParser { private static readonly Regex Pattern = ParseRegex(); - public static Result TryParse(string timeSpanString, CancellationToken ct = default) + /// + /// Parses from . + /// + /// + /// Parsed . + /// + /// + /// If parse wasn't successful, will return . + /// + public static TimeSpan TryParse(string timeSpanString, CancellationToken ct = default) { if (timeSpanString.StartsWith('-')) { return TimeSpan.Zero; } - timeSpanString = timeSpanString.Trim(); - if (TimeSpan.TryParse(timeSpanString, DateTimeFormatInfo.InvariantInfo, out var parsedTimeSpan)) { return parsedTimeSpan; @@ -49,16 +49,16 @@ public partial class TimeSpanParser : AbstractTypeParser { foreach ((var key, var groupValue) in groups) { - return !double.TryParse(groupValue, out var parsedGroupValue) - ? TimeSpan.Zero - : ParseFromRegex(timeSpan, key, groupValue, parsedGroupValue); + return double.TryParse(groupValue, out var parsedGroupValue) + ? ParseFromRegex(timeSpan, key, groupValue, parsedGroupValue) + : TimeSpan.Zero; } } return timeSpan; } - private static Result ParseFromRegex(TimeSpan timeSpan, + private static TimeSpan ParseFromRegex(TimeSpan timeSpan, string key, string groupValue, double parsedGroupValue) { if (key is "Years" or "Months")