Skip to content

Instantly share code, notes, and snippets.

@foxbot
Created October 18, 2016 20:09
Show Gist options
  • Save foxbot/f2bde8ad5a03e5abe2686d45511f77b7 to your computer and use it in GitHub Desktop.
Save foxbot/f2bde8ad5a03e5abe2686d45511f77b7 to your computer and use it in GitHub Desktop.
your purge command is really bad quick hold this
public class UtilityModule : ModuleBase
{
[Command("purge")]
[Alias("clean", "cleanup", "prune")]
[Summary("Cleans the bot's messages")]
[RequirePermission(ChannelPermission.ManageMessages)]
public async Task Clean(
[Summary("The optional number of messages to delete; defaults to 10")] int count = 10,
[Summary("The type of messages to delete - Self, Bot, or All")] DeleteType deleteType = DeleteType.Self,
[Summary("The strategy to delete messages - BulkDelete or Manual")] DeleteStrategy deleteStrategy = DeleteStrategy.BulkDelete)
{
int index = 0;
var deleteMessages = new List<IMessage>(count);
var messages = Context.Channel.GetMessagesAsync();
await messages.ForEachAsync(async m =>
{
IEnumerable<IMessage> delete = null;
if (deleteType == DeleteType.Self)
delete = m.Where(msg => msg.Author.Id == Context.Client.CurrentUser.Id);
else if (deleteType == DeleteType.Bot)
delete = m.Where(msg => msg.Author.IsBot);
else if (deleteType == DeleteType.All)
delete = m;
foreach (var msg in delete.OrderByDescending(msg => msg.Timestamp))
{
if (index >= count) { await EndClean(deleteMessages, deleteStrategy); return; }
deleteMessages.Add(msg);
index++;
}
});
}
internal async Task EndClean(IEnumerable<IMessage> messages, DeleteStrategy strategy)
{
if (strategy == DeleteStrategy.BulkDelete)
await Context.Channel.DeleteMessagesAsync(messages);
else if (strategy == DeleteStrategy.Manual)
{
foreach (var msg in messages.Cast<IUserMessage>())
{
await msg.DeleteAsync();
}
}
}
}
public enum DeleteType
{
Self = 0,
Bot = 1,
All = 2
}
public enum DeleteStrategy
{
BulkDelete = 0,
Manual = 1,
}
@nnoc
Copy link

nnoc commented Jan 9, 2019

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