Skip to content

Commit

Permalink
Add dry run option to /clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed May 26, 2024
1 parent 4ed4d92 commit 3df1a4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 12 additions & 2 deletions Commands/InteractionCommands/ClearInteractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public async Task ClearSlashCommand(InteractionContext ctx,
[Option("humans_only", "Optionally filter the deletion to only humans.")] bool humansOnly = false,
[Option("attachments_only", "Optionally filter the deletion to only messages with attachments.")] bool attachmentsOnly = false,
[Option("stickers_only", "Optionally filter the deletion to only messages with stickers.")] bool stickersOnly = false,
[Option("links_only", "Optionally filter the deletion to only messages containing links.")] bool linksOnly = false
[Option("links_only", "Optionally filter the deletion to only messages containing links.")] bool linksOnly = false,
[Option("dry_run", "Don't actually delete the messages, just output what would be deleted.")] bool dryRun = false
)
{
await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral(true));
await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral(!dryRun));

// If all args are unset
if (count == 0 && upTo == "" && user == default && ignoreMods == false && match == "" && botsOnly == false && humansOnly == false && attachmentsOnly == false && stickersOnly == false && linksOnly == false)
Expand Down Expand Up @@ -239,6 +240,15 @@ public async Task ClearSlashCommand(InteractionContext ctx,

// All filters checked. 'messages' is now our final list of messages to delete.

if (dryRun)
{
var msg = await LogChannelHelper.CreateDumpMessageAsync($"{Program.cfgjson.Emoji.Information} **{messagesToClear.Count}** messages would have been deleted, but are instead logged below.",
messagesToClear,
ctx.Channel);
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(msg.Content).AddFiles(msg.Files).AddEmbeds(msg.Embeds).AsEphemeral(false));
return;
}

// Warn the mod if we're going to be deleting 50 or more messages.
if (messagesToClear.Count >= 50)
{
Expand Down
9 changes: 7 additions & 2 deletions Helpers/LogChannelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static async Task<DiscordMessage> LogMessageAsync(string key, DiscordMess
}
}

public static async Task<DiscordMessage> LogDeletedMessagesAsync(string key, string content, List<DiscordMessage> messages, DiscordChannel channel)
public static async Task<DiscordMessageBuilder> CreateDumpMessageAsync(string content, List<DiscordMessage> messages, DiscordChannel channel)
{
string messageLog = await DiscordHelpers.CompileMessagesAsync(messages.AsEnumerable().Reverse().ToList(), channel);

Expand All @@ -144,8 +144,13 @@ public static async Task<DiscordMessage> LogDeletedMessagesAsync(string key, str
{
msg.AddEmbed(new DiscordEmbedBuilder().WithDescription($"[`📄 View online`]({Program.cfgjson.HastebinEndpoint}/raw/{hasteResult.Key})"));
}
return msg;
}

public static async Task<DiscordMessage> LogDeletedMessagesAsync(string key, string content, List<DiscordMessage> messages, DiscordChannel channel)
{

return await LogMessageAsync(key, msg);
return await LogMessageAsync(key, await CreateDumpMessageAsync(content, messages, channel));
}

}
Expand Down

0 comments on commit 3df1a4a

Please sign in to comment.