From 9cb090ce2d228108dda293073c6d7a8bbdac084d Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 28 May 2023 11:19:16 +0800 Subject: [PATCH] tgbot /list --- .../strifel/VTools/listeners/GlobalChat.java | 4 +- .../de/strifel/VTools/listeners/TGBridge.java | 98 ++++++++++++++----- 2 files changed, 78 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/strifel/VTools/listeners/GlobalChat.java b/src/main/java/de/strifel/VTools/listeners/GlobalChat.java index 7dc4c60..4954a8b 100644 --- a/src/main/java/de/strifel/VTools/listeners/GlobalChat.java +++ b/src/main/java/de/strifel/VTools/listeners/GlobalChat.java @@ -39,6 +39,8 @@ public class GlobalChat { } } } - TGBridge.INSTANCE.outbound(String.format("[%s] <%s> %s", senderServer == null ? "null" : senderServer.getServerInfo().getName(), event.getPlayer().getUsername(), event.getMessage())); + if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge")) { + TGBridge.INSTANCE.outbound(String.format("[%s] <%s> %s", senderServer == null ? "null" : senderServer.getServerInfo().getName(), event.getPlayer().getUsername(), event.getMessage())); + } } } diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java index 3a54e71..70fe22d 100644 --- a/src/main/java/de/strifel/VTools/listeners/TGBridge.java +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -1,9 +1,11 @@ package de.strifel.VTools.listeners; +import com.google.common.collect.ImmutableList; import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.UpdatesListener; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.SendResponse; import com.velocitypowered.api.event.Subscribe; @@ -12,6 +14,7 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.server.RegisteredServer; import de.strifel.VTools.VTools; import net.kyori.adventure.text.Component; import org.yaml.snakeyaml.Yaml; @@ -21,7 +24,11 @@ import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class TGBridge { private final VTools plugin; @@ -30,7 +37,7 @@ public class TGBridge { private TelegramBot bot; private String TOKEN = ""; - private long CHAT_ID = 0L; + private HashSet CHAT_IDS = new HashSet<>(); private long backoffSec = 1L; @@ -53,14 +60,15 @@ public class TGBridge { } File configFile = new File(configDir, "config.yaml"); if (!configFile.exists()) { - Files.write(Path.of(configFile.toURI()), "chat_id: \"0\"\ntoken: \"\"\n".getBytes(StandardCharsets.UTF_8)); + Files.write(Path.of(configFile.toURI()), "chat_id:\n- \"0\"\ntoken: \"\"\n".getBytes(StandardCharsets.UTF_8)); } String configStr = Files.readString(Path.of(configFile.toURI()), StandardCharsets.UTF_8); Yaml yaml = new Yaml(); - Map config = yaml.load(configStr); + Map config = yaml.load(configStr); synchronized (this) { - this.CHAT_ID = Long.parseLong(config.getOrDefault("chat_id", "0")); - this.TOKEN = config.getOrDefault("token", ""); + ((List)config.getOrDefault("chat_id", List.of())).stream().forEach(s -> this.CHAT_IDS.add(Long.parseLong(s))); + this.CHAT_IDS.removeIf(id -> id == 0L); + this.TOKEN = (String)config.getOrDefault("token", ""); } } catch (Exception e) { plugin.logger.error("parsing config", e); @@ -69,7 +77,7 @@ public class TGBridge { private void botInit() { loadConfig(); - if (TOKEN.isEmpty() || CHAT_ID == 0L) return; + if (TOKEN.isEmpty() || CHAT_IDS.isEmpty()) return; bot = new TelegramBot(TOKEN); bot.setUpdatesListener(updates -> { backoffSec = 1L; @@ -78,12 +86,46 @@ public class TGBridge { if (update != null && update.message() != null && update.message().chat() != null && - update.message().chat().id() == CHAT_ID && - update.message().text() != null && - !update.message().text().isEmpty() && + CHAT_IDS.contains(update.message().chat().id()) && update.message().from() != null ) { - inbound(String.format("[tg] <%s> %s", update.message().from().lastName() == null ? update.message().from().firstName(): String.format("%s %s", update.message().from().firstName(), update.message().from().lastName()), update.message().text())); + if (update.message().text() != null && !update.message().text().isEmpty()) { + String msg = update.message().text(); + if (msg.equals("/list")) { + ArrayList out = new ArrayList<>(); + String fmt = server.getAllPlayers().size() > 1 ? "%d players are currently connected to the proxy." : "%d player is currently connected to the proxy."; + out.add(String.format(fmt, server.getAllPlayers().size())); + List servers = new ArrayList<>(server.getAllServers()); + for (RegisteredServer server : servers) { + List onServer = ImmutableList.copyOf(server.getPlayersConnected()); + if (!onServer.isEmpty()) { + out.add(String.format("[%s] (%d): %s", + server.getServerInfo().getName(), + onServer.size(), + onServer.stream().map(Player::getUsername).collect(Collectors.joining(", "))) + ); + + } + } + outbound(String.join("\n", out)); + } + tgInbound(update.message().from(), msg); + } + else if (update.message().sticker() != null) { + tgInbound(update.message().from(), "[sticker]"); + } + else if (update.message().photo() != null) { + tgInbound(update.message().from(), "[photo]"); + } + else if (update.message().audio() != null) { + tgInbound(update.message().from(), "[audio]"); + } + else if (update.message().voice() != null) { + tgInbound(update.message().from(), "[voice]"); + } + else if (update.message().document() != null) { + tgInbound(update.message().from(), "[document]"); + } } } catch (Exception e) { @@ -102,6 +144,10 @@ public class TGBridge { }); } + protected void tgInbound(User user, String content) { + inbound(String.format("[tg] <%s> %s", user.lastName() == null ? user.firstName() : String.format("%s %s", user.firstName(), user.lastName()), content)); + } + protected void inbound(String content) { for (Player player : server.getAllPlayers()) { if (player.getCurrentServer().isPresent()) { @@ -115,19 +161,21 @@ public class TGBridge { if (content.length() > 4000) { content = content.substring(0, 4000); } - bot.execute(new SendMessage(CHAT_ID, content), new Callback() { - @Override - public void onResponse(SendMessage sendMessage, SendResponse sendResponse) { - if (!sendResponse.isOk()) { - plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description())); + for (long CHAT_ID : CHAT_IDS) { + bot.execute(new SendMessage(CHAT_ID, content), new Callback() { + @Override + public void onResponse(SendMessage sendMessage, SendResponse sendResponse) { + if (!sendResponse.isOk()) { + plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description())); + } } - } - @Override - public void onFailure(SendMessage sendMessage, IOException e) { - plugin.logger.error("sending message", e); - } - }); + @Override + public void onFailure(SendMessage sendMessage, IOException e) { + plugin.logger.error("sending message", e); + } + }); + } } @Subscribe @@ -140,12 +188,16 @@ public class TGBridge { @Subscribe public void onServerConnected(ServerConnectedEvent event) { if (event.getPreviousServer().isEmpty()) { - outbound(String.format("%s joined the proxy", event.getPlayer().getUsername())); + if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) { + outbound(String.format("%s joined the proxy", event.getPlayer().getUsername())); + } } } @Subscribe public void onDisconnect(DisconnectEvent event) { - outbound(String.format("%s left the proxy", event.getPlayer().getUsername())); + if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) { + outbound(String.format("%s left the proxy", event.getPlayer().getUsername())); + } } }