Skip to content

Instantly share code, notes, and snippets.

@nkomarn
Created February 10, 2019 19:44
Show Gist options
  • Select an option

  • Save nkomarn/966a37dbff9bc5a02e3fcc8712313815 to your computer and use it in GitHub Desktop.

Select an option

Save nkomarn/966a37dbff9bc5a02e3fcc8712313815 to your computer and use it in GitHub Desktop.
A simple and effective color picker tool for Discord JDA!
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