Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitsmt211 committed Nov 7, 2023
1 parent 672d319 commit c3f3532
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public PurgeHistoryCommand(Database database) {

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
event.deferReply().queue();
event.deferReply(true).queue();

OptionMapping targetOption =
Objects.requireNonNull(event.getOption(USER_OPTION), "target is null");
Expand All @@ -77,9 +77,7 @@ private void handleHistory(SlashCommandInteractionEvent event, Member author, St
String sourceChannelId = event.getChannel().getId();

if (!validateHierarchy(author, targetOption)) {
hook.sendMessage("Cannot purge history of user with a higher role than you")
.setEphemeral(true)
.queue();
hook.sendMessage("Cannot purge history of user with a higher role than you").queue();
return;
}

Expand All @@ -103,19 +101,18 @@ private void handleHistory(SlashCommandInteractionEvent event, Member author, St
}

handleDelete(messageIdsForDeletion, event.getJDA(), event.getChannel(), event.getHook(),
targetUser, reason);
targetUser, reason, author);
}

private void handleDelete(List<String> messageIdsForDeletion, JDA jda,
MessageChannelUnion channel, InteractionHook hook, User targetUser, String reason) {
MessageChannelUnion channel, InteractionHook hook, User targetUser, String reason,
Member author) {

if (messageIdsForDeletion.isEmpty()) {
handleEmptyMessageHistory(hook, targetUser);
return;
}

registerPurgeAction(reason);

if (hasSingleElement(messageIdsForDeletion)) {
String messageId = messageIdsForDeletion.get(0);

Expand All @@ -124,12 +121,12 @@ private void handleDelete(List<String> messageIdsForDeletion, JDA jda,
.queue();
}

int noOfMessagesToPurge = messageIdsForDeletion.size();
channel.purgeMessagesById(messageIdsForDeletion);

String messageForMod = "%s messages purged from user %s".formatted(noOfMessagesToPurge,
targetUser.getName());
hook.sendMessage(messageForMod).setEphemeral(true).queue();
String messageForMod = "messages purged from user %s".formatted(targetUser.getName());
hook.sendMessage(messageForMod)
.queue(onSuccess -> logger.info("{} purged messages from {} because: {}",
author.getUser(), targetUser, reason));
}

private boolean hasSingleElement(List<String> messageIdsForDeletion) {
Expand All @@ -140,7 +137,7 @@ private void handleEmptyMessageHistory(InteractionHook hook, User targetUser) {
String messageForMod = "%s has no message history in this channel within last %s hr"
.formatted(targetUser, PURGE_MESSAGES_AFTER_LIMIT_HOURS);

hook.sendMessage(messageForMod).setEphemeral(true).queue();
hook.sendMessage(messageForMod).queue();
}

private boolean validateHierarchy(Member author, OptionMapping target) {
Expand All @@ -153,8 +150,4 @@ private boolean validateHierarchy(Member author, OptionMapping target) {

return targetUserRole.getPosition() >= authorRole.getPosition();
}

private void registerPurgeAction(String reason) {
// TODO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,4 @@ private void updateHistory(MessageReceivedEvent event) {
private boolean shouldIgnoreMessages(MessageReceivedEvent event) {
return event.isWebhookMessage() || event.getAuthor().isBot();
}

private void limitHistoryRecords() {
// TODO
}
}

0 comments on commit c3f3532

Please sign in to comment.