Skip to content

Commit b58f1d0

Browse files
committed
Change command upserting to use updateCommands
1 parent 4a9284e commit b58f1d0

File tree

2 files changed

+32
-83
lines changed

2 files changed

+32
-83
lines changed

src/commander/java/com/mcmoddev/mmdbot/commander/TheCommander.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import io.github.cdimascio.dotenv.Dotenv;
8484
import io.github.matyrobbrt.curseforgeapi.CurseForgeAPI;
8585
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
86+
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
8687
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
8788
import net.dv8tion.jda.api.JDA;
8889
import net.dv8tion.jda.api.JDABuilder;
@@ -267,7 +268,7 @@ public static ComponentListener.Builder getComponentListener(final String featur
267268
private Configuration generalConfig;
268269
private final Dotenv dotenv;
269270
private final Path runPath;
270-
private final Long2ObjectMap<GuildConfiguration> guildConfigs = new Long2ObjectOpenHashMap<>();
271+
private final Long2ObjectMap<GuildConfiguration> guildConfigs = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
271272

272273
private final String githubToken;
273274

@@ -569,18 +570,20 @@ public String getGithubToken() {
569570
}
570571

571572
public GuildConfiguration getConfigForGuild(long guildId) {
572-
return guildConfigs.computeIfAbsent(guildId, rethrowFunction(id -> {
573-
final var path = runPath.resolve("configs").resolve("guild").resolve(guildId + ".conf");
574-
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
575-
.emitComments(true)
576-
.prettyPrinting(true)
577-
.defaultOptions(ConfigurationOptions.defaults().serializers(ADDED_SERIALIZERS))
578-
.path(path)
579-
.build();
580-
return ConfigurateUtils.loadConfig(loader, path, cfg -> guildConfigs.put(guildId, cfg), GuildConfiguration.class, GuildConfiguration.EMPTY)
581-
.value()
582-
.get();
583-
}));
573+
synchronized (guildConfigs) {
574+
return guildConfigs.computeIfAbsent(guildId, rethrowFunction(id -> {
575+
final var path = runPath.resolve("configs").resolve("guild").resolve(guildId + ".conf");
576+
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
577+
.emitComments(true)
578+
.prettyPrinting(true)
579+
.defaultOptions(ConfigurationOptions.defaults().serializers(ADDED_SERIALIZERS))
580+
.path(path)
581+
.build();
582+
return ConfigurateUtils.loadConfig(loader, path, cfg -> guildConfigs.put(guildId, cfg), GuildConfiguration.class, GuildConfiguration.EMPTY)
583+
.value()
584+
.get();
585+
}));
586+
}
584587
}
585588

586589
public GuildConfiguration getConfigForGuild(Guild guild) {

src/core/java/com/mcmoddev/mmdbot/core/commands/CommandUpserter.java

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@
3333
import net.dv8tion.jda.api.events.ReadyEvent;
3434
import net.dv8tion.jda.api.exceptions.ErrorHandler;
3535
import net.dv8tion.jda.api.hooks.EventListener;
36-
import net.dv8tion.jda.api.interactions.commands.Command;
3736
import net.dv8tion.jda.api.requests.ErrorResponse;
38-
import net.dv8tion.jda.api.requests.RestAction;
3937
import org.jetbrains.annotations.Nullable;
4038
import org.slf4j.Logger;
4139
import org.slf4j.LoggerFactory;
4240

4341
import java.util.Collection;
4442
import java.util.List;
4543
import java.util.Objects;
46-
import java.util.stream.Collectors;
47-
import java.util.stream.LongStream;
44+
import java.util.stream.Stream;
4845

4946
/**
5047
* Utility class for upserting commands. <br>
@@ -110,68 +107,34 @@ public void upsertCommands(@NonNull final JDA jda) {
110107
if (forceGuild) {
111108
final var guild = Objects.requireNonNull(guildId).resolve(jda::getGuildById);
112109
if (guild == null) throw new NullPointerException("Unknown guild with ID: " + guildId);
113-
guild.retrieveCommands().queue(commands -> {
114-
// Delete old commands.
115-
final var toRemove = getCommandsToRemove(commands);
116-
if (toRemove.length > 0) {
117-
RestAction.allOf(LongStream.of(toRemove).mapToObj(guild::deleteCommandById).toList()).queue();
118-
}
119-
120-
// Upsert new ones
121-
RestAction.allOf(client.getSlashCommands().stream()
122-
.map(SlashCommand::buildCommandData)
123-
.map(guild::upsertCommand)
124-
.collect(Collectors.toSet()))
125-
.queue(cmds -> LOG.info("Registered {} commands to guild '{}' ({})", cmds.size(), guild.getName(), guild.getId()), createErrorHandler(guild));
126110

127-
if (!client.getContextMenus().isEmpty()) {
128-
// Upsert menus
129-
RestAction.allOf(client.getContextMenus().stream()
130-
.map(ContextMenu::buildCommandData)
131-
.map(guild::upsertCommand)
132-
.collect(Collectors.toSet()))
133-
.queue(cmds -> LOG.info("Registered {} context menus to guild '{}' ({})", cmds.size(), guild.getName(), guild.getId()), createErrorHandler(guild));
134-
}
135-
});
111+
// Update the guild commands
112+
guild.updateCommands()
113+
.addCommands(Stream.concat(
114+
client.getSlashCommands().stream().map(SlashCommand::buildCommandData),
115+
client.getContextMenus().stream().map(ContextMenu::buildCommandData)
116+
).toList())
117+
.queue(it -> LOG.info("Registered {} commands to guild '{}' ({}).", it.size(), guild.getName(), guild.getId()), createErrorHandler(guild));
136118
} else {
137119
if (guildId != null) {
138120
final var guild = guildId.resolve(jda::getGuildById);
139121
if (guild != null) {
140122
// Guild still specified? Then remove guild commands
141123
guild.retrieveCommands().queue(commands -> {
142124
if (!commands.isEmpty()) {
143-
RestAction.allOf(commands.stream()
144-
.map(Command::getIdLong)
145-
.map(guild::deleteCommandById)
146-
.toList()).queue();
125+
guild.updateCommands().queue();
147126
}
148127
});
149128
}
150129
}
151-
jda.retrieveCommands().queue(commands -> {
152-
// Delete old commands.
153-
final var toRemove = getCommandsToRemove(commands);
154-
if (toRemove.length > 1) {
155-
RestAction.allOf(LongStream.of(toRemove).mapToObj(jda::deleteCommandById).toList()).queue();
156-
}
157-
158-
// Upsert new ones
159-
RestAction.allOf(client.getSlashCommands()
160-
.stream()
161-
.map(SlashCommand::buildCommandData)
162-
.map(jda::upsertCommand)
163-
.collect(Collectors.toSet()))
164-
.queue(cmds -> LOG.info("Registered {} global commands.", cmds.size()));
165130

166-
if (!client.getContextMenus().isEmpty()) {
167-
// Upsert menus
168-
RestAction.allOf(client.getContextMenus().stream()
169-
.map(ContextMenu::buildCommandData)
170-
.map(jda::upsertCommand)
171-
.collect(Collectors.toSet()))
172-
.queue(cmds -> LOG.info("Registered {} context menus.", cmds.size()));
173-
}
174-
});
131+
// Update the global commands
132+
jda.updateCommands()
133+
.addCommands(Stream.concat(
134+
client.getSlashCommands().stream().map(SlashCommand::buildCommandData),
135+
client.getContextMenus().stream().map(ContextMenu::buildCommandData)
136+
).toList())
137+
.queue(it -> LOG.info("Registered {} global commands.", it.size()));
175138
}
176139
}
177140

@@ -201,21 +164,4 @@ public ErrorHandler createErrorHandler(final Guild guild) {
201164
});
202165
}
203166

204-
private long[] getCommandsToRemove(final List<Command> existingCommands) {
205-
record ExistingCommand(String name, long id) {
206-
}
207-
final var ext = existingCommands.stream()
208-
.filter(c -> c.getType() == Command.Type.SLASH)
209-
.map(c -> new ExistingCommand(c.getName(), c.getIdLong()))
210-
.collect(Collectors.toSet());
211-
final var clientCommandNames = client.getSlashCommands().stream().map(SlashCommand::getName).collect(Collectors.toSet());
212-
ext.removeIf(p -> {
213-
final var contains = clientCommandNames.contains(p.name());
214-
if (contains) {
215-
clientCommandNames.remove(p.name());
216-
}
217-
return contains;
218-
});
219-
return ext.stream().mapToLong(ExistingCommand::id).toArray();
220-
}
221167
}

0 commit comments

Comments
 (0)