Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static RequestBuffer.RequestFuture<Boolean> sendFile(String message, String imageURL, IChannel channel) {
- return RequestBuffer.request(() -> {
- String messageID = "";
- try {
- messageID = sendMessage("`Loading...`", channel).get();
- final HttpURLConnection connection = (HttpURLConnection) new URL(imageURL).openConnection();
- connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) " + "AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
- InputStream stream = connection.getInputStream();
- String[] urlSplit = imageURL.split(Pattern.quote("."));
- String suffix = "." + urlSplit[urlSplit.length - 1];
- if (!isImageLink(suffix)) {
- sendMessage(message + " " + imageURL, channel);
- deleteMessage(Globals.getClient().getMessageByID(messageID));
- return true;
- }
- if (StringUtils.containsOnly(message, "\n") || (message == null) || message.equals("")) {
- if (imageURL != null) {
- channel.sendFile("", stream, suffix);
- } else {
- logger.debug("Error sending File to channel with id: " + channel.getID() + " on guild with id: " + channel.getGuild().getID() +
- ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: No file to send");
- return true;
- }
- } else {
- if (imageURL != null) {
- channel.sendFile(removeMentions(message), true, stream, suffix);
- } else {
- sendMessage(message, channel);
- return true;
- }
- }
- Globals.getClient().getMessageByID(messageID).delete();
- } catch (DiscordException e) {
- if (e.getMessage().contains("CloudFlare")) {
- sendFile(message, imageURL, channel);
- deleteMessage(Globals.getClient().getMessageByID(messageID));
- } else {
- e.printStackTrace();
- return true;
- }
- } catch (MalformedURLException e) {
- sendMessage(message + " " + imageURL, channel);
- deleteMessage(Globals.getClient().getMessageByID(messageID));
- } catch (FileNotFoundException e) {
- Utility.sendMessage("> Image Not Found : " + imageURL, channel);
- deleteMessage(Globals.getClient().getMessageByID(messageID));
- } catch (IOException e) {
- e.printStackTrace();
- } catch (MissingPermissionsException e) {
- sendMessage(message + " <" + imageURL + ">", channel);
- deleteMessage(Globals.getClient().getMessageByID(messageID));
- logger.debug("Error sending File to channel with id: " + channel.getID() + " on guild with id: " + channel.getGuild().getID() +
- ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: Missing permissions.");
- return true;
- }
- return false;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment