@@ -110,12 +110,11 @@ public Discord(String botToken, IdentifyRequest identifyRequest) {
110
110
111
111
public Discord (String botToken , IdentifyRequest identifyRequest , Cache cache ) {
112
112
System .err .println ("""
113
- _ ____ ___\s
113
+ _ ____ ___
114
114
| | _ \\ _ _| https://github.com/javadiscord/java-discord-api
115
- _ | | | | | | Open-Source Discord Framework\s
116
- | |_| | |_| | | GPL-3.0 license\s
115
+ _ | | | | | | Open-Source Discord Framework
116
+ | |_| | |_| | | GPL-3.0 license
117
117
\\ ___/|____/___| Version 1.0
118
-
119
118
""" );
120
119
121
120
this .botToken = botToken ;
@@ -140,8 +139,7 @@ private void registerLoadedAnnotationsWithDiscord() {
140
139
try {
141
140
Class <?> slashCommandClassInstanceClass = slashCommandClassInstance .getClass ();
142
141
Method method =
143
- (Method ) slashCommandClassInstanceClass
144
- .getMethod ("method" )
142
+ (Method ) slashCommandClassInstanceClass .getMethod ("method" )
145
143
.invoke (slashCommandClassInstance );
146
144
147
145
Annotation [] annotations = method .getAnnotations ();
@@ -150,66 +148,7 @@ private void registerLoadedAnnotationsWithDiscord() {
150
148
annotation .annotationType ().getName ()
151
149
.equals ("com.javadiscord.jdi.core.annotations.SlashCommand" )
152
150
) {
153
- Method nameMethod = annotation .annotationType ().getMethod ("name" );
154
- String name = (String ) nameMethod .invoke (annotation );
155
-
156
- Method descriptionMethod =
157
- annotation .annotationType ().getMethod ("description" );
158
- String description = (String ) descriptionMethod .invoke (annotation );
159
-
160
- Method optionsMethod = annotation .annotationType ().getMethod ("options" );
161
- Object [] options = (Object []) optionsMethod .invoke (annotation );
162
-
163
- CommandBuilder builder = new CommandBuilder (name , description );
164
-
165
- for (Object option : options ) {
166
- Method optionNameMethod = option .getClass ().getMethod ("name" );
167
- String optionName = (String ) optionNameMethod .invoke (option );
168
-
169
- Method optionDescriptionMethod =
170
- option .getClass ().getMethod ("description" );
171
- String optionDescription =
172
- (String ) optionDescriptionMethod .invoke (option );
173
-
174
- Method optionTypeMethod = option .getClass ().getMethod ("type" );
175
- Enum <?> optionType = (Enum <?>) optionTypeMethod .invoke (option );
176
- String optionTypeValue = optionType .name ();
177
-
178
- Method optionRequiredMethod = option .getClass ().getMethod ("required" );
179
- boolean optionRequired = (boolean ) optionRequiredMethod .invoke (option );
180
-
181
- List <CommandOptionChoice > choices = new ArrayList <>();
182
-
183
- Object [] choicesArray =
184
- (Object []) option .getClass ().getMethod ("choices" ).invoke (option );
185
-
186
- for (Object choice : choicesArray ) {
187
- Annotation annotation1 = (Annotation ) choice ;
188
- if (
189
- annotation1 .annotationType ().getName ().equals (
190
- "com.javadiscord.jdi.core.annotations.CommandOptionChoice"
191
- )
192
- ) {
193
- Method nameMethod1 =
194
- annotation1 .annotationType ().getMethod ("name" );
195
- Method valueMethod1 =
196
- annotation1 .annotationType ().getMethod ("value" );
197
- String name1 = (String ) nameMethod1 .invoke (annotation1 );
198
- String value1 = (String ) valueMethod1 .invoke (annotation1 );
199
- choices .add (new CommandOptionChoice (value1 , name1 ));
200
- }
201
- }
202
-
203
- builder .addOption (
204
- new CommandOption (
205
- optionName ,
206
- optionDescription ,
207
- CommandOptionType .fromName (optionTypeValue ),
208
- optionRequired
209
- ).addChoice (choices )
210
- );
211
- }
212
-
151
+ CommandBuilder builder = buildCommand (annotation );
213
152
createInteractionRequests .add (builder );
214
153
}
215
154
}
@@ -219,6 +158,74 @@ private void registerLoadedAnnotationsWithDiscord() {
219
158
});
220
159
}
221
160
161
+ private CommandBuilder buildCommand (Annotation annotation ) throws ReflectiveOperationException {
162
+ Method nameMethod = annotation .annotationType ().getMethod ("name" );
163
+ String name = (String ) nameMethod .invoke (annotation );
164
+
165
+ Method descriptionMethod = annotation .annotationType ().getMethod ("description" );
166
+ String description = (String ) descriptionMethod .invoke (annotation );
167
+
168
+ Method optionsMethod = annotation .annotationType ().getMethod ("options" );
169
+ Object [] options = (Object []) optionsMethod .invoke (annotation );
170
+
171
+ CommandBuilder builder = new CommandBuilder (name , description );
172
+ for (Object option : options ) {
173
+ addCommandOption (builder , option );
174
+ }
175
+
176
+ return builder ;
177
+ }
178
+
179
+ private void addCommandOption (
180
+ CommandBuilder builder ,
181
+ Object option
182
+ ) throws ReflectiveOperationException {
183
+ Method optionNameMethod = option .getClass ().getMethod ("name" );
184
+ String optionName = (String ) optionNameMethod .invoke (option );
185
+
186
+ Method optionDescriptionMethod = option .getClass ().getMethod ("description" );
187
+ String optionDescription = (String ) optionDescriptionMethod .invoke (option );
188
+
189
+ Method optionTypeMethod = option .getClass ().getMethod ("type" );
190
+ Enum <?> optionType = (Enum <?>) optionTypeMethod .invoke (option );
191
+ String optionTypeValue = optionType .name ();
192
+
193
+ Method optionRequiredMethod = option .getClass ().getMethod ("required" );
194
+ boolean optionRequired = (boolean ) optionRequiredMethod .invoke (option );
195
+
196
+ List <CommandOptionChoice > choices = new ArrayList <>();
197
+ Object [] choicesArray = (Object []) option .getClass ().getMethod ("choices" ).invoke (option );
198
+ for (Object choice : choicesArray ) {
199
+ addCommandOptionChoice (choices , choice );
200
+ }
201
+
202
+ builder .addOption (
203
+ new CommandOption (
204
+ optionName ,
205
+ optionDescription ,
206
+ CommandOptionType .fromName (optionTypeValue ),
207
+ optionRequired
208
+ ).addChoice (choices )
209
+ );
210
+ }
211
+
212
+ private void addCommandOptionChoice (
213
+ List <CommandOptionChoice > choices ,
214
+ Object choice
215
+ ) throws ReflectiveOperationException {
216
+ Annotation annotation1 = (Annotation ) choice ;
217
+ if (
218
+ annotation1 .annotationType ().getName ()
219
+ .equals ("com.javadiscord.jdi.core.annotations.CommandOptionChoice" )
220
+ ) {
221
+ Method nameMethod1 = annotation1 .annotationType ().getMethod ("name" );
222
+ Method valueMethod1 = annotation1 .annotationType ().getMethod ("value" );
223
+ String name1 = (String ) nameMethod1 .invoke (annotation1 );
224
+ String value1 = (String ) valueMethod1 .invoke (annotation1 );
225
+ choices .add (new CommandOptionChoice (value1 , name1 ));
226
+ }
227
+ }
228
+
222
229
private boolean annotationLibPresent () {
223
230
try {
224
231
Class .forName ("com.javadiscord.jdi.core.processor.loader.ListenerLoader" );
@@ -237,7 +244,7 @@ private void loadComponents() {
237
244
constructor .newInstance ();
238
245
}
239
246
} catch (Exception | Error e ) {
240
- /* Ignore */
247
+ LOGGER . warn ( "Component loading failed" , e );
241
248
}
242
249
}
243
250
@@ -256,7 +263,7 @@ private void loadAnnotations() {
256
263
}
257
264
}
258
265
} catch (Exception | Error e ) {
259
- /* Ignore */
266
+ LOGGER . warn ( "Event listener loading failed" , e );
260
267
}
261
268
}
262
269
@@ -277,7 +284,6 @@ private void loadSlashCommands() {
277
284
);
278
285
return ;
279
286
}
280
- return ;
281
287
}
282
288
}
283
289
} catch (Exception | Error e ) {
@@ -288,15 +294,8 @@ private void loadSlashCommands() {
288
294
public void start () {
289
295
started = true ;
290
296
291
- webSocketManager =
292
- new WebSocketManager (
293
- new GatewaySetting ().setApiVersion (10 ).setEncoding (GatewayEncoding .JSON ),
294
- identifyRequest ,
295
- cache
296
- );
297
-
298
- WebSocketManagerProxy webSocketManagerProxy =
299
- new WebSocketManagerProxy (webSocketManager );
297
+ webSocketManager = new WebSocketManager (gatewaySetting , identifyRequest , cache );
298
+ WebSocketManagerProxy webSocketManagerProxy = new WebSocketManagerProxy (webSocketManager );
300
299
ConnectionDetails connectionDetails =
301
300
new ConnectionDetails (gateway .url (), botToken , gatewaySetting );
302
301
ConnectionMediator connectionMediator =
@@ -315,26 +314,20 @@ public void stop() {
315
314
316
315
LOGGER .info ("Shutdown initiated" );
317
316
318
- if (webSocketManager != null ) {
319
- webSocketManager .stop ();
320
- }
321
-
322
317
discordRequestDispatcher .stop ();
323
-
324
318
EXECUTOR .shutdown ();
325
319
326
320
try {
327
321
if (!EXECUTOR .awaitTermination (30 , TimeUnit .SECONDS )) {
328
322
EXECUTOR .shutdownNow ();
329
323
if (!EXECUTOR .awaitTermination (30 , TimeUnit .SECONDS )) {
330
324
LOGGER .warn (
331
- "Executor failed to shutdown within the specified time limit, some"
332
- + " tasks may still be running"
325
+ "Executor failed to shutdown within the specified time limit, some tasks may still be running"
333
326
);
334
327
}
335
328
}
336
329
} catch (InterruptedException e ) {
337
- LOGGER .error ("Termination was interrupted within {} seconds" , 30 , e );
330
+ LOGGER .error ("Termination was interrupted" , e );
338
331
Thread .currentThread ().interrupt ();
339
332
}
340
333
}
@@ -376,16 +369,8 @@ private static Gateway getGatewayURL(String authentication) {
376
369
}
377
370
}
378
371
379
- public void registerSlashCommand (
380
- String name ,
381
- String description ,
382
- CommandOption ... options
383
- ) {
384
- CommandBuilder builder =
385
- new CommandBuilder (
386
- name ,
387
- description
388
- );
372
+ public void registerSlashCommand (String name , String description , CommandOption ... options ) {
373
+ CommandBuilder builder = new CommandBuilder (name , description );
389
374
for (CommandOption option : options ) {
390
375
builder .addOption (option );
391
376
}
@@ -399,7 +384,7 @@ public void registerSlashCommand(CommandBuilder builder) {
399
384
}
400
385
401
386
public void deleteSlashCommand (long id ) {
402
-
387
+ // Implement command deletion logic
403
388
}
404
389
405
390
public DiscordRequestDispatcher getDiscordRequestDispatcher () {
@@ -414,7 +399,7 @@ public List<Object> getAnnotatedEventListeners() {
414
399
return annotatedEventListeners ;
415
400
}
416
401
417
- public boolean started () {
402
+ public boolean isStarted () {
418
403
return started ;
419
404
}
420
405
@@ -428,29 +413,35 @@ public long getApplicationId() {
428
413
429
414
void handleReadyEvent (ReadyEvent event ) {
430
415
applicationId = event .application ().id ();
431
-
432
416
EXECUTOR .execute (discordRequestDispatcher );
433
417
434
418
for (CommandBuilder builder : createInteractionRequests ) {
435
419
builder .applicationId (applicationId );
436
420
CreateCommandRequest request = builder .build ();
437
421
DiscordResponseFuture future = sendRequest (request );
438
- future .onSuccess (res -> {
439
- if (res .status () >= 200 && res .status () < 300 ) {
440
- LOGGER .info ("Registered slash command {} with discord" , request .name ());
441
- } else {
442
- LOGGER .error (
443
- "Failed to register slash command {} with discord\n {}" , request .name (),
444
- res .body ()
445
- );
446
- }
447
- });
448
- future .onError (
449
- err -> LOGGER
450
- .error ("Failed to register slash command {} with discord" , request .name (), err )
451
- );
422
+ handleCommandRegistrationResponse (request , future );
452
423
}
453
424
454
425
createInteractionRequests .clear ();
455
426
}
427
+
428
+ private void handleCommandRegistrationResponse (
429
+ CreateCommandRequest request ,
430
+ DiscordResponseFuture future
431
+ ) {
432
+ future .onSuccess (res -> {
433
+ if (res .status () >= 200 && res .status () < 300 ) {
434
+ LOGGER .info ("Registered slash command {} with discord" , request .name ());
435
+ } else {
436
+ LOGGER .error (
437
+ "Failed to register slash command {} with discord\n {}" , request .name (),
438
+ res .body ()
439
+ );
440
+ }
441
+ });
442
+ future .onError (
443
+ err -> LOGGER
444
+ .error ("Failed to register slash command {} with discord" , request .name (), err )
445
+ );
446
+ }
456
447
}
0 commit comments