Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "Message blocked by harmful links filter" error #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions jda4/src/main/java/me/scarsz/jdaappender/ChannelLoggingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public ChannelLoggingHandler schedule(long period, @NotNull TimeUnit unit) {
/**
* RegEx pattern used to check if a URL contains a link for use with {@link HandlerConfig#isAllowLinkEmbeds()}
*/
private static final Pattern URL_PATTERN = Pattern.compile("https?://\\S+");
private static final Pattern URL_PATTERN = Pattern.compile("https?:\\/\\/((?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]:?\\d*\\/?[a-zA-Z0-9_\\/\\-#.]*\\??[a-zA-Z0-9\\-_~:\\/?#\\[\\]@!$&'()*+,;=%.]*)");

/**
* Error code for "Message blocked by harmful links filter" ErrorResponse
*/
private static final int MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER_ERROR_CODE = 240000;

@Getter private final HandlerConfig config = new HandlerConfig();
@Getter private final Deque<LogItem> messageQueue = new LinkedList<>();
Expand Down Expand Up @@ -236,25 +241,46 @@ private Message updateMessage() throws IllegalStateException {
// safeguard against empty lines
while (full.contains("\n\n")) full = full.replace("\n\n", "\n");

if (currentMessage != null) {
try {
return currentMessage.editMessage(full).submit().get();
} 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(e.getCause());
try {
// 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.getErrorCode() == MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER_ERROR_CODE) {
full = URL_PATTERN.matcher(full).replaceAll("$1");
return sendOrEditMessage(full, channel);
}
throw ex;
}
}

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

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

throw new RuntimeException(e);
}
}
Expand Down
49 changes: 35 additions & 14 deletions jda5/src/main/java/me/scarsz/jdaappender/ChannelLoggingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ChannelLoggingHandler schedule(long period, @NotNull TimeUnit unit) {
/**
* RegEx pattern used to check if a URL contains a link for use with {@link HandlerConfig#isAllowLinkEmbeds()}
*/
private static final Pattern URL_PATTERN = Pattern.compile("https?://\\S+");
private static final Pattern URL_PATTERN = Pattern.compile("https?:\\/\\/((?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]:?\\d*\\/?[a-zA-Z0-9_\\/\\-#.]*\\??[a-zA-Z0-9\\-_~:\\/?#\\[\\]@!$&'()*+,;=%.]*)");

@Getter private final HandlerConfig config = new HandlerConfig();
@Getter private final Deque<LogItem> messageQueue = new LinkedList<>();
Expand Down Expand Up @@ -236,25 +236,46 @@ private Message updateMessage() throws IllegalStateException {
// safeguard against empty lines
while (full.contains("\n\n")) full = full.replace("\n\n", "\n");

if (currentMessage != null) {
try {
return currentMessage.editMessage(full).submit().get();
} 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);
try {
// 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);
}
throw ex;
}
}

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

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

throw new RuntimeException(e);
}
}
Expand Down