Skip to content

Commit

Permalink
🐛 enhanced slash manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed May 6, 2024
1 parent 15f8adc commit fe6fdb5
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void shutdownAll() {
MANAGERS.clear();
}

private final Multimap<String, Runnable> waitingGuildCommands = ArrayListMultimap.create();
private final Map<String, List<Runnable>> waitingGuildCommands = new HashMap<>();
private final Set<String> readyGuilds = new HashSet<>();

private final List<RegisteredCommand> registeredCommands;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void registerCommand(ParsedCommand command) {
if (readyGuilds.contains(guildId)) {
runnable.run();
} else {
waitingGuildCommands.put(guildId, runnable);
waitingGuildCommands.computeIfAbsent(guildId, k -> new ArrayList<>()).add(runnable);
DiSky.debug("Guild " + guildId + " is not ready yet, waiting for ready event to register command (now " + waitingGuildCommands.get(guildId).size() + " waiting)");
}
}
Expand Down Expand Up @@ -102,13 +102,17 @@ public void registerCommand(ParsedCommand command,

registeredCommands.add(registeredCommand);
DiSky.debug("{CREATE} Registered command " + command.getName() + " on guild " + guildId + " for bot " + bot.getName());
}, ex -> {
DiSky.debug("{CREATE} Failed to register command " + command.getName() + " on guild " + guildId + " for bot " + bot.getName());
DiSky.getErrorHandler().exception(null, ex);
});
}

public void updateCommand(ParsedCommand command,
RegisteredCommand registeredCommand,
Bot bot, String guildId) {
// We first must check if they were any changes in the command itself
registeredCommand.setTrigger(command.getTrigger()); // we update the trigger anyway
if (!registeredCommand.shouldUpdate(command))
{
DiSky.debug("{UPDATE} No changes detected for command " + command.getName() + " on guild " + guildId + " for bot " + bot.getName());
Expand All @@ -121,6 +125,12 @@ public void updateCommand(ParsedCommand command,
throw new IllegalStateException("Command ID changed after update! (this should never happens)");

DiSky.debug("{UPDATE} Updated command " + command.getName() + " on guild " + guildId + " for bot " + bot.getName());
}, ex -> {
DiSky.debug("{UPDATE} Failed to update command " + command.getName() + " on guild " + guildId + " for bot " + bot.getName());
DiSky.debug("We'll register it again instead");
DiSky.getErrorHandler().exception(null, ex);

registerCommand(command, bot, guildId);
});
}

Expand Down Expand Up @@ -203,12 +213,15 @@ public void onGuildReady(GuildReadyEvent event) {
if (readyGuilds.contains(guildId)) {
DiSky.debug("Guild " + guildId + " is already ready, skipping command registration");
return;
} else if (!waitingGuildCommands.containsKey(guildId)) {
DiSky.debug("Guild " + guildId + " is ready, but no command to register");
return;
}

readyGuilds.add(guildId);
final Collection<Runnable> runnables = waitingGuildCommands.get(guildId);
DiSky.debug("Guild " + guildId + " is ready, registering commands (" + runnables.size() + ")");
runnables.forEach(Runnable::run);
waitingGuildCommands.removeAll(guildId);
waitingGuildCommands.remove(guildId);
}
}

0 comments on commit fe6fdb5

Please sign in to comment.