feat: *netcmd

This commit is contained in:
ChomeNS
2025-04-12 10:48:45 +07:00
parent 0ca9990d08
commit 30a7bcd24b
4 changed files with 104 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
package me.chayapak1.chomens_bot.command.contexts;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.CommandContext;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class RemoteCommandContext extends CommandContext {
public final Bot targetBot;
public final CommandContext source;
public RemoteCommandContext (final Bot targetBot, final CommandContext source) {
super(targetBot, source.prefix, source.sender, source.inGame);
this.targetBot = targetBot;
this.source = source;
}
@Override
public void sendOutput (final Component component) {
source.sendOutput(
Component
.translatable(
"[%s] %s",
Component.text(targetBot.getServerString(), NamedTextColor.GRAY),
Component.empty()
.color(NamedTextColor.WHITE)
.append(component)
)
.color(NamedTextColor.DARK_GRAY)
);
}
}

View File

@@ -0,0 +1,69 @@
package me.chayapak1.chomens_bot.commands;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.command.contexts.RemoteCommandContext;
import net.kyori.adventure.text.Component;
import java.util.ArrayList;
import java.util.List;
public class NetCommandCommand extends Command {
public NetCommandCommand () {
super(
"netcmd",
"Runs a command on the specified server(s) and return its output",
new String[] { "<servers separated by a comma> <command>" },
new String[] { "networkcommand", "irccommand", "remotecommand" },
TrustLevel.TRUSTED
);
}
@Override
public Component execute (final CommandContext context) throws CommandException {
final String rawServers = context.getString(false, true, true);
final List<Bot> allBots = context.bot.bots;
final List<Bot> bots;
if (rawServers.equals("all")) {
bots = allBots;
} else {
bots = new ArrayList<>();
final String[] servers = rawServers.split(",");
for (final Bot bot : allBots) {
if (!bot.loggedIn) continue;
for (final String server : servers) {
if (
server.isBlank()
|| !bot.getServerString(true)
.toLowerCase()
.trim()
.contains(server.toLowerCase())
) continue;
bots.add(bot);
}
}
}
if (bots.isEmpty()) throw new CommandException(Component.text("No servers have been found with your input"));
final String command = context.getString(true, true);
for (final Bot bot : bots) {
final CommandContext remoteContext = new RemoteCommandContext(bot, context);
context.bot.commandHandler.executeCommand(command, remoteContext, null);
}
return null;
}
}

View File

@@ -65,6 +65,7 @@ public class CommandHandlerPlugin implements TickPlugin.Listener {
registerCommand(new GrepLogCommand());
registerCommand(new FindAltsCommand());
registerCommand(new RestartCommand());
registerCommand(new NetCommandCommand());
}
public static void registerCommand (final Command command) {