Reduce method complexity of /clear (#56)

Depends on #54

This PR reduces the method complexity of
`ClearCommandGroup#ClearMessagesAsync` by doing the following:
- Splitting the command method into 2 parts: ExecuteClear and
ClearMessagesAsync. The former will check all the needed results and
will pass the outputs into the latter;
- Using the recently extracted method `UtilityService#LogActionAsync` to
log deletion of messages.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-07-20 12:44:07 +05:00 committed by GitHub
parent daa1dd4184
commit 4624b59a8a
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,15 +28,17 @@ public class ClearCommandGroup : CommandGroup {
private readonly GuildDataService _dataService; private readonly GuildDataService _dataService;
private readonly FeedbackService _feedbackService; private readonly FeedbackService _feedbackService;
private readonly IDiscordRestUserAPI _userApi; private readonly IDiscordRestUserAPI _userApi;
private readonly UtilityService _utility;
public ClearCommandGroup( public ClearCommandGroup(
IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService dataService, IDiscordRestChannelAPI channelApi, ICommandContext context, GuildDataService dataService,
FeedbackService feedbackService, IDiscordRestUserAPI userApi) { FeedbackService feedbackService, IDiscordRestUserAPI userApi, UtilityService utility) {
_channelApi = channelApi; _channelApi = channelApi;
_context = context; _context = context;
_dataService = dataService; _dataService = dataService;
_feedbackService = feedbackService; _feedbackService = feedbackService;
_userApi = userApi; _userApi = userApi;
_utility = utility;
} }
/// <summary> /// <summary>
@ -55,7 +57,7 @@ public class ClearCommandGroup : CommandGroup {
[RequireBotDiscordPermissions(DiscordPermission.ManageMessages)] [RequireBotDiscordPermissions(DiscordPermission.ManageMessages)]
[Description("Remove multiple messages")] [Description("Remove multiple messages")]
[UsedImplicitly] [UsedImplicitly]
public async Task<Result> ClearMessagesAsync( public async Task<Result> ExecuteClear(
[Description("Number of messages to remove (2-100)")] [MinValue(2)] [MaxValue(100)] [Description("Number of messages to remove (2-100)")] [MinValue(2)] [MaxValue(100)]
int amount) { int amount) {
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId)) if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
@ -66,12 +68,25 @@ public class ClearCommandGroup : CommandGroup {
channelId.Value, limit: amount + 1, ct: CancellationToken); channelId.Value, limit: amount + 1, ct: CancellationToken);
if (!messagesResult.IsDefined(out var messages)) if (!messagesResult.IsDefined(out var messages))
return Result.FromError(messagesResult); return Result.FromError(messagesResult);
var userResult = await _userApi.GetUserAsync(userId.Value, CancellationToken);
if (!userResult.IsDefined(out var user))
return Result.FromError(userResult);
// The current user's avatar is used when sending messages
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
if (!currentUserResult.IsDefined(out var currentUser))
return Result.FromError(currentUserResult);
var cfg = await _dataService.GetSettings(guildId.Value, CancellationToken); return await ClearMessagesAsync(amount, guildId.Value, channelId.Value, messages, user, currentUser);
}
private async Task<Result> ClearMessagesAsync(
int amount, Snowflake guildId, Snowflake channelId, IReadOnlyList<IMessage> messages,
IUser user, IUser currentUser) {
var cfg = await _dataService.GetSettings(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(cfg); Messages.Culture = GuildSettings.Language.Get(cfg);
var idList = new List<Snowflake>(messages.Count); var idList = new List<Snowflake>(messages.Count);
var builder = new StringBuilder().AppendLine(Mention.Channel(channelId.Value)).AppendLine(); var builder = new StringBuilder().AppendLine(Mention.Channel(channelId)).AppendLine();
for (var i = messages.Count - 1; i >= 1; i--) { // '>= 1' to skip last message ('Boyfriend is thinking...') for (var i = messages.Count - 1; i >= 1; i--) { // '>= 1' to skip last message ('Boyfriend is thinking...')
var message = messages[i]; var message = messages[i];
idList.Add(message.ID); idList.Add(message.ID);
@ -79,41 +94,18 @@ public class ClearCommandGroup : CommandGroup {
builder.Append(message.Content.InBlockCode()); builder.Append(message.Content.InBlockCode());
} }
var title = string.Format(Messages.MessagesCleared, amount.ToString());
var description = builder.ToString(); var description = builder.ToString();
var userResult = await _userApi.GetUserAsync(userId.Value, CancellationToken);
if (!userResult.IsDefined(out var user))
return Result.FromError(userResult);
var deleteResult = await _channelApi.BulkDeleteMessagesAsync( var deleteResult = await _channelApi.BulkDeleteMessagesAsync(
channelId.Value, idList, user.GetTag().EncodeHeader(), CancellationToken); channelId, idList, user.GetTag().EncodeHeader(), CancellationToken);
if (!deleteResult.IsSuccess) if (!deleteResult.IsSuccess)
return Result.FromError(deleteResult.Error); return Result.FromError(deleteResult.Error);
// The current user's avatar is used when sending messages var logResult = _utility.LogActionAsync(
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken); cfg, channelId, user, title, description, currentUser, CancellationToken);
if (!currentUserResult.IsDefined(out var currentUser)) if (!logResult.IsSuccess)
return Result.FromError(currentUserResult); return Result.FromError(logResult.Error);
var title = string.Format(Messages.MessagesCleared, amount.ToString());
if (!GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty()
&& GuildSettings.PrivateFeedbackChannel.Get(cfg) != channelId.Value) {
var logEmbed = new EmbedBuilder().WithSmallTitle(title, currentUser)
.WithDescription(description)
.WithActionFooter(user)
.WithCurrentTimestamp()
.WithColour(ColorsList.Red)
.Build();
if (!logEmbed.IsDefined(out var logBuilt))
return Result.FromError(logEmbed);
// Not awaiting to reduce response time
if (GuildSettings.PrivateFeedbackChannel.Get(cfg) != channelId.Value)
_ = _channelApi.CreateMessageAsync(
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { logBuilt },
ct: CancellationToken);
}
var embed = new EmbedBuilder().WithSmallTitle(title, currentUser) var embed = new EmbedBuilder().WithSmallTitle(title, currentUser)
.WithColour(ColorsList.Green).Build(); .WithColour(ColorsList.Green).Build();