Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //new code
- public static RequestBuffer.RequestFuture<IMessage> sendFileURL(String message, String imageURL, IChannel channel, boolean loadMessage) {
- if (Utility.isImageLink(imageURL) || imageURL.contains("giphy.gif")) {
- return sendMessage(message + "\n" + imageURL, channel);
- }
- IMessage loading = null;
- if (loadMessage) {
- loading = sendMessage("`Loading...`", channel).get();
- }
- RequestBuffer.RequestFuture<IMessage> sent = RequestBuffer.request(() -> {
- IMessage sentMessage = null;
- InputStream stream = null;
- int responseCode = -1;
- try {
- //connect to the Image URL
- HttpURLConnection connection = (HttpURLConnection) new URL(imageURL).openConnection();
- connection.setRequestProperty("User-Agent", Constants.MOZILLA_USER_AGENT);
- //get responseCode in case of IOException;
- responseCode = connection.getResponseCode();
- //turn the image connection into an inputStream
- stream = connection.getInputStream();
- //image's file name
- String filename = FilenameUtils.getName(new URL(imageURL).getPath());
- //send file
- if (StringUtils.containsOnly(message, "\n") || (message == null) || message.equals("") && imageURL != null) {
- sentMessage = channel.sendFile("", stream, filename);
- } else if (message != null && !message.isEmpty() && imageURL != null) {
- sentMessage = channel.sendFile(Utility.removeMentions(message), false, stream, filename);
- } else {
- logger.debug("Error sending File to channel with id: " + channel.getLongID() + " on guild with id: " + channel.getGuild().getLongID() +
- ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: No file to send");
- }
- } catch (MissingPermissionsException e) {
- //send message and url with url closed
- missingPermissions("URL_FILE", channel);
- sentMessage = sendMessage(message + " <" + imageURL + ">", channel).get();
- } catch (RateLimitException e) {
- //send exception back out, needed for request handling.
- throw e;
- } catch (MalformedURLException e) {
- //this should never show up. seriously
- Utility.sendStack(e);
- } catch (SSLHandshakeException e) {
- //something to do with the ssl handshake failed. unsure what causes this.
- sendMessage(message + " " + imageURL + " `FAILED TO EMBED - Failed SSL Handshake`", channel).get();
- } catch (IOException e) {
- //the file failed to be grabbed.
- String response = " `FAILED TO EMBED - ERROR:" + responseCode + "`";
- if (responseCode == 403) {
- sentMessage = sendMessage(message + "\n" + imageURL + " `ERROR: 403, IMAGE FAILED TO RESPOND, IMAGE LINK NEEDS UPDATING.`", channel).get();
- } else if (responseCode != -1) {
- sentMessage = sendMessage(message + "\n" + imageURL + response, channel).get();
- } else {
- sentMessage = sendMessage(message + "\n" + imageURL, channel).get();
- }
- } catch (IllegalArgumentException e) {
- //the host failed, unsure as to the cause. inspect.
- if (e.getMessage().contains("http host = null")) {
- sendMessage("> `HTTP HOST ERROR, CHECK URL FOR ERRORS.`", channel);
- }
- }
- try {
- //close off the stream
- if (stream != null) stream.close();
- } catch (IOException e) {
- //how the hell did this even happen
- Utility.sendStack(e);
- }
- //return the completed message
- return sentMessage;
- });
- if (loading != null) {
- deleteMessage(loading);
- }
- return sent;
- }
- //old code
- public static IMessage sendFileURL(String message, String imageURL, IChannel channel, boolean loadMessage) {
- IMessage toDelete = null;
- if (loadMessage) {
- toDelete = sendMessage("`Loading...`", channel).get();
- }
- IMessage sentMessage = null;
- HttpURLConnection connection = null;
- try {
- 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");
- //setup for the stream
- InputStream stream = connection.getInputStream();
- sentMessage = RequestBuffer.request(() -> {
- try {
- //set up the file name
- URL url = new URL(imageURL);
- String filename = FilenameUtils.getName(url.getPath());
- if (filename.equalsIgnoreCase("giphy.gif")) {
- return sendMessage(message + " " + imageURL, channel).get();
- }
- //checks if url is valid
- if (!Utility.isImageLink(filename)) {
- return sendMessage(message + " " + imageURL, channel).get();
- }
- //sends message/files
- if (StringUtils.containsOnly(message, "\n") || (message == null) || message.equals("") && imageURL != null) {
- return channel.sendFile("", stream, filename);
- } else if (message != null && !message.isEmpty() && imageURL != null) {
- return channel.sendFile(Utility.removeMentions(message), false, stream, filename);
- } else {
- logger.debug("Error sending File to channel with id: " + channel.getLongID() + " on guild with id: " + channel.getGuild().getLongID() +
- ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: No file to send");
- return null;
- }
- } catch (MalformedURLException e) {
- return sendMessage(message + " " + imageURL, channel).get();
- } catch (MissingPermissionsException e) {
- missingPermissions("URL_FILE", channel);
- return sendMessage(message + " <" + imageURL + ">", channel).get();
- }
- }).get();
- stream.close();
- } catch (MalformedURLException e) {
- Utility.sendStack(e);
- } catch (SSLHandshakeException e) {
- sendMessage(message + " " + imageURL + " `FAILED TO EMBED - Failed SSL Handshake`", channel).get();
- } catch (IOException e) {
- try {
- if (connection != null) {
- int responseCode = connection.getResponseCode();
- sendMessage(message + " " + imageURL + " `FAILED TO EMBED - ERROR:" + responseCode + "`", channel).get();
- } else {
- Utility.sendStack(e);
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- } catch (IllegalArgumentException e) {
- if (e.getMessage().contains("http host = null")) {
- sendMessage("> `HTTP HOST ERROR, CHECK URL FOR ERRORS.`", channel);
- }
- }
- if (loadMessage && toDelete != null) {
- deleteMessage(toDelete);
- }
- return sentMessage;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement