1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Use custom TimeSpanParser (#223)

Closes #154

---------

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2023-12-31 15:27:00 +03:00 committed by GitHub
parent 894e819865
commit e01fde83c6
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 183 additions and 16 deletions

View file

@ -585,6 +585,9 @@
<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="InvalidTimeSpan" xml:space="preserve">
<value>Time specified incorrectly!</value>
</data>
<data name="UserInfoKicked" xml:space="preserve"> <data name="UserInfoKicked" xml:space="preserve">
<value>Kicked</value> <value>Kicked</value>
</data> </data>

View file

@ -585,6 +585,9 @@
<data name="ButtonReportIssue" xml:space="preserve"> <data name="ButtonReportIssue" xml:space="preserve">
<value>Сообщить о проблеме</value> <value>Сообщить о проблеме</value>
</data> </data>
<data name="InvalidTimeSpan" xml:space="preserve">
<value>Неправильно указано время!</value>
</data>
<data name="UserInfoKicked" xml:space="preserve"> <data name="UserInfoKicked" xml:space="preserve">
<value>Выгнан</value> <value>Выгнан</value>
</data> </data>

View file

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

View file

@ -4,6 +4,7 @@ using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Octobot.Data; using Octobot.Data;
using Octobot.Extensions; using Octobot.Extensions;
using Octobot.Parsers;
using Octobot.Services; using Octobot.Services;
using Octobot.Services.Update; using Octobot.Services.Update;
using Remora.Commands.Attributes; using Remora.Commands.Attributes;
@ -53,7 +54,7 @@ public class BanCommandGroup : CommandGroup
/// A slash command that bans a Discord user with the specified reason. /// A slash command that bans a Discord user with the specified reason.
/// </summary> /// </summary>
/// <param name="target">The user to ban.</param> /// <param name="target">The user to ban.</param>
/// <param name="duration">The duration for this ban. The user will be automatically unbanned after this duration.</param> /// <param name="stringDuration">The duration for this ban. The user will be automatically unbanned after this duration.</param>
/// <param name="reason"> /// <param name="reason">
/// The reason for this ban. Must be encoded with <see cref="StringExtensions.EncodeHeader" /> when passed to /// The reason for this ban. Must be encoded with <see cref="StringExtensions.EncodeHeader" /> when passed to
/// <see cref="IDiscordRestGuildAPI.CreateGuildBanAsync" />. /// <see cref="IDiscordRestGuildAPI.CreateGuildBanAsync" />.
@ -75,7 +76,8 @@ public class BanCommandGroup : CommandGroup
[Description("User to ban")] IUser target, [Description("User to ban")] IUser target,
[Description("Ban reason")] [MaxLength(256)] [Description("Ban reason")] [MaxLength(256)]
string reason, string reason,
[Description("Ban duration")] TimeSpan? duration = null) [Description("Ban duration")] [Option("duration")]
string? stringDuration = null)
{ {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId)) if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId))
{ {
@ -104,6 +106,23 @@ public class BanCommandGroup : CommandGroup
var data = await _guildData.GetData(guild.ID, CancellationToken); var data = await _guildData.GetData(guild.ID, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings); Messages.Culture = GuildSettings.Language.Get(data.Settings);
if (stringDuration is null)
{
return await BanUserAsync(executor, target, reason, null, guild, data, channelId, bot,
CancellationToken);
}
var parseResult = TimeSpanParser.TryParse(stringDuration);
if (!parseResult.IsDefined(out var duration))
{
var failedEmbed = new EmbedBuilder()
.WithSmallTitle(Messages.InvalidTimeSpan, bot)
.WithColour(ColorsList.Red)
.Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
}
return await BanUserAsync(executor, target, reason, duration, guild, data, channelId, bot, CancellationToken); return await BanUserAsync(executor, target, reason, duration, guild, data, channelId, bot, CancellationToken);
} }

View file

@ -4,6 +4,7 @@ using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Octobot.Data; using Octobot.Data;
using Octobot.Extensions; using Octobot.Extensions;
using Octobot.Parsers;
using Octobot.Services; using Octobot.Services;
using Octobot.Services.Update; using Octobot.Services.Update;
using Remora.Commands.Attributes; using Remora.Commands.Attributes;
@ -50,7 +51,7 @@ public class MuteCommandGroup : CommandGroup
/// A slash command that mutes a Discord member with the specified reason. /// A slash command that mutes a Discord member with the specified reason.
/// </summary> /// </summary>
/// <param name="target">The member to mute.</param> /// <param name="target">The member to mute.</param>
/// <param name="duration">The duration for this mute. The member will be automatically unmuted after this duration.</param> /// <param name="stringDuration">The duration for this mute. The member will be automatically unmuted after this duration.</param>
/// <param name="reason"> /// <param name="reason">
/// The reason for this mute. Must be encoded with <see cref="StringExtensions.EncodeHeader" /> when passed to /// The reason for this mute. Must be encoded with <see cref="StringExtensions.EncodeHeader" /> when passed to
/// <see cref="IDiscordRestGuildAPI.ModifyGuildMemberAsync" />. /// <see cref="IDiscordRestGuildAPI.ModifyGuildMemberAsync" />.
@ -72,7 +73,8 @@ public class MuteCommandGroup : CommandGroup
[Description("Member to mute")] IUser target, [Description("Member to mute")] IUser target,
[Description("Mute reason")] [MaxLength(256)] [Description("Mute reason")] [MaxLength(256)]
string reason, string reason,
[Description("Mute duration")] TimeSpan duration) [Description("Mute duration")] [Option("duration")]
string stringDuration)
{ {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId)) if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId))
{ {
@ -104,6 +106,17 @@ public class MuteCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, ct: CancellationToken); return await _feedback.SendContextualEmbedResultAsync(embed, ct: CancellationToken);
} }
var parseResult = TimeSpanParser.TryParse(stringDuration);
if (!parseResult.IsDefined(out var duration))
{
var failedEmbed = new EmbedBuilder()
.WithSmallTitle(Messages.InvalidTimeSpan, bot)
.WithColour(ColorsList.Red)
.Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
}
return await MuteUserAsync(executor, target, reason, duration, guildId, data, channelId, bot, CancellationToken); return await MuteUserAsync(executor, target, reason, duration, guildId, data, channelId, bot, CancellationToken);
} }

View file

@ -17,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 Octobot.Parsers;
namespace Octobot.Commands; namespace Octobot.Commands;
@ -110,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="in">The period of time which must pass before the reminder will be sent.</param> /// <param name="timeSpanString">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")]
@ -120,7 +121,8 @@ public class RemindCommandGroup : CommandGroup
[UsedImplicitly] [UsedImplicitly]
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")]
TimeSpan @in, [Option("in")]
string timeSpanString,
[Description("Reminder text")] [MaxLength(512)] [Description("Reminder text")] [MaxLength(512)]
string text) string text)
{ {
@ -129,6 +131,12 @@ public class RemindCommandGroup : CommandGroup
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
} }
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
if (!botResult.IsDefined(out var bot))
{
return Result.FromError(botResult);
}
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken); var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
if (!executorResult.IsDefined(out var executor)) if (!executorResult.IsDefined(out var executor))
{ {
@ -138,14 +146,25 @@ 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(@in, text, data, channelId, executor, CancellationToken); var parseResult = TimeSpanParser.TryParse(timeSpanString);
if (!parseResult.IsDefined(out var timeSpan))
{
var failedEmbed = new EmbedBuilder()
.WithSmallTitle(Messages.InvalidTimeSpan, bot)
.WithColour(ColorsList.Red)
.Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
} }
private async Task<Result> AddReminderAsync(TimeSpan @in, string text, GuildData data, 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) Snowflake channelId, IUser executor, CancellationToken ct = default)
{ {
var memberData = data.GetOrCreateMemberData(executor.ID); var memberData = data.GetOrCreateMemberData(executor.ID);
var remindAt = DateTimeOffset.UtcNow.Add(@in); var remindAt = DateTimeOffset.UtcNow.Add(timeSpan);
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))
{ {

View file

@ -4,6 +4,7 @@ using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Octobot.Data; using Octobot.Data;
using Octobot.Extensions; using Octobot.Extensions;
using Octobot.Parsers;
using Octobot.Services; using Octobot.Services;
using Remora.Commands.Attributes; using Remora.Commands.Attributes;
using Remora.Commands.Groups; using Remora.Commands.Groups;
@ -418,7 +419,7 @@ public class ToolsCommandGroup : CommandGroup
/// <summary> /// <summary>
/// A slash command that shows the current timestamp with an optional offset in all styles supported by Discord. /// A slash command that shows the current timestamp with an optional offset in all styles supported by Discord.
/// </summary> /// </summary>
/// <param name="offset">The offset for the current timestamp.</param> /// <param name="stringOffset">The offset for the current timestamp.</param>
/// <returns> /// <returns>
/// A feedback sending result which may or may not have succeeded. /// A feedback sending result which may or may not have succeeded.
/// </returns> /// </returns>
@ -427,14 +428,20 @@ public class ToolsCommandGroup : CommandGroup
[Description("Shows a timestamp in all styles")] [Description("Shows a timestamp in all styles")]
[UsedImplicitly] [UsedImplicitly]
public async Task<Result> ExecuteTimestampAsync( public async Task<Result> ExecuteTimestampAsync(
[Description("Offset from current time")] [Description("Offset from current time")] [Option("offset")]
TimeSpan? offset = null) string? stringOffset = null)
{ {
if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId)) if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId))
{ {
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
} }
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
if (!botResult.IsDefined(out var bot))
{
return Result.FromError(botResult);
}
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken); var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
if (!executorResult.IsDefined(out var executor)) if (!executorResult.IsDefined(out var executor))
{ {
@ -444,6 +451,22 @@ public class ToolsCommandGroup : 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);
if (stringOffset is null)
{
return await SendTimestampAsync(null, executor, CancellationToken);
}
var parseResult = TimeSpanParser.TryParse(stringOffset);
if (!parseResult.IsDefined(out var offset))
{
var failedEmbed = new EmbedBuilder()
.WithSmallTitle(Messages.InvalidTimeSpan, bot)
.WithColour(ColorsList.Red)
.Build();
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
}
return await SendTimestampAsync(offset, executor, CancellationToken); return await SendTimestampAsync(offset, executor, CancellationToken);
} }

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,6 +1037,14 @@ namespace Octobot {
} }
} }
internal static string InvalidTimeSpan
{
get
{
return ResourceManager.GetString("InvalidTimeSpan", resourceCulture);
}
}
internal static string UserInfoKicked internal static string UserInfoKicked
{ {
get get

View file

@ -0,0 +1,78 @@
using System.Globalization;
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 a <see cref="TimeSpan"/> from the <paramref name="timeSpanString"/>.
/// </summary>
/// <returns>
/// The parsed <see cref="TimeSpan"/>, or <see cref="ArgumentInvalidError"/> if parsing failed.
/// </returns>
public static Result<TimeSpan> TryParse(string timeSpanString)
{
if (timeSpanString.StartsWith('-'))
{
return new ArgumentInvalidError(nameof(timeSpanString), "TimeSpans cannot be negative.");
}
if (TimeSpan.TryParse(timeSpanString, DateTimeFormatInfo.InvariantInfo, out var parsedTimeSpan))
{
return parsedTimeSpan;
}
var matches = ParseRegex().Matches(timeSpanString);
return matches.Count > 0
? ParseFromRegex(matches)
: new ArgumentInvalidError(nameof(timeSpanString), "The regex did not produce any matches.");
}
private static Result<TimeSpan> ParseFromRegex(MatchCollection matches)
{
var timeSpan = TimeSpan.Zero;
foreach (var groups in matches.Select(match => match.Groups
.Cast<Group>()
.Where(g => g.Success)
.Skip(1)
.Select(g => (g.Name, g.Value))))
{
foreach ((var key, var groupValue) in groups)
{
if (!int.TryParse(groupValue, out var parsedIntegerValue))
{
return new ArgumentInvalidError(nameof(groupValue), "The input value was not an integer.");
}
var now = DateTimeOffset.UtcNow;
timeSpan += key switch
{
"Years" => now.AddYears(parsedIntegerValue) - now,
"Months" => now.AddMonths(parsedIntegerValue) - now,
"Weeks" => TimeSpan.FromDays(parsedIntegerValue * 7),
"Days" => TimeSpan.FromDays(parsedIntegerValue),
"Hours" => TimeSpan.FromHours(parsedIntegerValue),
"Minutes" => TimeSpan.FromMinutes(parsedIntegerValue),
"Seconds" => TimeSpan.FromSeconds(parsedIntegerValue),
_ => throw new ArgumentOutOfRangeException(key)
};
}
}
return timeSpan;
}
[GeneratedRegex("(?<Years>\\d+(?=y|л|г))|(?<Months>\\d+(?=mo|мес))|(?<Weeks>\\d+(?=w|н|нед))|(?<Days>\\d+(?=d|д|дн))|(?<Hours>\\d+(?=h|ч))|(?<Minutes>\\d+(?=m|min|мин|м))|(?<Seconds>\\d+(?=s|sec|с|сек))")]
private static partial Regex ParseRegex();
}