Skip to content

Commit 8bfa876

Browse files
committed
Removed try and catch for exeception handling
1 parent 2cec053 commit 8bfa876

File tree

2 files changed

+83
-83
lines changed

2 files changed

+83
-83
lines changed

src/main/java/xyz/geik/webhook/discord/Test.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package xyz.geik.webhook.discord;
22

3+
import java.io.IOException;
4+
35
public class Test {
46

5-
public static void main(String[] args) throws CloneNotSupportedException {
7+
public static void main(String[] args) throws IOException {
68
Webhook webhook = new Webhook("dataId", "https://discord.com/api/webhooks/840009672756559883/q5dn6sZNBmWyDRm2_QpBYSIB_uno9yMnoUrOFG21suN_c7w7g9tanWFJbrkkp1rWzauw",
79
"username", "https://cdn.discordapp.com/attachments/901908081569579029/901908177833058314/logo.png",
810
"https://cdn.discordapp.com/attachments/901908081569579029/901908177833058314/logo.png", "title",

src/main/java/xyz/geik/webhook/discord/Webhook.java

Lines changed: 80 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.awt.*;
1010
import java.io.IOException;
1111
import java.io.OutputStream;
12+
import java.net.ProtocolException;
1213
import java.net.URL;
1314
import java.nio.charset.StandardCharsets;
1415
import java.util.ArrayList;
@@ -93,112 +94,109 @@ private static Color getColor(GColor color) {
9394
return color1;
9495
}
9596

96-
public void execute() {
97-
try {
98-
if (this.content == null && this.embeds.isEmpty()) {
99-
throw new IllegalArgumentException("Set content or add at least one EmbedObject");
100-
}
101-
102-
JSONObject json = new JSONObject();
97+
public void execute() throws IOException {
98+
if (this.content == null && this.embeds.isEmpty()) {
99+
throw new IllegalArgumentException("Set content or add at least one EmbedObject");
100+
}
103101

104-
json.put("username", this.getUserName());
105-
json.put("avatar_url", this.getPhotoUrl());
106-
json.put("tts", false);
102+
JSONObject json = new JSONObject();
107103

108-
if (!this.embeds.isEmpty()) {
109-
List<JSONObject> embedObjects = new ArrayList<>();
104+
json.put("username", this.getUserName());
105+
json.put("avatar_url", this.getPhotoUrl());
106+
json.put("tts", false);
110107

111-
for (EmbedObject embed : this.embeds) {
112-
JSONObject jsonEmbed = new JSONObject();
108+
if (!this.embeds.isEmpty()) {
109+
List<JSONObject> embedObjects = new ArrayList<>();
113110

114-
if (embed.getTitle() != null && !embed.getTitle().equals(""))
115-
jsonEmbed.put("title", embed.getTitle());
111+
for (EmbedObject embed : this.embeds) {
112+
JSONObject jsonEmbed = new JSONObject();
116113

117-
if (embed.getDescription() != null && !embed.getDescription().equals(""))
118-
jsonEmbed.put("description", embed.getDescription());
119-
jsonEmbed.put("url", embed.getUrl());
114+
if (embed.getTitle() != null && !embed.getTitle().equals(""))
115+
jsonEmbed.put("title", embed.getTitle());
120116

121-
if (embed.getColor() != null) {
122-
Color color = embed.getColor();
123-
int rgb = color.getRed();
124-
rgb = (rgb << 8) + color.getGreen();
125-
rgb = (rgb << 8) + color.getBlue();
117+
if (embed.getDescription() != null && !embed.getDescription().equals(""))
118+
jsonEmbed.put("description", embed.getDescription());
119+
jsonEmbed.put("url", embed.getUrl());
126120

127-
jsonEmbed.put("color", rgb);
128-
}
121+
if (embed.getColor() != null) {
122+
Color color = embed.getColor();
123+
int rgb = color.getRed();
124+
rgb = (rgb << 8) + color.getGreen();
125+
rgb = (rgb << 8) + color.getBlue();
129126

130-
EmbedObject.Footer footer = embed.getFooter();
131-
EmbedObject.Image image = embed.getImage();
132-
EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
133-
EmbedObject.Author author = embed.getAuthor();
134-
List<EmbedObject.Field> fields = embed.getFields();
127+
jsonEmbed.put("color", rgb);
128+
}
135129

136-
if (footer != null && !footer.equals("")) {
137-
JSONObject jsonFooter = new JSONObject();
130+
EmbedObject.Footer footer = embed.getFooter();
131+
EmbedObject.Image image = embed.getImage();
132+
EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
133+
EmbedObject.Author author = embed.getAuthor();
134+
List<EmbedObject.Field> fields = embed.getFields();
138135

139-
jsonFooter.put("text", footer.getText());
140-
jsonFooter.put("icon_url", footer.getIconUrl());
141-
jsonEmbed.put("footer", jsonFooter);
142-
}
136+
if (footer != null && !footer.equals("")) {
137+
JSONObject jsonFooter = new JSONObject();
143138

144-
if (image != null && !image.equals("")) {
145-
JSONObject jsonImage = new JSONObject();
139+
jsonFooter.put("text", footer.getText());
140+
jsonFooter.put("icon_url", footer.getIconUrl());
141+
jsonEmbed.put("footer", jsonFooter);
142+
}
146143

147-
jsonImage.put("url", image.getUrl());
148-
jsonEmbed.put("image", jsonImage);
149-
}
144+
if (image != null && !image.equals("")) {
145+
JSONObject jsonImage = new JSONObject();
150146

151-
if (thumbnail != null && !thumbnail.equals("")) {
152-
JSONObject jsonThumbnail = new JSONObject();
147+
jsonImage.put("url", image.getUrl());
148+
jsonEmbed.put("image", jsonImage);
149+
}
153150

154-
jsonThumbnail.put("url", thumbnail.getUrl());
155-
jsonEmbed.put("thumbnail", jsonThumbnail);
156-
}
151+
if (thumbnail != null && !thumbnail.equals("")) {
152+
JSONObject jsonThumbnail = new JSONObject();
157153

158-
if (author != null && !author.equals("")) {
159-
JSONObject jsonAuthor = new JSONObject();
154+
jsonThumbnail.put("url", thumbnail.getUrl());
155+
jsonEmbed.put("thumbnail", jsonThumbnail);
156+
}
160157

161-
jsonAuthor.put("name", author.getName());
162-
jsonAuthor.put("url", author.getUrl());
163-
jsonAuthor.put("icon_url", author.getIconUrl());
164-
jsonEmbed.put("author", jsonAuthor);
165-
}
158+
if (author != null && !author.equals("")) {
159+
JSONObject jsonAuthor = new JSONObject();
166160

167-
List<JSONObject> jsonFields = new ArrayList<>();
168-
for (EmbedObject.Field field : fields) {
169-
JSONObject jsonField = new JSONObject();
161+
jsonAuthor.put("name", author.getName());
162+
jsonAuthor.put("url", author.getUrl());
163+
jsonAuthor.put("icon_url", author.getIconUrl());
164+
jsonEmbed.put("author", jsonAuthor);
165+
}
170166

171-
jsonField.put("name", field.getName());
172-
jsonField.put("value", field.getValue());
173-
jsonField.put("inline", field.isInline());
167+
List<JSONObject> jsonFields = new ArrayList<>();
168+
for (EmbedObject.Field field : fields) {
169+
JSONObject jsonField = new JSONObject();
174170

175-
jsonFields.add(jsonField);
176-
}
171+
jsonField.put("name", field.getName());
172+
jsonField.put("value", field.getValue());
173+
jsonField.put("inline", field.isInline());
177174

178-
jsonEmbed.put("fields", jsonFields.toArray());
179-
embedObjects.add(jsonEmbed);
175+
jsonFields.add(jsonField);
180176
}
181177

182-
json.put("embeds", embedObjects.toArray());
178+
jsonEmbed.put("fields", jsonFields.toArray());
179+
embedObjects.add(jsonEmbed);
183180
}
184181

185-
if (this.content != null && !this.content.equals(""))
186-
json.put("content", this.content);
187-
188-
URL url = new URL(this.url);
189-
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
190-
connection.addRequestProperty("Content-Type", "application/json");
191-
connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
192-
connection.setDoOutput(true);
193-
connection.setRequestMethod("POST");
194-
195-
OutputStream stream = connection.getOutputStream();
196-
stream.write(json.toString().getBytes(StandardCharsets.UTF_8));
197-
stream.flush();
198-
stream.close();
199-
connection.getInputStream().close();
200-
connection.disconnect();
182+
json.put("embeds", embedObjects.toArray());
201183
}
202-
catch (IOException e) { e.printStackTrace(); }
184+
185+
if (this.content != null && !this.content.equals(""))
186+
json.put("content", this.content);
187+
188+
URL url = new URL(this.url);
189+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
190+
connection.addRequestProperty("Content-Type", "application/json");
191+
connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
192+
connection.setDoOutput(true);
193+
connection.setRequestMethod("POST");
194+
195+
OutputStream stream = connection.getOutputStream();
196+
stream.write(json.toString().getBytes(StandardCharsets.UTF_8));
197+
stream.flush();
198+
stream.close();
199+
connection.getInputStream().close();
200+
connection.disconnect();
203201
}
204202
}

0 commit comments

Comments
 (0)