From c2ee4fca4f6449382b6760008a360af76be1dfbf Mon Sep 17 00:00:00 2001 From: Sodium-Aluminate Date: Sat, 28 Oct 2023 17:11:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E4=B8=80=E4=B8=AA=E7=BD=AE?= =?UTF-8?q?=E9=A1=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../de/strifel/VTools/listeners/TGBridge.java | 93 +++++++++++++++---- 1 file changed, 75 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java index ec89215..0bd272f 100644 --- a/src/main/java/de/strifel/VTools/listeners/TGBridge.java +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -1,6 +1,5 @@ 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; @@ -8,8 +7,10 @@ import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.EditMessageText; +import com.pengrad.telegrambot.request.GetChat; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.BaseResponse; +import com.pengrad.telegrambot.response.GetChatResponse; import com.pengrad.telegrambot.response.SendResponse; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.DisconnectEvent; @@ -49,6 +50,9 @@ public class TGBridge { private long backoffSec = 1L; + private static final String DIVIDER = "----------\n"; + private String pinNote; + public TGBridge(@NotNull VTools plugin) { INSTANCE = this; this.plugin = plugin; @@ -88,6 +92,7 @@ public class TGBridge { loadConfig(); if (TOKEN.isEmpty() || CHAT_ID == 0L) return; bot = new TelegramBot(TOKEN); + getPinnedMessage(); bot.setUpdatesListener(updates -> { backoffSec = 1L; for (Update update : updates) { @@ -100,22 +105,43 @@ public class TGBridge { ) { if (update.message().text() != null && !update.message().text().isEmpty()) { String msg = update.message().text(); - switch (msg) { - case "/list": - outbound(genOnlineStatus()); - break; - case "/setpin": - Message replyTo = update.message().replyToMessage(); - if (replyTo == null || replyTo.from() == null || replyTo.from().id() != BOT_ID) { - outbound("must reply a message that from the bot"); - } else { - int messageId = replyTo.messageId(); - ONLINE_STATUS_MESSAGE_ID = messageId > 0 ? messageId : ONLINE_STATUS_MESSAGE_ID; - outbound("done"); + if (msg.startsWith("/")) { + String[] s = msg.split("(@[A-Za-z0-9_]bot)?[\t \n]+", 2); + String command = s[0]; + @Nullable String arg = s.length == 2 ? s[1] : null; + switch (command) { + case "/list" -> outbound(genOnlineStatus()); + case "/setpin" -> { + boolean shouldApplyEdit = true; + boolean needUpdate = false; + Message replyTo = update.message().replyToMessage(); + if (replyTo == null) { + if (arg == null || arg.length() == 0) + outbound(""" + usage: + use "/setpin" reply a message that from the bot to set that message to pin-message, + or use "/setpin " to update current pinned message."""); + } else if (replyTo.from() == null || replyTo.from().id() != BOT_ID || replyTo.messageId() <= 0) { + outbound("must reply a message that from the bot (or reply nothing)."); + shouldApplyEdit = false; + } else { + readOldPinnedMessage(replyTo); + if (arg == null || arg.length() == 0) { + outbound("done"); + }else { + ONLINE_STATUS_MESSAGE_ID = replyTo.messageId(); + } + needUpdate = true; + } + + if (shouldApplyEdit && arg != null && arg.length() > 0) { + outbound("done. old pinned note: \n" + pinNote); + pinNote = arg; + needUpdate = true; + } + if (needUpdate) updateOnlineStatus(); } - break; - case "/genpin": - outbound(genOnlineStatus(), + case "/genpin" -> outbound(genPinMessage(), (sendMessage, sendResponse) -> { if (!sendResponse.isOk()) { plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description())); @@ -125,7 +151,7 @@ public class TGBridge { } } ); - break; + } } tgInbound(update.message().from(), msg); } else if (update.message().sticker() != null) { @@ -156,6 +182,37 @@ public class TGBridge { }); } + private boolean getPinnedMessage(){ + + try { + GetChatResponse response = bot.execute(new GetChat(CHAT_ID)); + Message pinnedMessage = response.chat().pinnedMessage(); + if(pinnedMessage.from().id()==BOT_ID){ + readOldPinnedMessage(pinnedMessage); + updateOnlineStatus(); + } + }catch (RuntimeException e){ + plugin.logger.error("get group info failed."); + throw e; + } + return true; + } + + private void readOldPinnedMessage(Message message){ + ONLINE_STATUS_MESSAGE_ID = message.messageId(); + String text = message.text(); + String[] s = text.split(DIVIDER, 2); + pinNote = s.length == 2 ? s[1] : "(use \"/setpin\" to set note here)"; + } + + private String genPinMessage() { + String onlineStatus = genOnlineStatus(); + if (pinNote != null && pinNote.length() > 1) { + return onlineStatus + "\n" + DIVIDER + pinNote; + } else { + return onlineStatus; + } + } private String genOnlineStatus() { 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."; @@ -320,7 +377,7 @@ public class TGBridge { } protected boolean updateOnlineStatus() { - return editOnlineStatusMessage(genOnlineStatus()); + return editOnlineStatusMessage(genPinMessage()); } protected boolean setOnlineStatusNotAvailable() {