Skip to content

Commit

Permalink
Make NexusSynchronizer methods customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Apr 9, 2022
1 parent 158327e commit f8942c4
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package pw.mihou.nexus.features.command.synchronizer;

import org.javacord.api.entity.server.Server;
import org.javacord.api.interaction.SlashCommand;
import org.javacord.api.interaction.SlashCommandBuilder;
import pw.mihou.nexus.Nexus;
import pw.mihou.nexus.core.NexusCore;
import pw.mihou.nexus.core.enginex.facade.NexusEngineX;
import pw.mihou.nexus.core.managers.facade.NexusCommandManager;
import pw.mihou.nexus.features.command.facade.NexusCommand;
import pw.mihou.nexus.features.command.synchronizer.overwrites.NexusSynchronizeMethods;
import pw.mihou.nexus.features.command.synchronizer.overwrites.defaults.NexusDefaultSynchronizeMethods;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;

public record NexusSynchronizer(
Nexus nexus
) {

public static final AtomicReference<NexusSynchronizeMethods> SYNCHRONIZE_METHODS = new AtomicReference<>(new NexusDefaultSynchronizeMethods());

/**
* Deletes a command to a specific server.
*
Expand All @@ -33,39 +37,10 @@ public CompletableFuture<Void> delete(NexusCommand command, int totalShards, lon
serverMappedFutures.put(serverId, new CompletableFuture<>());
}

engineX.queue((int) ((serverId >> 22) % totalShards), (api, store) -> {
if (api.getServerById(serverId).isEmpty()) {
NexusCore.logger.error(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? [shard={};id={}]",
api.getCurrentShard(),
serverId
);
serverMappedFutures.get(serverId).completeExceptionally(
new IllegalStateException(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? " +
"[shard=" + api.getCurrentShard() + ";id=" + serverId + "]"
)
);
return;
}

Server server = api.getServerById(serverId).orElseThrow();
server.getSlashCommands()
.join()
.stream()
.filter(slashCommand -> slashCommand.getName().equalsIgnoreCase(command.getName()))
.findFirst()
.ifPresent(slashCommand -> slashCommand.deleteForServer(server).thenAccept(unused -> {
NexusCore.logger.debug("A command has completed deletion. [server={}, command={}]", serverId, slashCommand.getName());
serverMappedFutures.get(serverId).complete(null);
}).exceptionally(throwable -> {
if (throwable != null) {
serverMappedFutures.get(serverId).completeExceptionally(throwable);
}

return null;
}));
});
engineX.queue(
(int) ((serverId >> 22) % totalShards),
(api, store) -> SYNCHRONIZE_METHODS.get().deleteForServer(api, command, serverId, serverMappedFutures.get(serverId))
);
}

return CompletableFuture.allOf(serverMappedFutures.values().toArray(new CompletableFuture[0]));
Expand All @@ -92,36 +67,10 @@ public CompletableFuture<Void> batchUpdate(long serverId, int totalShards) {
NexusEngineX engineX = ((NexusCore) nexus).getEngineX();
List<SlashCommandBuilder> slashCommandBuilders = new ArrayList<>();
serverCommands.forEach(command -> slashCommandBuilders.add(command.asSlashCommand()));
engineX.queue((int) ((serverId >> 22) % totalShards), (api, store) -> {
if (api.getServerById(serverId).isEmpty()) {
NexusCore.logger.error(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? [shard={};id={}]",
api.getCurrentShard(),
serverId
);
future.completeExceptionally(
new IllegalStateException(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? " +
"[shard=" + api.getCurrentShard() + ";id=" + serverId + "]"
)
);
return;
}

Server server = api.getServerById(serverId).orElseThrow();
api.bulkOverwriteServerApplicationCommands(server, slashCommandBuilders)
.thenAccept(applicationCommands -> {
NexusCore.logger.debug("A server has completed synchronization. [server={}, size={}]", serverId, applicationCommands.size());
future.complete(null);
})
.exceptionally(throwable -> {
if (throwable != null) {
future.completeExceptionally(throwable);
}

return null;
});
});
engineX.queue(
(int) ((serverId >> 22) % totalShards),
(api, store) -> SYNCHRONIZE_METHODS.get().bulkOverwriteServer(api, slashCommandBuilders, serverId, future)
);

return future;
}
Expand All @@ -143,57 +92,10 @@ public CompletableFuture<Void> upsert(NexusCommand command, int totalShards, lon
serverMappedFutures.put(serverId, new CompletableFuture<>());
}

engineX.queue((int) ((serverId >> 22) % totalShards), (api, store) -> {
if (api.getServerById(serverId).isEmpty()) {
NexusCore.logger.error(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? [shard={};id={}]",
api.getCurrentShard(),
serverId
);
serverMappedFutures.get(serverId).completeExceptionally(
new IllegalStateException(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? " +
"[shard=" + api.getCurrentShard() + ";id=" + serverId + "]"
)
);
return;
}

Server server = api.getServerById(serverId).orElseThrow();
List<SlashCommand> commands = server.getSlashCommands().join();

Optional<SlashCommand> matchingCommand = commands.stream()
.filter(slashCommand -> slashCommand.getName().equalsIgnoreCase(command.getName()))
.findFirst();

if (matchingCommand.isPresent()) {
command.asSlashCommandUpdater(matchingCommand.get().getId()).updateForServer(server)
.thenAccept(slashCommand -> {
NexusCore.logger.debug("A command has completed synchronization. [server={}, command={}]", serverId, slashCommand.getName());
serverMappedFutures.get(serverId).complete(null);
})
.exceptionally(throwable -> {
if (throwable != null) {
serverMappedFutures.get(serverId).completeExceptionally(throwable);
}

return null;
});
} else {
command.asSlashCommand().createForServer(server)
.thenAccept(slashCommand -> {
NexusCore.logger.debug("A command has completed synchronization. [server={}, command={}]", serverId, slashCommand.getName());
serverMappedFutures.get(serverId).complete(null);
})
.exceptionally(throwable -> {
if (throwable != null) {
serverMappedFutures.get(serverId).completeExceptionally(throwable);
}

return null;
});
}
});
engineX.queue(
(int) ((serverId >> 22) % totalShards),
(api, store) -> SYNCHRONIZE_METHODS.get().updateForServer(api, command, serverId, serverMappedFutures.get(serverId))
);
}

return CompletableFuture.allOf(serverMappedFutures.values().toArray(new CompletableFuture[0]));
Expand Down Expand Up @@ -222,21 +124,10 @@ public CompletableFuture<Void> synchronize(int totalShards) {

NexusEngineX engineX = ((NexusCore) nexus).getEngineX();
engineX.queue(
(api, store) -> api.bulkOverwriteGlobalApplicationCommands(
manager.getCommands()
.stream()
.filter(nexusCommand -> nexusCommand.getServerIds().isEmpty())
.map(NexusCommand::asSlashCommand).toList()
).thenAccept(applicationCommands -> {
NexusCore.logger.debug("Global commands completed synchronization. [size={}]", applicationCommands.size());
globalFuture.complete(null);
}).exceptionally(throwable -> {
if (throwable != null) {
globalFuture.completeExceptionally(throwable);
}

return null;
})
(api, store) -> SYNCHRONIZE_METHODS.get().bulkOverwriteGlobal(api, manager.getCommands()
.stream()
.filter(nexusCommand -> nexusCommand.getServerIds().isEmpty())
.map(NexusCommand::asSlashCommand).toList())
);

Map<Long, List<SlashCommandBuilder>> serverMappedCommands = new HashMap<>();
Expand All @@ -262,36 +153,10 @@ public CompletableFuture<Void> synchronize(int totalShards) {
serverMappedFutures.put(serverId, new CompletableFuture<>());
}

engineX.queue((int) ((serverId >> 22) % totalShards), (api, store) -> {
if (api.getServerById(serverId).isEmpty()) {
NexusCore.logger.error(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? [shard={};id={}]",
api.getCurrentShard(),
serverId
);
serverMappedFutures.get(serverId).completeExceptionally(
new IllegalStateException(
"Failed to synchronize commands for server, not found on the shard calculated. Is the total shard number value wrong? " +
"[shard=" + api.getCurrentShard() + ";id=" + serverId + "]"
)
);
return;
}

Server server = api.getServerById(serverId).orElseThrow();
api.bulkOverwriteServerApplicationCommands(server, slashCommandBuilders)
.thenAccept(applicationCommands -> {
NexusCore.logger.debug("A server has completed synchronization. [server={}, size={}]", serverId, applicationCommands.size());
serverMappedFutures.get(serverId).complete(null);
})
.exceptionally(throwable -> {
if (throwable != null) {
serverMappedFutures.get(serverId).completeExceptionally(throwable);
}

return null;
});
});
engineX.queue((int) (
(serverId >> 22) % totalShards),
(api, store) -> SYNCHRONIZE_METHODS.get().bulkOverwriteServer(api, slashCommandBuilders, serverId, serverMappedFutures.get(serverId))
);
});

List<CompletableFuture<Void>> futures = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pw.mihou.nexus.features.command.synchronizer.overwrites;

import org.javacord.api.DiscordApi;
import org.javacord.api.interaction.ApplicationCommand;
import org.javacord.api.interaction.SlashCommandBuilder;
import pw.mihou.nexus.features.command.facade.NexusCommand;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface NexusSynchronizeMethods {

CompletableFuture<Void> bulkOverwriteGlobal(DiscordApi shard, List<SlashCommandBuilder> slashCommands);

void bulkOverwriteServer(DiscordApi shard, List<SlashCommandBuilder> slashCommands,
long serverId, CompletableFuture<Void> future);

void deleteForServer(DiscordApi shard, NexusCommand command, long serverId, CompletableFuture<Void> future);

void updateForServer(DiscordApi shard, NexusCommand command, long serverId, CompletableFuture<Void> future);

void createForServer(DiscordApi shard, NexusCommand command, long serverId, CompletableFuture<Void> future);

}
Loading

0 comments on commit f8942c4

Please sign in to comment.