1
0
Fork 0
forked from mc/VTools

Compare commits

...

1 commit

Author SHA1 Message Date
9cb090ce2d
tgbot /list 2023-05-28 11:19:40 +08:00
2 changed files with 78 additions and 24 deletions

View file

@ -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()));
}
} }
} }

View file

@ -1,9 +1,11 @@
package de.strifel.VTools.listeners; package de.strifel.VTools.listeners;
import com.google.common.collect.ImmutableList;
import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener; import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.User;
import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.SendResponse; import com.pengrad.telegrambot.response.SendResponse;
import com.velocitypowered.api.event.Subscribe; 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.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import de.strifel.VTools.VTools; import de.strifel.VTools.VTools;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
@ -21,7 +24,11 @@ import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class TGBridge { public class TGBridge {
private final VTools plugin; private final VTools plugin;
@ -30,7 +37,7 @@ public class TGBridge {
private TelegramBot bot; private TelegramBot bot;
private String TOKEN = ""; private String TOKEN = "";
private long CHAT_ID = 0L; private HashSet<Long> CHAT_IDS = new HashSet<>();
private long backoffSec = 1L; private long backoffSec = 1L;
@ -53,14 +60,15 @@ public class TGBridge {
} }
File configFile = new File(configDir, "config.yaml"); File configFile = new File(configDir, "config.yaml");
if (!configFile.exists()) { 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); String configStr = Files.readString(Path.of(configFile.toURI()), StandardCharsets.UTF_8);
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
Map<String, String> config = yaml.load(configStr); Map<String, Object> config = yaml.load(configStr);
synchronized (this) { synchronized (this) {
this.CHAT_ID = Long.parseLong(config.getOrDefault("chat_id", "0")); ((List<String>)config.getOrDefault("chat_id", List.of())).stream().forEach(s -> this.CHAT_IDS.add(Long.parseLong(s)));
this.TOKEN = config.getOrDefault("token", ""); this.CHAT_IDS.removeIf(id -> id == 0L);
this.TOKEN = (String)config.getOrDefault("token", "");
} }
} catch (Exception e) { } catch (Exception e) {
plugin.logger.error("parsing config", e); plugin.logger.error("parsing config", e);
@ -69,7 +77,7 @@ public class TGBridge {
private void botInit() { private void botInit() {
loadConfig(); loadConfig();
if (TOKEN.isEmpty() || CHAT_ID == 0L) return; if (TOKEN.isEmpty() || CHAT_IDS.isEmpty()) return;
bot = new TelegramBot(TOKEN); bot = new TelegramBot(TOKEN);
bot.setUpdatesListener(updates -> { bot.setUpdatesListener(updates -> {
backoffSec = 1L; backoffSec = 1L;
@ -78,12 +86,46 @@ public class TGBridge {
if (update != null && if (update != null &&
update.message() != null && update.message() != null &&
update.message().chat() != null && update.message().chat() != null &&
update.message().chat().id() == CHAT_ID && CHAT_IDS.contains(update.message().chat().id()) &&
update.message().text() != null &&
!update.message().text().isEmpty() &&
update.message().from() != null 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) { 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) { protected void inbound(String content) {
for (Player player : server.getAllPlayers()) { for (Player player : server.getAllPlayers()) {
if (player.getCurrentServer().isPresent()) { if (player.getCurrentServer().isPresent()) {
@ -115,19 +161,21 @@ public class TGBridge {
if (content.length() > 4000) { if (content.length() > 4000) {
content = content.substring(0, 4000); content = content.substring(0, 4000);
} }
bot.execute(new SendMessage(CHAT_ID, content), new Callback<SendMessage, SendResponse>() { for (long CHAT_ID : CHAT_IDS) {
@Override bot.execute(new SendMessage(CHAT_ID, content), new Callback<SendMessage, SendResponse>() {
public void onResponse(SendMessage sendMessage, SendResponse sendResponse) { @Override
if (!sendResponse.isOk()) { public void onResponse(SendMessage sendMessage, SendResponse sendResponse) {
plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description())); if (!sendResponse.isOk()) {
plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description()));
}
} }
}
@Override @Override
public void onFailure(SendMessage sendMessage, IOException e) { public void onFailure(SendMessage sendMessage, IOException e) {
plugin.logger.error("sending message", e); plugin.logger.error("sending message", e);
} }
}); });
}
} }
@Subscribe @Subscribe
@ -140,12 +188,16 @@ public class TGBridge {
@Subscribe @Subscribe
public void onServerConnected(ServerConnectedEvent event) { public void onServerConnected(ServerConnectedEvent event) {
if (event.getPreviousServer().isEmpty()) { 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 @Subscribe
public void onDisconnect(DisconnectEvent event) { 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()));
}
} }
} }