Skip to content

Instantly share code, notes, and snippets.

@Quahu
Created June 2, 2018 16:52
Show Gist options
  • Save Quahu/7ee7550cd54ef8329af4a733f9052fe4 to your computer and use it in GitHub Desktop.
Save Quahu/7ee7550cd54ef8329af4a733f9052fe4 to your computer and use it in GitHub Desktop.
An example purge command that removes messages from the channel it's used in.
[Command("purge")]
[Alias("clean")]
[Summary("Downloads and removes X messages from the current channel.")]
[RequireUserPermission(ChannelPermission.ManageMessages)]
[RequireBotPermission(ChannelPermission.ManageMessages)]
public async Task PurgeAsync(int amount)
{
// Check if the amount provided by the user is positive.
if (amount <= 0)
{
await ReplyAsync("The amount of messages to remove must be positive.");
return;
}
// Download X messages starting from Context.Message, which means
// that it won't delete the message used to invoke this command.
var messages = await Context.Channel.GetMessagesAsync(Context.Message, Direction.Before, amount).FlattenAsync();
// Note:
// FlattenAsync() might show up as a compiler error, because it's
// named differently on stable and nightly versions of Discord.Net.
// - Discord.Net 1.x: Flatten()
// - Discord.Net 2.x: FlattenAsync()
// Ensure that the messages aren't older than 14 days,
// because trying to bulk delete messages older than that
// will result in a bad request.
var filteredMessages = messages.Where(x => (DateTimeOffset.UtcNow - x.Timestamp).TotalDays <= 14);
// Get the total amount of messages.
var count = filteredMessages.Count();
// Check if there are any messages to delete.
if (count == 0)
await ReplyAsync("Nothing to delete.");
else
{
// The cast here isn't needed if you're using Discord.Net 1.x,
// but I'd recommend leaving it as it's what's required on 2.x, so
// if you decide to update you won't have to change this line.
await (Context.Channel as ITextChannel).DeleteMessagesAsync(filteredMessages);
await ReplyAsync($"Done. Removed {count} {(count > 1 ? "messages" : "message")}.");
}
}
@torchizm
Copy link

Thanks, helped me alote!!

@ZoiruS-YT
Copy link

Thanks soooo much

@ImJoosan
Copy link

ImJoosan commented Nov 6, 2021

omg thank you so much

@Kuch4rX
Copy link

Kuch4rX commented Nov 22, 2022

Thanks mate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment