diff --git a/src/commands/CommandBlurry.java b/src/commands/CommandBlurry.java index c875ff2..a777897 100644 --- a/src/commands/CommandBlurry.java +++ b/src/commands/CommandBlurry.java @@ -39,7 +39,7 @@ public class CommandBlurry extends Command { try { - filename = ImageUtils.DownloadFromURL(input.args.split(" ")[0], ".png"); + filename = ImageUtils.downloadFromURL(input.args.split(" ")[0], ".png"); preProcessed = new File(filename); } catch(Exception e) @@ -48,7 +48,7 @@ public class CommandBlurry extends Command if(input.mentions != null) person = input.mentions[0]; - filename = ImageUtils.DownloadFromURL(person.avatarID, ".png"); + filename = ImageUtils.downloadFromURL(person.avatarID, ".png"); if(filename == null) return; } diff --git a/src/commands/CommandCatch.java b/src/commands/CommandCatch.java index fac6208..a2a55c7 100644 --- a/src/commands/CommandCatch.java +++ b/src/commands/CommandCatch.java @@ -48,7 +48,7 @@ public class CommandCatch extends Command else person = input.mentions[0]; - String catchFilename = ImageUtils.DownloadFromURL(person.avatarID, ".png"); + String catchFilename = ImageUtils.downloadFromURL(person.avatarID, ".png"); if(catchFilename == null) return; diff --git a/src/commands/CommandColor.java b/src/commands/CommandColor.java index aac5c04..5bdd9e9 100644 --- a/src/commands/CommandColor.java +++ b/src/commands/CommandColor.java @@ -3,6 +3,7 @@ package commands; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; +import java.io.File; import javax.imageio.ImageIO; @@ -16,6 +17,8 @@ import dataStructures.KittyRole; import dataStructures.KittyUser; import dataStructures.Response; import dataStructures.UserInput; +import utils.GlobalLog; +import utils.ImageUtils; public class CommandColor extends Command { @@ -27,26 +30,35 @@ public class CommandColor extends Command @Override public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) { + // Config int sideLength = 50; - - float r = 0; - float g = 0; - float b = 0; + float r = 209.0f/255; + float g = 74.0f/255; + float b = 153.0f/255; float a = 1; Color parsed = new Color(r, g, b, a); + GlobalLog.Log("1"); + + // Construct the image example file that we intend to display locally BufferedImage img = new BufferedImage(sideLength, sideLength, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D graphics = img.createGraphics(); graphics.setColor(parsed); graphics.fillRect(0, 0, sideLength, sideLength); graphics.dispose(); - + GlobalLog.Log("2"); + String tempFileName = ImageUtils.writeTempImageData(img, ".png"); + File file = new File(tempFileName); + GlobalLog.Log("3"); KittyEmbed response = new KittyEmbed(); + response.title = "Color Name"; response.color = parsed; - response.thumbnailURL = "attachment://test.png"; - + response.descriptionText = "`rgba` test\n`hex` test\n`hsv` test"; + response.thumbnailURL = "attachment://" + tempFileName; + GlobalLog.Log("4"); res.CallEmbed(response); - //ImageIO.write(bufferedImage, "png", file); + ImageUtils.BlockingFileDelete(file); + GlobalLog.Log("5"); }; } diff --git a/src/commands/CommandPerish.java b/src/commands/CommandPerish.java index 73ca6e6..d5803ee 100644 --- a/src/commands/CommandPerish.java +++ b/src/commands/CommandPerish.java @@ -45,7 +45,7 @@ public class CommandPerish extends Command { try { - filename = ImageUtils.DownloadFromURL(input.args.split(" ")[0], ".png"); + filename = ImageUtils.downloadFromURL(input.args.split(" ")[0], ".png"); preProcessed = new File(filename); } catch(Exception e) @@ -54,7 +54,7 @@ public class CommandPerish extends Command if(input.mentions != null) person = input.mentions[0]; - filename = ImageUtils.DownloadFromURL(person.avatarID, ".png"); + filename = ImageUtils.downloadFromURL(person.avatarID, ".png"); if(filename == null) return; } diff --git a/src/commands/CommandStark.java b/src/commands/CommandStark.java index 16378c8..1f474ae 100644 --- a/src/commands/CommandStark.java +++ b/src/commands/CommandStark.java @@ -39,7 +39,7 @@ public class CommandStark extends Command { try { - filename = ImageUtils.DownloadFromURL(input.args.split(" ")[0], ".png"); + filename = ImageUtils.downloadFromURL(input.args.split(" ")[0], ".png"); preProcessed = new File(filename); } catch(Exception e) @@ -48,7 +48,7 @@ public class CommandStark extends Command if(input.mentions != null) person = input.mentions[0]; - filename = ImageUtils.DownloadFromURL(person.avatarID, ".png"); + filename = ImageUtils.downloadFromURL(person.avatarID, ".png"); if(filename == null) return; } diff --git a/src/commands/CommandTeey.java b/src/commands/CommandTeey.java index 6a3394a..99f2631 100644 --- a/src/commands/CommandTeey.java +++ b/src/commands/CommandTeey.java @@ -48,7 +48,7 @@ public class CommandTeey extends Command else person = input.mentions[0]; - String yeeteeFilename = ImageUtils.DownloadFromURL(person.avatarID, ".png"); + String yeeteeFilename = ImageUtils.downloadFromURL(person.avatarID, ".png"); if(yeeteeFilename == null) return; diff --git a/src/commands/CommandWolfram.java b/src/commands/CommandWolfram.java index 4947261..2fe5871 100644 --- a/src/commands/CommandWolfram.java +++ b/src/commands/CommandWolfram.java @@ -29,7 +29,7 @@ public class CommandWolfram extends Command res.CallFile(pic, "png"); ImageUtils.BlockingFileDelete(pic); } - catch (IOException e) + catch (IOException e) { res.Call(LocStrings.Stub("WolframError")); } diff --git a/src/commands/CommandYeet.java b/src/commands/CommandYeet.java index 65ea728..b3fd5b4 100644 --- a/src/commands/CommandYeet.java +++ b/src/commands/CommandYeet.java @@ -48,7 +48,7 @@ public class CommandYeet extends Command else person = input.mentions[0]; - String yeeteeFilename = ImageUtils.DownloadFromURL(person.avatarID, ".png"); + String yeeteeFilename = ImageUtils.downloadFromURL(person.avatarID, ".png"); if(yeeteeFilename == null) return; diff --git a/src/dataStructures/Response.java b/src/dataStructures/Response.java index afbc8a4..5ce4c3b 100644 --- a/src/dataStructures/Response.java +++ b/src/dataStructures/Response.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.InputStream; import net.dv8tion.jda.core.JDA; +import net.dv8tion.jda.core.MessageBuilder; import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.events.message.guild.*; import net.dv8tion.jda.core.EmbedBuilder; @@ -51,12 +52,33 @@ public class Response embed.setAuthor(embedInfo.authorText, embedInfo.authorLink, embedInfo.authorImage); if(embedInfo.imageURL != null) - embed.setImage(embedInfo.imageURL); + embed.setImage(embedInfo.imageURL); - if(embedInfo.thumbnailURL != null) - embed.setThumbnail(embedInfo.thumbnailURL); - - event.getChannel().sendMessage(embed.build()).queue(); + if(embedInfo.thumbnailURL != null && embedInfo.thumbnailURL.contains("attachment://")) + { + // This is the case where you have a thumbnail image, but you want it to be a local file. + // TODO: Make this more generic. Consider message builder for the entire thing. + String thumbPath = embedInfo.thumbnailURL; + String thumbName = thumbPath.replace("attachment://", ""); + + MessageBuilder message = new MessageBuilder(); + embed.setThumbnail(thumbPath); + message.setEmbed(embed.build()); + + File thumbImage = new File(thumbName); + if(thumbImage.exists()) + { + event.getChannel().sendFile(thumbImage, thumbName, message.build()).queue(); + } + } + else + { + if(embedInfo.thumbnailURL != null) + embed.setThumbnail(embedInfo.thumbnailURL); + + event.getChannel().sendMessage(embed.build()); + } + } // Queues a standard text-based message response to the channel that issued the command. diff --git a/src/utils/ImageUtils.java b/src/utils/ImageUtils.java index e85af12..23dcbdc 100644 --- a/src/utils/ImageUtils.java +++ b/src/utils/ImageUtils.java @@ -5,19 +5,45 @@ import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import javax.imageio.ImageIO; + public class ImageUtils { private static Long uniqueID = 0l; + public static String writeTempImageData(BufferedImage data, String extension) + { + String name = null; + synchronized(uniqueID) + { + name = "imagetempwriterutil_" + (uniqueID++) + extension; + } + + try + { + File outputfile = new File(name); + ImageIO.write(data, extension, outputfile); + + return name; + } + catch (IOException e) + { + GlobalLog.Error(LogFilter.Util, "Exception in writeTempImageData: " + e.toString()); + return null; + } + + } + // Downloads the file at the url and assigns it a unique local string. // The string (filename) is returned, and can be used. It will have to be // deleted later. By assigning a unique name to the files, this is - public static String DownloadFromURL(String URL, String extension) + public static String downloadFromURL(String URL, String extension) { String name = null; synchronized(uniqueID) @@ -51,6 +77,7 @@ public class ImageUtils } catch(Exception e) { + GlobalLog.Error(LogFilter.Util, "Exception in downloadFromURL: " + e.toString()); return null; } } diff --git a/test.png b/test.png index 0c987f9..3e54667 100644 Binary files a/test.png and b/test.png differ