Skip to content

Commit 84c101e

Browse files
committed
chore: readability refactoring
1 parent 93b8b92 commit 84c101e

File tree

2 files changed

+106
-114
lines changed

2 files changed

+106
-114
lines changed

annotations/src/main/java/com/javadiscord/jdi/core/processor/validator/SlashCommandValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class SlashCommandValidator {
2828
}
2929

3030
public boolean validate(Method method) {
31-
for (Map.Entry<Class<? extends Annotation>, String[]> entry : EXPECTED_PARAM_TYPES_MAP.entrySet()) {
31+
for (Map.Entry<Class<? extends Annotation>, String[]> entry : EXPECTED_PARAM_TYPES_MAP
32+
.entrySet()) {
3233
Class<? extends Annotation> annotationClass = entry.getKey();
3334
if (method.isAnnotationPresent(annotationClass)) {
3435
if (!validateMethodParameters(method, entry.getValue())) {

core/src/main/java/com/javadiscord/jdi/core/Discord.java

Lines changed: 104 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,11 @@ public Discord(String botToken, IdentifyRequest identifyRequest) {
110110

111111
public Discord(String botToken, IdentifyRequest identifyRequest, Cache cache) {
112112
System.err.println("""
113-
_ ____ ___\s
113+
_ ____ ___
114114
| | _ \\_ _| 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
117117
\\___/|____/___| Version 1.0
118-
119118
""");
120119

121120
this.botToken = botToken;
@@ -140,8 +139,7 @@ private void registerLoadedAnnotationsWithDiscord() {
140139
try {
141140
Class<?> slashCommandClassInstanceClass = slashCommandClassInstance.getClass();
142141
Method method =
143-
(Method) slashCommandClassInstanceClass
144-
.getMethod("method")
142+
(Method) slashCommandClassInstanceClass.getMethod("method")
145143
.invoke(slashCommandClassInstance);
146144

147145
Annotation[] annotations = method.getAnnotations();
@@ -150,66 +148,7 @@ private void registerLoadedAnnotationsWithDiscord() {
150148
annotation.annotationType().getName()
151149
.equals("com.javadiscord.jdi.core.annotations.SlashCommand")
152150
) {
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);
213152
createInteractionRequests.add(builder);
214153
}
215154
}
@@ -219,6 +158,74 @@ private void registerLoadedAnnotationsWithDiscord() {
219158
});
220159
}
221160

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+
222229
private boolean annotationLibPresent() {
223230
try {
224231
Class.forName("com.javadiscord.jdi.core.processor.loader.ListenerLoader");
@@ -237,7 +244,7 @@ private void loadComponents() {
237244
constructor.newInstance();
238245
}
239246
} catch (Exception | Error e) {
240-
/* Ignore */
247+
LOGGER.warn("Component loading failed", e);
241248
}
242249
}
243250

@@ -256,7 +263,7 @@ private void loadAnnotations() {
256263
}
257264
}
258265
} catch (Exception | Error e) {
259-
/* Ignore */
266+
LOGGER.warn("Event listener loading failed", e);
260267
}
261268
}
262269

@@ -277,7 +284,6 @@ private void loadSlashCommands() {
277284
);
278285
return;
279286
}
280-
return;
281287
}
282288
}
283289
} catch (Exception | Error e) {
@@ -288,15 +294,8 @@ private void loadSlashCommands() {
288294
public void start() {
289295
started = true;
290296

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);
300299
ConnectionDetails connectionDetails =
301300
new ConnectionDetails(gateway.url(), botToken, gatewaySetting);
302301
ConnectionMediator connectionMediator =
@@ -315,26 +314,20 @@ public void stop() {
315314

316315
LOGGER.info("Shutdown initiated");
317316

318-
if (webSocketManager != null) {
319-
webSocketManager.stop();
320-
}
321-
322317
discordRequestDispatcher.stop();
323-
324318
EXECUTOR.shutdown();
325319

326320
try {
327321
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
328322
EXECUTOR.shutdownNow();
329323
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
330324
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"
333326
);
334327
}
335328
}
336329
} catch (InterruptedException e) {
337-
LOGGER.error("Termination was interrupted within {} seconds", 30, e);
330+
LOGGER.error("Termination was interrupted", e);
338331
Thread.currentThread().interrupt();
339332
}
340333
}
@@ -376,16 +369,8 @@ private static Gateway getGatewayURL(String authentication) {
376369
}
377370
}
378371

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);
389374
for (CommandOption option : options) {
390375
builder.addOption(option);
391376
}
@@ -399,7 +384,7 @@ public void registerSlashCommand(CommandBuilder builder) {
399384
}
400385

401386
public void deleteSlashCommand(long id) {
402-
387+
// Implement command deletion logic
403388
}
404389

405390
public DiscordRequestDispatcher getDiscordRequestDispatcher() {
@@ -414,7 +399,7 @@ public List<Object> getAnnotatedEventListeners() {
414399
return annotatedEventListeners;
415400
}
416401

417-
public boolean started() {
402+
public boolean isStarted() {
418403
return started;
419404
}
420405

@@ -428,29 +413,35 @@ public long getApplicationId() {
428413

429414
void handleReadyEvent(ReadyEvent event) {
430415
applicationId = event.application().id();
431-
432416
EXECUTOR.execute(discordRequestDispatcher);
433417

434418
for (CommandBuilder builder : createInteractionRequests) {
435419
builder.applicationId(applicationId);
436420
CreateCommandRequest request = builder.build();
437421
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);
452423
}
453424

454425
createInteractionRequests.clear();
455426
}
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+
}
456447
}

0 commit comments

Comments
 (0)