From 425210963fdf83b28144e5617d10d6f95f436a2d Mon Sep 17 00:00:00 2001 From: Sodium-Aluminate Date: Thu, 2 Nov 2023 06:47:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=A0=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VTools/listeners/MarkdownString.java | 35 ++++++++++++++----- .../de/strifel/VTools/listeners/TGBridge.java | 26 +++++++------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/de/strifel/VTools/listeners/MarkdownString.java b/src/main/java/de/strifel/VTools/listeners/MarkdownString.java index 156204e..fdcc9fb 100644 --- a/src/main/java/de/strifel/VTools/listeners/MarkdownString.java +++ b/src/main/java/de/strifel/VTools/listeners/MarkdownString.java @@ -7,8 +7,8 @@ import java.util.*; import java.util.function.BiFunction; public class MarkdownString { - - private MarkdownString(){ + + private MarkdownString() { throw new IllegalStateException(); } @@ -82,6 +82,15 @@ public class MarkdownString { private static class StringBlock { + @Override + public String toString() { + return "StringBlock{" + + "text='" + text + '\'' + + ", entities=" + entities + + ", offset=" + offset + + '}'; + } + private final String text; private Map entities; private final int offset; @@ -94,24 +103,29 @@ public class MarkdownString { private String getMarkdownTextWithoutLink() { // todo: 压缩 *text1**_text2_* 为 *text1_text2_* // 链接类不能和链接类套娃 - assert !((entities.containsKey(MessageEntity.Type.text_link)) && (entities.containsKey(MessageEntity.Type.text_mention))); + assert (!entities.containsKey(MessageEntity.Type.text_link)) || (!entities.containsKey(MessageEntity.Type.text_mention)); // 等宽(pre 和 code)完全禁止套娃 - assert !((entities.containsKey(MessageEntity.Type.pre) || entities.containsKey(MessageEntity.Type.code)) && (entities.size() > 1)); + assert (!entities.containsKey(MessageEntity.Type.pre) && !entities.containsKey(MessageEntity.Type.code)) || (entities.size() <= 1); StringBuilder s = new StringBuilder(escapeStr(text)); for (var entry : entities.entrySet()) { - s = formats.get(entry.getKey()).apply(s, entry.getValue()); // 无用赋值...理论上。 + BiFunction repeater = (sb, e) -> sb; + s = formats.getOrDefault(entry.getKey(), repeater).apply(s, entry.getValue()); // 无用赋值...理论上。 } return s.toString(); } } public static String markdownString(Message message) { + if (message.entities() == null || message.entities().length == 0) return message.text(); List stringBlocks = createStringBlocks(message); + StringBuilder s = new StringBuilder(); for (int i = 0; i < stringBlocks.size(); i++) { + + var stringBlock = stringBlocks.get(i); var entities = stringBlock.entities; String withoutLink = stringBlock.getMarkdownTextWithoutLink(); @@ -131,7 +145,9 @@ public class MarkdownString { if (stringBlock.text.length() + stringBlock.offset == end) { break; } - assert (stringBlock.text.length() + stringBlock.offset < end); + if (stringBlock.text.length() + stringBlock.offset > end) { + throw new IllegalStateException("奶冰可爱捏"); + } i++; stringBlock = stringBlocks.get(i); } @@ -179,6 +195,9 @@ public class MarkdownString { // 给文本重新赋格式(MessageEntity) for (MessageEntity entity : message.entities()) { + if (!formats.containsKey(entity.type())) { + continue; + } for (StringBlock stringBlock : stringBlocks) { int blockStart = stringBlock.offset; int blockEnd = blockStart + stringBlock.text.length(); @@ -187,10 +206,10 @@ public class MarkdownString { int entityEnd = entityStart + entity.length(); assert (blockStart < blockEnd) && (entityStart < entityEnd); - if (blockStart > entityEnd || blockEnd < entityStart) { + if (blockStart >= entityEnd || blockEnd <= entityStart) { continue; } - assert (blockStart >= entityStart) && (blockEnd <= entityEnd); + assert (blockStart >= entityStart) && (blockEnd <= entityEnd) : String.format("%s,%s,%s,%s", blockStart, blockEnd, entityStart, entityEnd); MessageEntity old = stringBlock.entities.put(entity.type(), entity); assert (old == null); } diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java index 8dfe7a0..67bde90 100644 --- a/src/main/java/de/strifel/VTools/listeners/TGBridge.java +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -51,7 +51,7 @@ public class TGBridge { private long backoffSec = 1L; - private static final String DIVIDER = "----------\n"; + private static final String DIVIDER = "————————\n"; /** * markdown escaped */ @@ -114,7 +114,7 @@ public class TGBridge { String command = s[0]; @Nullable String arg = s.length == 2 ? s[1] : null; switch (command) { - case "/list" -> outbound(genOnlineStatus(), ParseMode.Markdown); + case "/list" -> outbound(genOnlineStatus(), ParseMode.MarkdownV2); case "/setpin" -> { Message replyTo = message.replyToMessage(); if (arg == null || arg.length() == 0) { @@ -143,7 +143,7 @@ public class TGBridge { } - outbound("old pinned note: \n" + pinNote, ParseMode.Markdown); + outbound("old pinned note: \n" + pinNote, ParseMode.MarkdownV2); pinNote = markdownString.substring("/setpin ".length()); if (replyTo != null) { @@ -159,7 +159,7 @@ public class TGBridge { int messageId = sendResponse.message().messageId(); ONLINE_STATUS_MESSAGE_ID = messageId > 0 ? messageId : ONLINE_STATUS_MESSAGE_ID; } - }, ParseMode.Markdown + }, ParseMode.MarkdownV2 ); } } @@ -230,7 +230,7 @@ public class TGBridge { */ 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."; + 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 registeredServers = new ArrayList<>(server.getAllServers()); for (RegisteredServer registeredServer : registeredServers) { @@ -241,14 +241,15 @@ public class TGBridge { } } if (!onServer.isEmpty()) { - out.add(String.format("[%s] (%d): %s", - registeredServer.getServerInfo().getName(), - onServer.size(), - onServer.stream().map(Player::getUsername).collect(Collectors.joining(", "))) + out.add( + String.format("\\[%s\\] \\(%d\\): %s", + "`" + MarkdownString.escapeStr(registeredServer.getServerInfo().getName()) + "`", + onServer.size(), + onServer.stream().map(player -> "`" + MarkdownString.escapeStr(player.getUsername()) + "`").collect(Collectors.joining(", "))) ); } } - return MarkdownString.escapeStr(String.join("\n", out)); + return String.join("\n", out); } protected void tgInbound(User user, String content) { @@ -285,7 +286,8 @@ public class TGBridge { SendMessage sendMessage = new SendMessage(CHAT_ID, content); if (parseMode != null) { - sendMessage.parseMode(parseMode); + boolean a = sendMessage == sendMessage.parseMode(parseMode); + assert a; } bot.execute(sendMessage, new Callback() { @Override @@ -415,7 +417,7 @@ public class TGBridge { } BaseResponse response; try { - response = bot.execute(new EditMessageText(CHAT_ID, ONLINE_STATUS_MESSAGE_ID, markdownText).parseMode(ParseMode.Markdown)); + response = bot.execute(new EditMessageText(CHAT_ID, ONLINE_STATUS_MESSAGE_ID, markdownText).parseMode(ParseMode.MarkdownV2)); } catch (RuntimeException e) {return false;} return response != null && response.isOk(); }