Skip to content

Commit

Permalink
port to jda5
Browse files Browse the repository at this point in the history
  • Loading branch information
granny committed Sep 13, 2024
1 parent 8df25dc commit 1ce67cb
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions jda5/src/main/java/me/scarsz/jdaappender/ChannelLoggingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,27 @@ private Message updateMessage() throws IllegalStateException {
while (full.contains("\n\n")) full = full.replace("\n\n", "\n");

try {
return sendOrEditMessage(full, channel);
} catch (ErrorResponseException errorResponseException) {
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
return sendOrEditMessage(full, channel);
} else if (errorResponseException.getErrorResponse() == ErrorResponse.MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER) {
// Make at most two attempts to process message.
// If the message is missing on the first attempt, try again.
// If the message runs into anything else, throw to higher catch
for (int i = 0; i < 2; i++) {
try {
return sendOrEditMessage(full, channel);
} catch (ErrorResponseException ex) {
if (i == 0 && ex.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
continue;
}
throw ex;
}
}
throw new RuntimeException("Unexpected error: Failed to update message for unknown reason.");
} catch (ErrorResponseException ex) {
if (ex.getErrorResponse() == ErrorResponse.MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER) {
full = URL_PATTERN.matcher(full).replaceAll("$1");
return sendOrEditMessage(full, channel);
} else {
throw new RuntimeException(errorResponseException);
}
throw ex;
}
}

Expand All @@ -261,8 +271,9 @@ private Message sendOrEditMessage(String full, MessageChannel channel) throws Er
} catch (ExecutionException | InterruptedException e) {
if (this.isInterruptedException(e)) return currentMessage;

if (e.getCause() instanceof ErrorResponseException) {
throw (ErrorResponseException) e.getCause();
Throwable cause = e.getCause();
if (cause instanceof ErrorResponseException) {
throw (ErrorResponseException) cause;
}

throw new RuntimeException(e);
Expand Down

0 comments on commit 1ce67cb

Please sign in to comment.