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 10, 2024
1 parent 658f128 commit a444d41
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions jda5/src/main/java/me/scarsz/jdaappender/ChannelLoggingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,26 +236,49 @@ private Message updateMessage() throws IllegalStateException {
// safeguard against empty lines
while (full.contains("\n\n")) full = full.replace("\n\n", "\n");

if (currentMessage != null) {
boolean retry = true;
int attempts = 0;
while (retry) {
retry = false;
try {
return currentMessage.editMessage(full).submit().get();
return sendOrEditMessage(full, channel);
} catch (Exception e) {
if (this.isInterruptedException(e)) return currentMessage;

Throwable cause = e.getCause();
if (cause instanceof ErrorResponseException
&& ((ErrorResponseException) cause).getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
} else {
throw new RuntimeException(cause);
if (cause instanceof ErrorResponseException) {
ErrorResponseException errorResponseException = (ErrorResponseException) cause;

if (attempts >= 2) {
throw new RuntimeException("Too many attempts were made", cause);
}

if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
retry = true;
attempts++;
continue;
} else if (errorResponseException.getErrorResponse() == ErrorResponse.MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER) {
full = URL_PATTERN.matcher(full).replaceAll("$1");
retry = true;
attempts++;
continue;
} else {
throw new RuntimeException(cause);
}
}

throw new RuntimeException(e);
}
}
throw new RuntimeException("This should never throw");
}

try {
private Message sendOrEditMessage(String full, MessageChannel channel) throws InterruptedException, ExecutionException {
if (currentMessage != null) {
return currentMessage.editMessage(full).submit().get();
} else {
return channel.sendMessage(full).submit().get();
} catch (Exception e) {
if (this.isInterruptedException(e)) return currentMessage;
throw new RuntimeException(e);
}
}

Expand Down

0 comments on commit a444d41

Please sign in to comment.