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 5 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
55 changes: 41 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,47 @@ 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 {
return sendOrEditMessage(full, channel);
} catch (ErrorResponseException errorResponseException) {
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
currentMessage = null;
// in case the message was deleted and the original message had a harmful link, catch the error again
try {
return sendOrEditMessage(full, channel);
} catch (ErrorResponseException errorResponseException2) {
if (errorResponseException2.getErrorCode() == MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER_ERROR_CODE) {
full = URL_PATTERN.matcher(full).replaceAll("$1");
return sendOrEditMessage(full, channel);
}
throw new RuntimeException(errorResponseException2);
}
} else if (errorResponseException.getErrorCode() == MESSAGE_BLOCKED_BY_HARMFUL_LINK_FILTER_ERROR_CODE) {
full = URL_PATTERN.matcher(full).replaceAll("$1");
return sendOrEditMessage(full, channel);
} else {
throw new RuntimeException(errorResponseException);
granny marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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;
}
granny marked this conversation as resolved.
Show resolved Hide resolved

throw new RuntimeException(e);
}
}
Expand Down
40 changes: 25 additions & 15 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,35 @@ 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 {
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) {
full = URL_PATTERN.matcher(full).replaceAll("$1");
return sendOrEditMessage(full, channel);
} else {
throw new RuntimeException(errorResponseException);
}
}
}

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;

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

throw new RuntimeException(e);
}
}
Expand Down