mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-19 16:33:36 +03:00
commitin'
Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
bfd767cf0a
commit
9084bf28a3
4 changed files with 29 additions and 34 deletions
|
@ -586,7 +586,7 @@
|
|||
<value>Report an issue</value>
|
||||
</data>
|
||||
<data name="InvalidTimeSpan" xml:space="preserve">
|
||||
<value>Incorrect time specified.</value>
|
||||
<value>Time specified incorrectly!</value>
|
||||
</data>
|
||||
<data name="UserInfoKicked" xml:space="preserve">
|
||||
<value>Kicked</value>
|
||||
|
|
|
@ -586,7 +586,7 @@
|
|||
<value>Сообщить о проблеме</value>
|
||||
</data>
|
||||
<data name="InvalidTimeSpan" xml:space="preserve">
|
||||
<value>Указано некорректное время.</value>
|
||||
<value>Неправильно указано время!</value>
|
||||
</data>
|
||||
<data name="UserInfoKicked" xml:space="preserve">
|
||||
<value>Выгнан</value>
|
||||
|
|
|
@ -111,7 +111,7 @@ public class RemindCommandGroup : CommandGroup
|
|||
/// <summary>
|
||||
/// A slash command that schedules a reminder with the specified text.
|
||||
/// </summary>
|
||||
/// <param name="stringIn">The period of time which must pass before the reminder will be sent.</param>
|
||||
/// <param name="stringTimeSpan">The period of time which must pass before the reminder will be sent.</param>
|
||||
/// <param name="text">The text of the reminder.</param>
|
||||
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
|
||||
[Command("remind")]
|
||||
|
@ -122,7 +122,7 @@ public class RemindCommandGroup : CommandGroup
|
|||
public async Task<Result> 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<Result> 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<Result> 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))
|
||||
{
|
||||
|
|
|
@ -2,10 +2,17 @@
|
|||
using System.Text.RegularExpressions;
|
||||
using JetBrains.Annotations;
|
||||
using Remora.Commands.Parsers;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Octobot.Parsers;
|
||||
|
||||
/// <summary>
|
||||
/// Parses <see cref="TimeSpan"/>s.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public partial class TimeSpanParser : AbstractTypeParser<TimeSpan>
|
||||
{
|
||||
private static readonly Regex Pattern = ParseRegex();
|
||||
|
||||
/// <summary>
|
||||
/// Parses <see cref="TimeSpan"/> from <see cref="string"/>.
|
||||
/// </summary>
|
||||
|
@ -15,20 +22,13 @@ namespace Octobot.Parsers;
|
|||
/// <remarks>
|
||||
/// If parse wasn't successful, <see cref="TimeSpanParser"/> will return <see cref="TimeSpan.Zero"/>.
|
||||
/// </remarks>
|
||||
[PublicAPI]
|
||||
public partial class TimeSpanParser : AbstractTypeParser<TimeSpan>
|
||||
{
|
||||
private static readonly Regex Pattern = ParseRegex();
|
||||
|
||||
public static Result<TimeSpan> TryParse(string timeSpanString, CancellationToken ct = default)
|
||||
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<TimeSpan>
|
|||
{
|
||||
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<TimeSpan> ParseFromRegex(TimeSpan timeSpan,
|
||||
private static TimeSpan ParseFromRegex(TimeSpan timeSpan,
|
||||
string key, string groupValue, double parsedGroupValue)
|
||||
{
|
||||
if (key is "Years" or "Months")
|
||||
|
|
Loading…
Add table
Reference in a new issue