9
9
import java .awt .*;
10
10
import java .io .IOException ;
11
11
import java .io .OutputStream ;
12
+ import java .net .ProtocolException ;
12
13
import java .net .URL ;
13
14
import java .nio .charset .StandardCharsets ;
14
15
import java .util .ArrayList ;
@@ -93,112 +94,109 @@ private static Color getColor(GColor color) {
93
94
return color1 ;
94
95
}
95
96
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
+ }
103
101
104
- json .put ("username" , this .getUserName ());
105
- json .put ("avatar_url" , this .getPhotoUrl ());
106
- json .put ("tts" , false );
102
+ JSONObject json = new JSONObject ();
107
103
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 );
110
107
111
- for ( EmbedObject embed : this .embeds ) {
112
- JSONObject jsonEmbed = new JSONObject ();
108
+ if (! this .embeds . isEmpty () ) {
109
+ List < JSONObject > embedObjects = new ArrayList <> ();
113
110
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 ( );
116
113
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 ());
120
116
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 ());
126
120
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 ();
129
126
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
+ }
135
129
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 ();
138
135
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 ();
143
138
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
+ }
146
143
147
- jsonImage .put ("url" , image .getUrl ());
148
- jsonEmbed .put ("image" , jsonImage );
149
- }
144
+ if (image != null && !image .equals ("" )) {
145
+ JSONObject jsonImage = new JSONObject ();
150
146
151
- if (thumbnail != null && !thumbnail .equals ("" )) {
152
- JSONObject jsonThumbnail = new JSONObject ();
147
+ jsonImage .put ("url" , image .getUrl ());
148
+ jsonEmbed .put ("image" , jsonImage );
149
+ }
153
150
154
- jsonThumbnail .put ("url" , thumbnail .getUrl ());
155
- jsonEmbed .put ("thumbnail" , jsonThumbnail );
156
- }
151
+ if (thumbnail != null && !thumbnail .equals ("" )) {
152
+ JSONObject jsonThumbnail = new JSONObject ();
157
153
158
- if (author != null && !author .equals ("" )) {
159
- JSONObject jsonAuthor = new JSONObject ();
154
+ jsonThumbnail .put ("url" , thumbnail .getUrl ());
155
+ jsonEmbed .put ("thumbnail" , jsonThumbnail );
156
+ }
160
157
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 ();
166
160
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
+ }
170
166
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 ( );
174
170
175
- jsonFields .add (jsonField );
176
- }
171
+ jsonField .put ("name" , field .getName ());
172
+ jsonField .put ("value" , field .getValue ());
173
+ jsonField .put ("inline" , field .isInline ());
177
174
178
- jsonEmbed .put ("fields" , jsonFields .toArray ());
179
- embedObjects .add (jsonEmbed );
175
+ jsonFields .add (jsonField );
180
176
}
181
177
182
- json .put ("embeds" , embedObjects .toArray ());
178
+ jsonEmbed .put ("fields" , jsonFields .toArray ());
179
+ embedObjects .add (jsonEmbed );
183
180
}
184
181
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 ());
201
183
}
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 ();
203
201
}
204
202
}
0 commit comments