forked from TeamInklings/Octobot
Use custom TimeSpanParser (#223)
Closes #154 --------- Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
894e819865
commit
e01fde83c6
10 changed files with 183 additions and 16 deletions
|
@ -585,6 +585,9 @@
|
|||
<data name="ButtonReportIssue" xml:space="preserve">
|
||||
<value>Report an issue</value>
|
||||
</data>
|
||||
<data name="InvalidTimeSpan" xml:space="preserve">
|
||||
<value>Time specified incorrectly!</value>
|
||||
</data>
|
||||
<data name="UserInfoKicked" xml:space="preserve">
|
||||
<value>Kicked</value>
|
||||
</data>
|
||||
|
|
|
@ -585,6 +585,9 @@
|
|||
<data name="ButtonReportIssue" xml:space="preserve">
|
||||
<value>Сообщить о проблеме</value>
|
||||
</data>
|
||||
<data name="InvalidTimeSpan" xml:space="preserve">
|
||||
<value>Неправильно указано время!</value>
|
||||
</data>
|
||||
<data name="UserInfoKicked" xml:space="preserve">
|
||||
<value>Выгнан</value>
|
||||
</data>
|
||||
|
|
|
@ -585,6 +585,9 @@
|
|||
<data name="ButtonReportIssue" xml:space="preserve">
|
||||
<value>зарепортить баг</value>
|
||||
</data>
|
||||
<data name="InvalidTimeSpan" xml:space="preserve">
|
||||
<value>ты там правильно напиши таймспан</value>
|
||||
</data>
|
||||
<data name="UserInfoKicked" xml:space="preserve">
|
||||
<value>кикнут</value>
|
||||
</data>
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using JetBrains.Annotations;
|
||||
using Octobot.Data;
|
||||
using Octobot.Extensions;
|
||||
using Octobot.Parsers;
|
||||
using Octobot.Services;
|
||||
using Octobot.Services.Update;
|
||||
using Remora.Commands.Attributes;
|
||||
|
@ -53,7 +54,7 @@ public class BanCommandGroup : CommandGroup
|
|||
/// A slash command that bans a Discord user with the specified reason.
|
||||
/// </summary>
|
||||
/// <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">
|
||||
/// The reason for this ban. Must be encoded with <see cref="StringExtensions.EncodeHeader" /> when passed to
|
||||
/// <see cref="IDiscordRestGuildAPI.CreateGuildBanAsync" />.
|
||||
|
@ -75,7 +76,8 @@ public class BanCommandGroup : CommandGroup
|
|||
[Description("User to ban")] IUser target,
|
||||
[Description("Ban reason")] [MaxLength(256)]
|
||||
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))
|
||||
{
|
||||
|
@ -104,6 +106,23 @@ public class BanCommandGroup : CommandGroup
|
|||
var data = await _guildData.GetData(guild.ID, CancellationToken);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using JetBrains.Annotations;
|
||||
using Octobot.Data;
|
||||
using Octobot.Extensions;
|
||||
using Octobot.Parsers;
|
||||
using Octobot.Services;
|
||||
using Octobot.Services.Update;
|
||||
using Remora.Commands.Attributes;
|
||||
|
@ -50,7 +51,7 @@ public class MuteCommandGroup : CommandGroup
|
|||
/// A slash command that mutes a Discord member with the specified reason.
|
||||
/// </summary>
|
||||
/// <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">
|
||||
/// The reason for this mute. Must be encoded with <see cref="StringExtensions.EncodeHeader" /> when passed to
|
||||
/// <see cref="IDiscordRestGuildAPI.ModifyGuildMemberAsync" />.
|
||||
|
@ -72,7 +73,8 @@ public class MuteCommandGroup : CommandGroup
|
|||
[Description("Member to mute")] IUser target,
|
||||
[Description("Mute reason")] [MaxLength(256)]
|
||||
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))
|
||||
{
|
||||
|
@ -104,6 +106,17 @@ public class MuteCommandGroup : CommandGroup
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ using Remora.Discord.Extensions.Embeds;
|
|||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
using Octobot.Parsers;
|
||||
|
||||
namespace Octobot.Commands;
|
||||
|
||||
|
@ -110,7 +111,7 @@ public class RemindCommandGroup : CommandGroup
|
|||
/// <summary>
|
||||
/// A slash command that schedules a reminder with the specified text.
|
||||
/// </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>
|
||||
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
|
||||
[Command("remind")]
|
||||
|
@ -120,7 +121,8 @@ public class RemindCommandGroup : CommandGroup
|
|||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteReminderAsync(
|
||||
[Description("After what period of time mention the reminder")]
|
||||
TimeSpan @in,
|
||||
[Option("in")]
|
||||
string timeSpanString,
|
||||
[Description("Reminder text")] [MaxLength(512)]
|
||||
string text)
|
||||
{
|
||||
|
@ -129,6 +131,12 @@ public class RemindCommandGroup : CommandGroup
|
|||
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);
|
||||
if (!executorResult.IsDefined(out var executor))
|
||||
{
|
||||
|
@ -138,14 +146,25 @@ public class RemindCommandGroup : CommandGroup
|
|||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
if (!responseResult.IsDefined(out var response))
|
||||
{
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using JetBrains.Annotations;
|
||||
using Octobot.Data;
|
||||
using Octobot.Extensions;
|
||||
using Octobot.Parsers;
|
||||
using Octobot.Services;
|
||||
using Remora.Commands.Attributes;
|
||||
using Remora.Commands.Groups;
|
||||
|
@ -418,7 +419,7 @@ public class ToolsCommandGroup : CommandGroup
|
|||
/// <summary>
|
||||
/// A slash command that shows the current timestamp with an optional offset in all styles supported by Discord.
|
||||
/// </summary>
|
||||
/// <param name="offset">The offset for the current timestamp.</param>
|
||||
/// <param name="stringOffset">The offset for the current timestamp.</param>
|
||||
/// <returns>
|
||||
/// A feedback sending result which may or may not have succeeded.
|
||||
/// </returns>
|
||||
|
@ -427,14 +428,20 @@ public class ToolsCommandGroup : CommandGroup
|
|||
[Description("Shows a timestamp in all styles")]
|
||||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteTimestampAsync(
|
||||
[Description("Offset from current time")]
|
||||
TimeSpan? offset = null)
|
||||
[Description("Offset from current time")] [Option("offset")]
|
||||
string? stringOffset = null)
|
||||
{
|
||||
if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId))
|
||||
{
|
||||
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);
|
||||
if (!executorResult.IsDefined(out var executor))
|
||||
{
|
||||
|
@ -444,6 +451,22 @@ public class ToolsCommandGroup : CommandGroup
|
|||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Commands.Parsers;
|
||||
using Octobot.Parsers;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Octobot.Data.Options;
|
||||
|
||||
public sealed class TimeSpanOption : Option<TimeSpan>
|
||||
{
|
||||
private static readonly TimeSpanParser Parser = new();
|
||||
|
||||
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
|
||||
|
||||
public override TimeSpan Get(JsonNode settings)
|
||||
|
@ -29,6 +27,6 @@ public sealed class TimeSpanOption : Option<TimeSpan>
|
|||
|
||||
private static Result<TimeSpan> ParseTimeSpan(string from)
|
||||
{
|
||||
return Parser.TryParseAsync(from).AsTask().GetAwaiter().GetResult();
|
||||
return TimeSpanParser.TryParse(from);
|
||||
}
|
||||
}
|
||||
|
|
8
src/Messages.Designer.cs
generated
8
src/Messages.Designer.cs
generated
|
@ -1037,6 +1037,14 @@ namespace Octobot {
|
|||
}
|
||||
}
|
||||
|
||||
internal static string InvalidTimeSpan
|
||||
{
|
||||
get
|
||||
{
|
||||
return ResourceManager.GetString("InvalidTimeSpan", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
internal static string UserInfoKicked
|
||||
{
|
||||
get
|
||||
|
|
78
src/Parsers/TimeSpanParser.cs
Normal file
78
src/Parsers/TimeSpanParser.cs
Normal 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();
|
||||
}
|
Reference in a new issue