diff --git a/src/main/java/net/dv8tion/jda/api/EmbedBuilder.java b/src/main/java/net/dv8tion/jda/api/EmbedBuilder.java index 50363dcce9..4ae617edb2 100644 --- a/src/main/java/net/dv8tion/jda/api/EmbedBuilder.java +++ b/src/main/java/net/dv8tion/jda/api/EmbedBuilder.java @@ -907,12 +907,46 @@ public List getFields() return fields; } + private static boolean hasUrlFormat(@Nonnull String url) + { + if (url.regionMatches(true, 0, "https://", 0, "https://".length())) + { + return url.length() > "https://".length(); + } + if (url.regionMatches(true, 0, "http://", 0, "http://".length())) + { + return url.length() > "http://".length(); + } + if (url.regionMatches(true, 0, "attachment://", 0, "attachment://".length())) + { + return url.length() > "attachment://".length(); + } + return false; + } + + /** + * Checks if the provided {@code url} is a valid http(s) or attachment url. + * The length of the {@code url} can't exceed {@link MessageEmbed#URL_MAX_LENGTH}. + * Note that this check is case-insensitive and at least one character is required + * after the url prefix. Meaning that "HtTpS://" will return {@code false}, while + * "HtTpS://a" will return {@code true}. + * + * @param url The string to check. + * + * @return {@code true} if {@code url} is a valid http(s) or attachment url, + * {@code false} otherwise. + */ + public static boolean isUrlOrAttachment(@Nullable String url) + { + return url != null && url.length() <= MessageEmbed.URL_MAX_LENGTH && hasUrlFormat(url); + } + private void urlCheck(@Nullable String url) { if (url != null) { Checks.notLonger(url, MessageEmbed.URL_MAX_LENGTH, "URL"); - Checks.check(URL_PATTERN.matcher(url).matches(), "URL must be a valid http(s) or attachment url."); + Checks.check(hasUrlFormat(url), "URL must be a valid http(s) or attachment url."); } } }