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

do not forget about other commands

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2023-12-31 15:11:06 +03:00
parent fa28cd35bd
commit f68705e54c
Signed by: mctaylors
GPG key ID: 7181BEBE676903C1
4 changed files with 63 additions and 8 deletions

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

@ -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

@ -73,6 +73,6 @@ public partial class TimeSpanParser : AbstractTypeParser<TimeSpan>
return timeSpan; 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|с|сек))")] [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(); private static partial Regex ParseRegex();
} }