1
0
Fork 0
forked from mc/VTools

tgbot /list

This commit is contained in:
JerryXiao 2023-05-28 11:19:16 +08:00
parent fc1bf1339e
commit 9cb090ce2d
Signed by untrusted user: Jerry
GPG key ID: 22618F758B5BE2E5
2 changed files with 78 additions and 24 deletions

View file

@ -39,6 +39,8 @@ public class GlobalChat {
}
}
}
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()));
}
}
}

View file

@ -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<Long> 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<String, String> config = yaml.load(configStr);
Map<String, Object> config = yaml.load(configStr);
synchronized (this) {
this.CHAT_ID = Long.parseLong(config.getOrDefault("chat_id", "0"));
this.TOKEN = config.getOrDefault("token", "");
((List<String>)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<String> 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<RegisteredServer> servers = new ArrayList<>(server.getAllServers());
for (RegisteredServer server : servers) {
List<Player> 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,6 +161,7 @@ public class TGBridge {
if (content.length() > 4000) {
content = content.substring(0, 4000);
}
for (long CHAT_ID : CHAT_IDS) {
bot.execute(new SendMessage(CHAT_ID, content), new Callback<SendMessage, SendResponse>() {
@Override
public void onResponse(SendMessage sendMessage, SendResponse sendResponse) {
@ -129,6 +176,7 @@ public class TGBridge {
}
});
}
}
@Subscribe
public void onProxyShutdown(ProxyShutdownEvent event) {
@ -140,12 +188,16 @@ public class TGBridge {
@Subscribe
public void onServerConnected(ServerConnectedEvent event) {
if (event.getPreviousServer().isEmpty()) {
if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) {
outbound(String.format("%s joined the proxy", event.getPlayer().getUsername()));
}
}
}
@Subscribe
public void onDisconnect(DisconnectEvent event) {
if (!event.getPlayer().hasPermission("vtools.globalchat.bypassbridge.join")) {
outbound(String.format("%s left the proxy", event.getPlayer().getUsername()));
}
}
}