Created
February 10, 2019 19:44
-
-
Save nkomarn/966a37dbff9bc5a02e3fcc8712313815 to your computer and use it in GitHub Desktop.
A simple and effective color picker tool for Discord JDA!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| event.getChannel().sendTyping().complete(); | |
| Random random = new Random(); | |
| int nextInt = random.nextInt(0xffffff + 1); | |
| try { | |
| BufferedImage img = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB); | |
| Graphics graphics = img.getGraphics(); | |
| graphics.setColor(Color.decode(String.valueOf(nextInt))); | |
| graphics.fillRect(0, 0, 500, 500); | |
| graphics.dispose(); | |
| File file = new File("image.png"); | |
| ImageIO.write(img, "png", file); | |
| HttpURLConnection conn = (HttpURLConnection) new URL("https://api.imgur.com/3/image").openConnection(); | |
| conn.setDoInput(true); | |
| conn.setDoOutput(true); | |
| conn.setRequestMethod("POST"); | |
| // TODO Put in your client ID from https://imgur.com/register/api/ | |
| conn.setRequestProperty("Authorization", "Client-ID " + "PUT IMGUR API CLIENT ID HERE!"); | |
| conn.setReadTimeout(100000); | |
| conn.connect(); | |
| // Convert to base64 | |
| byte[] b = new byte[(int) file.length()]; | |
| FileInputStream fs = new FileInputStream(file); | |
| fs.read(b); | |
| fs.close(); | |
| String message = URLEncoder.encode(DatatypeConverter.printBase64Binary(b), "UTF-8"); | |
| OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); | |
| writer.write("image=" + message); | |
| writer.flush(); | |
| writer.close(); | |
| // Get response | |
| BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
| StringBuilder str = new StringBuilder(); | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| str.append(line); | |
| } | |
| JSONParser parser = new JSONParser(); | |
| JSONObject json = (JSONObject) parser.parse(str.toString()); | |
| String data = json.get("data").toString(); | |
| JSONObject dataJson = (JSONObject) parser.parse(data); | |
| reader.close(); | |
| // Convert to hex | |
| String colorCode = String.format("#%06x", nextInt); | |
| // Build embed | |
| EmbedBuilder embed = new EmbedBuilder(); | |
| embed.setColor(nextInt); | |
| embed.setTitle("🏳 Here's your color."); | |
| embed.addField("Color code", colorCode, false); | |
| embed.setThumbnail(dataJson.get("link").toString()); | |
| event.getChannel().sendMessage(embed.build()).queue(); | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment