Advertisement
Dodo1213

WebhookBuilder

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