1
0
Fork 0
forked from mc/VTools

一堆bug

This commit is contained in:
Sodium-Aluminate 2023-11-02 06:47:47 +08:00
parent e4c91adac0
commit 425210963f
2 changed files with 41 additions and 20 deletions

View file

@ -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<MessageEntity.Type, MessageEntity> 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<StringBuilder, MessageEntity, StringBuilder> 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<StringBlock> 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);
}

View file

@ -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<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.";
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> 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<SendMessage, SendResponse>() {
@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();
}