Skip to content

Commit 4e96c08

Browse files
committed
Delete command request
1 parent c038ef7 commit 4e96c08

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package com.javadiscord.jdi.internal.api.application_commands;
22

3-
import java.util.Optional;
4-
53
import com.javadiscord.jdi.internal.api.DiscordRequest;
64
import com.javadiscord.jdi.internal.api.DiscordRequestBuilder;
75

86
public record DeleteCommandRequest(
97
long applicationId,
10-
Optional<Long> guildId,
11-
long commandId
8+
long guildId,
9+
long commandId,
10+
boolean global
1211
) implements DiscordRequest {
1312

1413
@Override
1514
public DiscordRequestBuilder create() {
16-
// TODO: Implement
17-
// https://discord.com/developers/docs/interactions/application-commands#updating-and-deleting-a-command
18-
return null;
15+
String path;
16+
17+
if (global) {
18+
path = "applications/%s/commands/%s".formatted(applicationId, commandId);
19+
} else {
20+
path =
21+
"applications/%s/guilds/%s/commands/%s"
22+
.formatted(applicationId, guildId, commandId);
23+
}
24+
25+
return new DiscordRequestBuilder()
26+
.delete()
27+
.path(path);
1928
}
2029
}

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.javadiscord.jdi.internal.api.DiscordRequestDispatcher;
2828
import com.javadiscord.jdi.internal.api.DiscordResponseFuture;
2929
import com.javadiscord.jdi.internal.api.application_commands.CreateCommandRequest;
30+
import com.javadiscord.jdi.internal.api.application_commands.DeleteCommandRequest;
3031
import com.javadiscord.jdi.internal.cache.Cache;
3132
import com.javadiscord.jdi.internal.cache.CacheType;
3233
import com.javadiscord.jdi.internal.gateway.*;
@@ -60,6 +61,7 @@ public class Discord {
6061
private final Map<String, Object> loadedSlashCommands = new HashMap<>();
6162
private final List<EventListener> eventListeners = new ArrayList<>();
6263
private final List<CommandBuilder> createInteractionRequests = new ArrayList<>();
64+
private final List<DeleteCommandRequest> deleteInteractionRequests = new ArrayList<>();
6365

6466
private WebSocketManager webSocketManager;
6567
private long applicationId;
@@ -381,8 +383,9 @@ public void registerSlashCommand(CommandBuilder builder) {
381383
createInteractionRequests.add(builder);
382384
}
383385

384-
public void deleteSlashCommand(long id) {
385-
// Implement command deletion logic
386+
public void deleteSlashCommand(long commandId, long guildId, boolean global) {
387+
deleteInteractionRequests
388+
.add(new DeleteCommandRequest(applicationId, guildId, commandId, global));
386389
}
387390

388391
public DiscordRequestDispatcher getDiscordRequestDispatcher() {
@@ -420,7 +423,30 @@ void handleReadyEvent(ReadyEvent event) {
420423
handleCommandRegistrationResponse(request, future);
421424
}
422425

426+
for (DeleteCommandRequest request : deleteInteractionRequests) {
427+
DiscordResponseFuture future = sendRequest(request);
428+
handleDeleteResponse(request, future);
429+
}
430+
423431
createInteractionRequests.clear();
432+
deleteInteractionRequests.clear();
433+
}
434+
435+
private void handleDeleteResponse(DeleteCommandRequest request, DiscordResponseFuture future) {
436+
future.onSuccess(res -> {
437+
if (res.status() >= 200 && res.status() < 300) {
438+
LOGGER.info("Deleted slash command {} with discord", request.commandId());
439+
} else {
440+
LOGGER.error(
441+
"Failed to delete slash command {} with discord\n{}", request.commandId(),
442+
res.body()
443+
);
444+
}
445+
});
446+
future.onError(
447+
err -> LOGGER
448+
.error("Failed to delete slash command {} with discord", request.commandId(), err)
449+
);
424450
}
425451

426452
private void handleCommandRegistrationResponse(

0 commit comments

Comments
 (0)