Skip to content

Commit

Permalink
railgun: purge: Make it possible to delete all messages after specifi…
Browse files Browse the repository at this point in the history
…c message
  • Loading branch information
argraur committed May 6, 2020
1 parent deafdd6 commit 380219f
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/main/java/me/argraur/railgun/commands/admin/Purge.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package me.argraur.railgun.commands.admin;

import me.argraur.railgun.level.Level;

import java.util.List;

import me.argraur.railgun.interfaces.Command;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Message;

public class Purge implements Command {
private final String command = "purge";
private final String usage = command + " <n>";
private final String description = "Removes n last messages.";
private final int level = Level.MESSAGE;

@Override
public String getCommand() {
Expand All @@ -41,11 +45,26 @@ public String getDescription() {
return description;
}

/**
* Returns command's access level
*
* @return level
*/
@Override
public int getLevel() {
return level;
}

@Override
public void call(Message message) {
if (message.getMember().hasPermission(Permission.MESSAGE_MANAGE)) {
try {
int amount = Integer.parseInt(message.getContentRaw().split(" ")[1]);
message.getChannel().purgeMessages(message.getChannel().getHistory().retrievePast(amount + 1).complete());
} catch (NumberFormatException e) {
final String id = message.getContentRaw().split(" ")[1].split("/")[6];
List<Message> history = message.getChannel().getHistoryAfter(id, 100).complete().getRetrievedHistory();
message.getChannel().deleteMessageById(id).queue();
message.getChannel().purgeMessages(history);
}
}
}

0 comments on commit 380219f

Please # to comment.