Skip to content

Commit

Permalink
Use function to generate progress message
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan19 committed Dec 1, 2019
1 parent 7ebb649 commit d364a16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'java'
apply plugin: 'maven'

group = 'main'
version = '5.3.8'
version = '5.3.9'

description = "A bot for the Facets tabletop RPG"

Expand Down
23 changes: 19 additions & 4 deletions src/main/java/commands/EmojiPurgeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,33 @@ private CompletableFuture<Pair<Integer, Integer>> removeAllPlotPointEmojisFromCh
*/
private CompletableFuture<Pair<Integer, Integer>> clearReactionsFromChannel(ArrayList<Message> enhancementEmojiMessageList, int messagesToClear, int emojis, Message progressMessage) {
AtomicInteger current = new AtomicInteger();
DecimalFormat df = new DecimalFormat("0.##");
ArrayList<CompletableFuture<Void>> completedRemovalFutures = new ArrayList<>();
enhancementEmojiMessageList.forEach(message -> {
CompletableFuture<Void> voidCompletableFuture = PlotPointEnhancementHelper.removeEnhancementEmojis(message);
completedRemovalFutures.add(voidCompletableFuture.thenAccept(aVoid -> {
current.getAndIncrement();
completedRemovalFutures.add(progressMessage.edit("Removing " + emojis + " emojis from " + messagesToClear + " messages! " + current + "/" + messagesToClear + " (" + df.format((double) current.get() / messagesToClear * 100) + "%)"));
completedRemovalFutures.add(progressMessage.edit(generateProgressMessage(messagesToClear, emojis, current.get())));
}));
});
/*When all reacts are removed, add a :X: react to allow it to be deleted and then return a Pair as a
CompletedFuture*/
return CompletableFuture.allOf(completedRemovalFutures.toArray(new CompletableFuture[0])).thenCompose(aVoid ->
//Hack to get display to work correctly
progressMessage.edit("Removing " + emojis + " emojis from " + messagesToClear + " messages! " + messagesToClear + "/" + messagesToClear + " (" + df.format((double) messagesToClear / messagesToClear * 100) + "%)" + "\nDone!").thenCompose(aVoid1 -> StatisticsCommand.addCancelReactToMessage(progressMessage).thenApply(aVoid2 -> new Pair<>(messagesToClear, emojis))));
progressMessage
.edit(generateProgressMessage(messagesToClear, emojis, current.get()))
.thenCompose(aVoid1 -> StatisticsCommand.addCancelReactToMessage(progressMessage).thenApply(aVoid2 -> new Pair<>(messagesToClear, emojis))));
}

/**
* Helper function to generate the string for the reaction removal progress message
*
* @param messagesToClear The number of messages that have reactions that still need to be clear
* @param emojis The numebr of reactions that will be cleared
* @param current The number of messages whose plot point enhancement reactions have been cleared
* @return A string for the progress message
*/
private String generateProgressMessage(int messagesToClear, int emojis, int current) {
DecimalFormat df = new DecimalFormat("0.##");
return "Removing " + emojis + " emojis from " + messagesToClear + " messages! " + current + "/" + messagesToClear + " (" + df.format((double) messagesToClear / messagesToClear * 100) + "%)" + (current == messagesToClear ? "\nDone!" : "");
}

/**
Expand Down

0 comments on commit d364a16

Please sign in to comment.