mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-30 11:09:54 +03:00
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
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue