Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. /*
  2. * (c) 2013 InfinityArts
  3. * All codes are for use only in HiddenProject
  4. */
  5. package alter.discord;
  6.  
  7. import javax.net.ssl.HttpsURLConnection;
  8. import java.awt.Color;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.lang.reflect.Array;
  12. import java.net.URL;
  13. import java.text.SimpleDateFormat;
  14. import java.util.*;
  15.  
  16.  
  17. /**
  18. *
  19. * @author Rin
  20. */
  21.  
  22. /**
  23. * Class used to execute AccessListener Webhooks with low effort
  24. */
  25. public class Webhook {
  26.  
  27. private final String url;
  28. private String content;
  29. private String username;
  30. private String avatarUrl;
  31. private boolean tts;
  32. private List<EmbedObject> embeds = new ArrayList<>();
  33.  
  34. /**
  35. * Constructs a new DiscordWebhook instance
  36. *
  37. * @param url The webhook URL obtained in AccessListener
  38. */
  39. public Webhook(String url) {
  40. this.url = url;
  41. }
  42.  
  43. public void setContent(String content) {
  44. this.content = content;
  45. }
  46.  
  47. public void setUsername(String username) {
  48. this.username = username;
  49. }
  50.  
  51. public void setAvatarUrl(String avatarUrl) {
  52. this.avatarUrl = avatarUrl;
  53. }
  54.  
  55. public void setTts(boolean tts) {
  56. this.tts = tts;
  57. }
  58.  
  59. public void addEmbed(EmbedObject embed) {
  60. this.embeds.add(embed);
  61. }
  62.  
  63. public void execute() throws IOException {
  64. if (this.content == null && this.embeds.isEmpty()) {
  65. throw new IllegalArgumentException("Set content or add at least one EmbedObject");
  66. }
  67.  
  68. JSONObject json = new JSONObject();
  69.  
  70. json.put("content", this.content);
  71. json.put("username", this.username);
  72. json.put("avatar_url", this.avatarUrl);
  73. json.put("tts", this.tts);
  74.  
  75. if (!this.embeds.isEmpty()) {
  76. List<JSONObject> embedObjects = new ArrayList<>();
  77.  
  78. for (EmbedObject embed : this.embeds) {
  79. JSONObject jsonEmbed = new JSONObject();
  80. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
  81.  
  82. jsonEmbed.put("title", embed.getTitle());
  83. jsonEmbed.put("description", embed.getDescription());
  84. jsonEmbed.put("url", embed.getUrl());
  85. jsonEmbed.put("timestamp", df.format(new Date()));
  86.  
  87. if (embed.getColor() != null) {
  88. Color color = embed.getColor();
  89. int rgb = color.getRed();
  90. rgb = (rgb << 8) + color.getGreen();
  91. rgb = (rgb << 8) + color.getBlue();
  92.  
  93. jsonEmbed.put("color", rgb);
  94. }
  95.  
  96. EmbedObject.Footer footer = embed.getFooter();
  97. EmbedObject.Image image = embed.getImage();
  98. EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
  99. EmbedObject.Author author = embed.getAuthor();
  100. List<EmbedObject.Field> fields = embed.getFields();
  101.  
  102. if (footer != null) {
  103. JSONObject jsonFooter = new JSONObject();
  104.  
  105. jsonFooter.put("text", footer.getText());
  106. jsonFooter.put("icon_url", footer.getIconUrl());
  107. jsonEmbed.put("footer", jsonFooter);
  108. }
  109.  
  110. if (image != null) {
  111. JSONObject jsonImage = new JSONObject();
  112.  
  113. jsonImage.put("url", image.getUrl());
  114. jsonEmbed.put("image", jsonImage);
  115. }
  116.  
  117. if (thumbnail != null) {
  118. JSONObject jsonThumbnail = new JSONObject();
  119.  
  120. jsonThumbnail.put("url", thumbnail.getUrl());
  121. jsonEmbed.put("thumbnail", jsonThumbnail);
  122. }
  123.  
  124. if (author != null) {
  125. JSONObject jsonAuthor = new JSONObject();
  126.  
  127. jsonAuthor.put("name", author.getName());
  128. jsonAuthor.put("url", author.getUrl());
  129. jsonAuthor.put("icon_url", author.getIconUrl());
  130. jsonEmbed.put("author", jsonAuthor);
  131. }
  132.  
  133. List<JSONObject> jsonFields = new ArrayList<>();
  134. for (EmbedObject.Field field : fields) {
  135. JSONObject jsonField = new JSONObject();
  136.  
  137. jsonField.put("name", field.getName());
  138. jsonField.put("value", field.getValue());
  139. jsonField.put("inline", field.isInline());
  140.  
  141. jsonFields.add(jsonField);
  142. }
  143.  
  144. jsonEmbed.put("fields", jsonFields.toArray());
  145. embedObjects.add(jsonEmbed);
  146. }
  147.  
  148. json.put("embeds", embedObjects.toArray());
  149. }
  150.  
  151. URL url = new URL(this.url);
  152. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  153. connection.addRequestProperty("Content-Type", "application/json");
  154. connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
  155. connection.setDoOutput(true);
  156. connection.setRequestMethod("POST");
  157.  
  158. OutputStream stream = connection.getOutputStream();
  159. stream.write(json.toString().getBytes());
  160. stream.flush();
  161. stream.close();
  162.  
  163. connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream
  164. connection.disconnect();
  165. }
  166.  
  167. public static class EmbedObject {
  168. private String title;
  169. private String description;
  170. private String url;
  171. private Color color;
  172.  
  173. private Footer footer;
  174. private Thumbnail thumbnail;
  175. private Image image;
  176. private Author author;
  177. private List<Field> fields = new ArrayList<>();
  178.  
  179. public String getTitle() {
  180. return title;
  181. }
  182.  
  183. public String getDescription() {
  184. return description;
  185. }
  186.  
  187. public String getUrl() {
  188. return url;
  189. }
  190.  
  191. public Color getColor() {
  192. return color;
  193. }
  194.  
  195. public Footer getFooter() {
  196. return footer;
  197. }
  198.  
  199. public Thumbnail getThumbnail() {
  200. return thumbnail;
  201. }
  202.  
  203. public Image getImage() {
  204. return image;
  205. }
  206.  
  207. public Author getAuthor() {
  208. return author;
  209. }
  210.  
  211. public List<Field> getFields() {
  212. return fields;
  213. }
  214.  
  215. public EmbedObject setTitle(String title) {
  216. this.title = title;
  217. return this;
  218. }
  219.  
  220. public EmbedObject setDescription(String description) {
  221. this.description = description;
  222. return this;
  223. }
  224.  
  225. public EmbedObject setUrl(String url) {
  226. this.url = url;
  227. return this;
  228. }
  229.  
  230. public EmbedObject setColor(Color color) {
  231. this.color = color;
  232. return this;
  233. }
  234.  
  235. public EmbedObject setFooter(String text, String icon) {
  236. this.footer = new Footer(text, icon);
  237. return this;
  238. }
  239.  
  240. public EmbedObject setThumbnail(String url) {
  241. this.thumbnail = new Thumbnail(url);
  242. return this;
  243. }
  244.  
  245. public EmbedObject setImage(String url) {
  246. this.image = new Image(url);
  247. return this;
  248. }
  249.  
  250. public EmbedObject setAuthor(String name, String url, String icon) {
  251. this.author = new Author(name, url, icon);
  252. return this;
  253. }
  254.  
  255. public EmbedObject addField(String name, String value, boolean inline) {
  256. this.fields.add(new Field(name, value, inline));
  257. return this;
  258. }
  259.  
  260. private class Footer {
  261. private String text;
  262. private String iconUrl;
  263.  
  264. private Footer(String text, String iconUrl) {
  265. this.text = text;
  266. this.iconUrl = iconUrl;
  267. }
  268.  
  269. private String getText() {
  270. return text;
  271. }
  272.  
  273. private String getIconUrl() {
  274. return iconUrl;
  275. }
  276. }
  277.  
  278. private class Thumbnail {
  279. private String url;
  280.  
  281. private Thumbnail(String url) {
  282. this.url = url;
  283. }
  284.  
  285. private String getUrl() {
  286. return url;
  287. }
  288. }
  289.  
  290. private class Image {
  291. private String url;
  292.  
  293. private Image(String url) {
  294. this.url = url;
  295. }
  296.  
  297. private String getUrl() {
  298. return url;
  299. }
  300. }
  301.  
  302. private class Author {
  303. private String name;
  304. private String url;
  305. private String iconUrl;
  306.  
  307. private Author(String name, String url, String iconUrl) {
  308. this.name = name;
  309. this.url = url;
  310. this.iconUrl = iconUrl;
  311. }
  312.  
  313. private String getName() {
  314. return name;
  315. }
  316.  
  317. private String getUrl() {
  318. return url;
  319. }
  320.  
  321. private String getIconUrl() {
  322. return iconUrl;
  323. }
  324. }
  325.  
  326. private class Field {
  327. private String name;
  328. private String value;
  329. private boolean inline;
  330.  
  331. private Field(String name, String value, boolean inline) {
  332. this.name = name;
  333. this.value = value;
  334. this.inline = inline;
  335. }
  336.  
  337. private String getName() {
  338. return name;
  339. }
  340.  
  341. private String getValue() {
  342. return value;
  343. }
  344.  
  345. private boolean isInline() {
  346. return inline;
  347. }
  348. }
  349. }
  350.  
  351. private class JSONObject {
  352.  
  353. private final HashMap<String, Object> map = new HashMap<>();
  354.  
  355. void put(String key, Object value) {
  356. if (value != null) {
  357. map.put(key, value);
  358. }
  359. }
  360.  
  361. @Override
  362. public String toString() {
  363. StringBuilder builder = new StringBuilder();
  364. Set<Map.Entry<String, Object>> entrySet = map.entrySet();
  365. builder.append("{");
  366.  
  367. int i = 0;
  368. for (Map.Entry<String, Object> entry : entrySet) {
  369. Object val = entry.getValue();
  370. builder.append(quote(entry.getKey())).append(":");
  371.  
  372. if (val instanceof String) {
  373. builder.append(quote(String.valueOf(val)));
  374. } else if (val instanceof Integer) {
  375. builder.append(Integer.valueOf(String.valueOf(val)));
  376. } else if (val instanceof Boolean) {
  377. builder.append(val);
  378. } else if (val instanceof JSONObject) {
  379. builder.append(val.toString());
  380. } else if (val.getClass().isArray()) {
  381. builder.append("[");
  382. int len = Array.getLength(val);
  383. for (int j = 0; j < len; j++) {
  384. builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : "");
  385. }
  386. builder.append("]");
  387. }
  388.  
  389. builder.append(++i == entrySet.size() ? "}" : ",");
  390. }
  391.  
  392. return builder.toString();
  393. }
  394.  
  395. private String quote(String string) {
  396. return "\"" + string + "\"";
  397. }
  398. }
  399.  
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement