Skip to content

Commit

Permalink
Limit number of lines in command result, and join into single message
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Dec 9, 2023
1 parent 0e7608c commit 5851126
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public final class RemoteCommandIntegration {
private static final String DISCORD_EVERYONE_ROLE = "discord_everyone";
private static final int MAX_RESULT_LINES = 5;

private final ConcurrentLinkedQueue<RemoteCommand> commandQueue = new ConcurrentLinkedQueue<>();

Expand Down Expand Up @@ -61,14 +63,26 @@ public static void bind(NucleoidIntegrations integrations, IntegrationsConfig co
private void tick(MinecraftServer server) {
RemoteCommand command;
while ((command = this.commandQueue.poll()) != null) {
var commandSource = command.createCommandSource(server, this::sendCommandResult);
var results = new ArrayList<Text>();
var commandSource = command.createCommandSource(server, results::add);
server.getCommandManager().executeWithPrefix(commandSource, command.command);
sendCommandResults(results);
}
}

private void sendCommandResult(Text text) {
private void sendCommandResults(List<Text> results) {
if (results.isEmpty()) {
return;
}
var content = results.stream()
.limit(MAX_RESULT_LINES)
.map(Text::getString)
.collect(Collectors.joining("\n"));
if (results.size() > MAX_RESULT_LINES) {
content += "\n...and " + (results.size() - MAX_RESULT_LINES) + " more lines";
}
var body = new JsonObject();
body.addProperty("content", text.getString());
body.addProperty("content", content);
this.systemSender.send(body);
}

Expand Down

1 comment on commit 5851126

@ashhhleyyy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this cause commands that send messages after they've returned to have the responses dropped, or does vanilla not have commands like that?

Please sign in to comment.