1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-19 16:33:36 +03:00

what if i just

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2023-12-23 20:42:48 +03:00
parent 6a512b13fd
commit 49215f628a
Signed by: mctaylors
GPG key ID: 7181BEBE676903C1
7 changed files with 48 additions and 34 deletions

View file

@ -585,7 +585,7 @@
<data name="ButtonReportIssue" xml:space="preserve"> <data name="ButtonReportIssue" xml:space="preserve">
<value>Report an issue</value> <value>Report an issue</value>
</data> </data>
<data name="ReminderInvalidTimeSpan" xml:space="preserve"> <data name="InvalidTimeSpan" xml:space="preserve">
<value>Incorrect reminder send time specified.</value> <value>Incorrect time specified.</value>
</data> </data>
</root> </root>

View file

@ -585,7 +585,7 @@
<data name="ButtonReportIssue" xml:space="preserve"> <data name="ButtonReportIssue" xml:space="preserve">
<value>Сообщить о проблеме</value> <value>Сообщить о проблеме</value>
</data> </data>
<data name="ReminderInvalidTimeSpan" xml:space="preserve"> <data name="InvalidTimeSpan" xml:space="preserve">
<value>Указано некорректное время отправки напоминания.</value> <value>Указано некорректное время.</value>
</data> </data>
</root> </root>

View file

@ -585,7 +585,7 @@
<data name="ButtonReportIssue" xml:space="preserve"> <data name="ButtonReportIssue" xml:space="preserve">
<value>зарепортить баг</value> <value>зарепортить баг</value>
</data> </data>
<data name="ReminderInvalidTimeSpan" xml:space="preserve"> <data name="InvalidTimeSpan" xml:space="preserve">
<value>ты там правильно напиши когда мне тебя пинать</value> <value>ты там правильно напиши таймспан</value>
</data> </data>
</root> </root>

View file

@ -7,7 +7,6 @@ using Octobot.Extensions;
using Octobot.Services; using Octobot.Services;
using Remora.Commands.Attributes; using Remora.Commands.Attributes;
using Remora.Commands.Groups; using Remora.Commands.Groups;
using Remora.Commands.Parsers;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest; using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Commands.Attributes; using Remora.Discord.Commands.Attributes;
@ -18,6 +17,7 @@ using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Extensions.Formatting; using Remora.Discord.Extensions.Formatting;
using Remora.Rest.Core; using Remora.Rest.Core;
using Remora.Results; using Remora.Results;
using TimeSpanParser = Octobot.Parsers.TimeSpanParser;
namespace Octobot.Commands; namespace Octobot.Commands;
@ -111,7 +111,7 @@ public class RemindCommandGroup : CommandGroup
/// <summary> /// <summary>
/// A slash command that schedules a reminder with the specified text. /// A slash command that schedules a reminder with the specified text.
/// </summary> /// </summary>
/// <param name="timeSpanString">The period of time which must pass before the reminder will be sent.</param> /// <param name="stringIn">The period of time which must pass before the reminder will be sent.</param>
/// <param name="text">The text of the reminder.</param> /// <param name="text">The text of the reminder.</param>
/// <returns>A feedback sending result which may or may not have succeeded.</returns> /// <returns>A feedback sending result which may or may not have succeeded.</returns>
[Command("remind")] [Command("remind")]
@ -122,7 +122,7 @@ public class RemindCommandGroup : CommandGroup
public async Task<Result> ExecuteReminderAsync( public async Task<Result> ExecuteReminderAsync(
[Description("After what period of time mention the reminder")] [Description("After what period of time mention the reminder")]
[Option("in")] [Option("in")]
string timeSpanString, string stringIn,
[Description("Reminder text")] [MaxLength(512)] [Description("Reminder text")] [MaxLength(512)]
string text) string text)
{ {
@ -146,17 +146,22 @@ public class RemindCommandGroup : CommandGroup
var data = await _guildData.GetData(guildId, CancellationToken); var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings); Messages.Culture = GuildSettings.Language.Get(data.Settings);
return await AddReminderAsync(timeSpanString, text, data, channelId, bot, executor, CancellationToken); return await AddReminderAsync(stringIn, text, data, channelId, bot, executor, CancellationToken);
} }
private async Task<Result> AddReminderAsync(string timeSpanString, string text, GuildData data, private async Task<Result> AddReminderAsync(string stringIn, string text, GuildData data,
Snowflake channelId, IUser bot, IUser executor, CancellationToken ct = default) Snowflake channelId, IUser bot, IUser executor, CancellationToken ct = default)
{ {
var timeSpan = await ParseReminderAsync(timeSpanString, ct); var parseResult = TimeSpanParser.TryParse(stringIn, ct);
if (timeSpan == TimeSpan.Zero) if (!parseResult.IsDefined(out var timeSpanIn))
{
return Result.FromError(parseResult);
}
if (timeSpanIn == TimeSpan.Zero)
{ {
var failedEmbed = new EmbedBuilder() var failedEmbed = new EmbedBuilder()
.WithSmallTitle(Messages.ReminderInvalidTimeSpan, bot) .WithSmallTitle(Messages.InvalidTimeSpan, bot)
.WithColour(ColorsList.Red) .WithColour(ColorsList.Red)
.Build(); .Build();
@ -164,7 +169,7 @@ public class RemindCommandGroup : CommandGroup
} }
var memberData = data.GetOrCreateMemberData(executor.ID); var memberData = data.GetOrCreateMemberData(executor.ID);
var remindAt = DateTimeOffset.UtcNow.Add(timeSpan); var remindAt = DateTimeOffset.UtcNow.Add(timeSpanIn);
var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct); var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct);
if (!responseResult.IsDefined(out var response)) if (!responseResult.IsDefined(out var response))
{ {
@ -193,19 +198,6 @@ public class RemindCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct); return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
} }
private static async Task<TimeSpan> ParseReminderAsync(string timeSpanString, CancellationToken ct)
{
if (timeSpanString.StartsWith('-'))
{
return TimeSpan.Zero;
}
var parseResult = await new TimeSpanParser().TryParseAsync(timeSpanString, ct);
return !parseResult.IsDefined(out var @in)
? TimeSpan.Zero
: @in;
}
/// <summary> /// <summary>
/// A slash command that deletes a reminder using its list position. /// A slash command that deletes a reminder using its list position.
/// </summary> /// </summary>

View file

@ -1,13 +1,11 @@
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Remora.Commands.Parsers; using Octobot.Parsers;
using Remora.Results; using Remora.Results;
namespace Octobot.Data.Options; namespace Octobot.Data.Options;
public sealed class TimeSpanOption : Option<TimeSpan> public sealed class TimeSpanOption : Option<TimeSpan>
{ {
private static readonly TimeSpanParser Parser = new();
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { } public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
public override TimeSpan Get(JsonNode settings) public override TimeSpan Get(JsonNode settings)
@ -29,6 +27,6 @@ public sealed class TimeSpanOption : Option<TimeSpan>
private static Result<TimeSpan> ParseTimeSpan(string from) private static Result<TimeSpan> ParseTimeSpan(string from)
{ {
return Parser.TryParseAsync(from).AsTask().GetAwaiter().GetResult(); return TimeSpanParser.TryParse(from);
} }
} }

View file

@ -1037,11 +1037,11 @@ namespace Octobot {
} }
} }
internal static string ReminderInvalidTimeSpan internal static string InvalidTimeSpan
{ {
get get
{ {
return ResourceManager.GetString("ReminderInvalidTimeSpan", resourceCulture); return ResourceManager.GetString("InvalidTimeSpan", resourceCulture);
} }
} }
} }

View file

@ -0,0 +1,24 @@
using JetBrains.Annotations;
using Remora.Commands.Parsers;
using Remora.Results;
namespace Octobot.Parsers;
/// <summary>
/// Parses <see cref="TimeSpan"/>.
/// </summary>
[PublicAPI]
public class TimeSpanParser : AbstractTypeParser<TimeSpan>
{
public static Result<TimeSpan> TryParse(string timeSpanString, CancellationToken ct = default)
{
if (timeSpanString.StartsWith('-'))
{
return TimeSpan.Zero;
}
var parser = new Remora.Commands.Parsers.TimeSpanParser();
var parseResult = parser.TryParseAsync(timeSpanString, ct).AsTask().GetAwaiter().GetResult();
return !parseResult.IsDefined(out var @in) ? TimeSpan.Zero : @in;
}
}