Skip to content

Commit 2946623

Browse files
committed
SlashCommands Fixed
v1.0.4
1 parent ea85452 commit 2946623

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>i.fran2019.BotMaster</groupId>
88
<artifactId>BotMasterDC</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.0.4</version>
1010
<repositories>
1111
<repository>
1212
<id>central</id>

src/main/java/i/fran2019/BotMaster/Managers/CommandManager.java

+59-45
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ public CommandManager() {
3434

3535
@SuppressWarnings("unused")
3636
public void registerCommand(Command cmd) {
37+
if (started) {
38+
BotMaster.getLogger().error("Can't register a new command.");
39+
return;
40+
}
41+
3742
if (this.commands.get(cmd.getName().toLowerCase()) == null) {
3843
this.commands.put(cmd.getName().toLowerCase(), cmd);
3944
addCommandData(cmd);
4045
}
4146
else BotMaster.getLogger().warn("The command cannot be registered correctly because it already exists.");
4247

43-
if (started) registerSlashCommands();
4448
}
4549

4650
protected void registerSlashCommands() {
@@ -50,67 +54,77 @@ protected void registerSlashCommands() {
5054
BotMaster.getLogger().info("Loading commands. (Global) (takes 1 hour to update)");
5155

5256
BotMaster.getBotMaster().getJda().retrieveCommands().queue(existingCommands -> {
53-
List<Long> commandsToDelete = existingCommands.stream()
54-
.filter(existingCommand -> commandsData.stream().anyMatch(commandData -> commandData.getName().equals(existingCommand.getName())))
55-
.map(ISnowflake::getIdLong)
56-
.toList();
57-
58-
if (!commandsToDelete.isEmpty()) {
59-
CompletableFuture<Void> deletionFuture = CompletableFuture.allOf(
60-
commandsToDelete.stream()
61-
.map(commandId -> CompletableFuture.runAsync(() ->
62-
BotMaster.getBotMaster().getJda().deleteCommandById(commandId).queue()))
63-
.toArray(CompletableFuture[]::new)
64-
);
65-
66-
deletionFuture.thenRun(() ->
67-
BotMaster.getBotMaster().getJda().updateCommands().addCommands(this.commandsData).queue(
68-
success -> BotMaster.getLogger().info("Global commands registered successfully."),
69-
failure -> BotMaster.getLogger().error("Failed to register global commands.", failure)
70-
)
71-
);
72-
} else {
73-
BotMaster.getBotMaster().getJda().updateCommands().addCommands(this.commandsData).queue(
74-
success -> BotMaster.getLogger().info("Global commands registered successfully."),
75-
failure -> BotMaster.getLogger().error("Failed to register global commands.", failure)
76-
);
77-
}
78-
});
79-
80-
} else {
81-
BotMaster.getLogger().info("Loading commands on {} guilds. (Local)", BotMaster.getBotMaster().getJda().getGuilds().size());
82-
83-
if (BotMaster.getBotMaster().getJda().getGuilds().size() >= 50) {
84-
BotMaster.getLogger().warn("You could consider starting to use \"Global\" commands to avoid a high performance load when starting the bot.");
85-
}
86-
87-
for (Guild guild : BotMaster.getBotMaster().getJda().getGuilds()) {
88-
guild.retrieveCommands().queue(existingCommands -> {
57+
if (existingCommands != null && commandsData != null) {
8958
List<Long> commandsToDelete = existingCommands.stream()
90-
.filter(existingCommand -> commandsData.stream().anyMatch(commandData -> commandData.getName().equals(existingCommand.getName())))
59+
.filter(existingCommand -> commandsData.stream().noneMatch(commandData -> commandData.getName().equals(existingCommand.getName())))
9160
.map(ISnowflake::getIdLong)
9261
.toList();
9362

9463
if (!commandsToDelete.isEmpty()) {
9564
CompletableFuture<Void> deletionFuture = CompletableFuture.allOf(
9665
commandsToDelete.stream()
9766
.map(commandId -> CompletableFuture.runAsync(() ->
98-
guild.deleteCommandById(commandId).queue()))
67+
BotMaster.getBotMaster().getJda().deleteCommandById(commandId).queue(
68+
success -> {},
69+
throwable -> {}
70+
)))
9971
.toArray(CompletableFuture[]::new)
10072
);
10173

10274
deletionFuture.thenRun(() ->
103-
guild.updateCommands().addCommands(this.commandsData).queue(
104-
success -> BotMaster.getLogger().info("Commands registered successfully on guild: {}", guild.getName()),
105-
failure -> BotMaster.getLogger().error("Failed to register commands on guild: {}", guild.getName(), failure)
75+
BotMaster.getBotMaster().getJda().updateCommands().addCommands(this.commandsData).queue(
76+
success -> BotMaster.getLogger().info("Global commands registered successfully."),
77+
failure -> BotMaster.getLogger().error("Failed to register global commands.", failure)
10678
)
10779
);
10880
} else {
109-
guild.updateCommands().addCommands(this.commandsData).queue(
110-
success -> BotMaster.getLogger().info("Commands registered successfully on guild: {}", guild.getName()),
111-
failure -> BotMaster.getLogger().error("Failed to register commands on guild: {}", guild.getName(), failure)
81+
BotMaster.getBotMaster().getJda().updateCommands().addCommands(this.commandsData).queue(
82+
success -> BotMaster.getLogger().info("Global commands registered successfully."),
83+
failure -> BotMaster.getLogger().error("Failed to register global commands.", failure)
11284
);
11385
}
86+
}
87+
});
88+
89+
} else {
90+
BotMaster.getLogger().info("Loading commands on {} guilds. (Local)", BotMaster.getBotMaster().getJda().getGuilds().size());
91+
92+
if (BotMaster.getBotMaster().getJda().getGuilds().size() >= 50) {
93+
BotMaster.getLogger().warn("You could consider starting to use \"Global\" commands to avoid a high performance load when starting the bot.");
94+
}
95+
96+
for (Guild guild : BotMaster.getBotMaster().getJda().getGuilds()) {
97+
guild.retrieveCommands().queue(existingCommands -> {
98+
if (existingCommands != null && commandsData != null) {
99+
List<Long> commandsToDelete = existingCommands.stream()
100+
.filter(existingCommand -> commandsData.stream().noneMatch(commandData -> commandData.getName().equals(existingCommand.getName())))
101+
.map(ISnowflake::getIdLong)
102+
.toList();
103+
104+
if (!commandsToDelete.isEmpty()) {
105+
CompletableFuture<Void> deletionFuture = CompletableFuture.allOf(
106+
commandsToDelete.stream()
107+
.map(commandId -> CompletableFuture.runAsync(() ->
108+
guild.deleteCommandById(commandId).queue(
109+
success -> {},
110+
throwable -> {}
111+
)))
112+
.toArray(CompletableFuture[]::new)
113+
);
114+
115+
deletionFuture.thenRun(() ->
116+
guild.updateCommands().addCommands(this.commandsData).queue(
117+
success -> BotMaster.getLogger().info("Commands registered successfully on guild: {}", guild.getName()),
118+
failure -> BotMaster.getLogger().error("Failed to register commands on guild: {}", guild.getName(), failure)
119+
)
120+
);
121+
} else {
122+
guild.updateCommands().addCommands(this.commandsData).queue(
123+
success -> BotMaster.getLogger().info("Commands registered successfully on guild: {}", guild.getName()),
124+
failure -> BotMaster.getLogger().error("Failed to register commands on guild: {}", guild.getName(), failure)
125+
);
126+
}
127+
}
114128
});
115129
}
116130
}

0 commit comments

Comments
 (0)