Created
June 2, 2018 16:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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")}."); | |
} | |
} |
Thanks soooo much
omg thank you so much
Thanks mate.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, helped me alote!!