diff --git a/common/src/main/java/live/turna/phenyl/common/message/Formatter.java b/common/src/main/java/live/turna/phenyl/common/message/Formatter.java index 1110ffe..b9b6d7b 100644 --- a/common/src/main/java/live/turna/phenyl/common/message/Formatter.java +++ b/common/src/main/java/live/turna/phenyl/common/message/Formatter.java @@ -5,15 +5,16 @@ import live.turna.phenyl.common.config.Config; import live.turna.phenyl.common.message.schema.*; import live.turna.phenyl.common.plugin.AbstractPhenyl; +import live.turna.phenyl.common.utils.MiraiUtils; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; import net.mamoe.mirai.message.data.*; -import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.NoSuchElementException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -33,14 +34,18 @@ public class Formatter

{ private final transient MessageChain message; private final transient String messageString; private final transient List images; + private final transient QuoteReply quote; - public Formatter(P plugin, String[] format, String color, MessageChain message, @Nullable List images) { + public Formatter(P plugin, String[] format, String color, MessageChain message) { phenyl = plugin; this.format = format; this.color = color; this.message = message; this.messageString = message.contentToString(); - this.images = images; + this.images = this.message.stream().filter(Image.class::isInstance).toList(); + List replyList = this.message.stream().filter(QuoteReply.class::isInstance).toList(); + if (!replyList.isEmpty()) this.quote = (QuoteReply) replyList.get(0); + else this.quote = null; } public Component get() { @@ -182,7 +187,8 @@ Component groupImage() { String pattern = "\\u56fe\\u7247|\\u52a8\\u753b\\u8868\\u60c5"; Matcher match = Pattern.compile(pattern).matcher(messageString); List other = List.of(messageString.split(pattern)); - TextComponent.Builder result = Component.text().content(altColor(format[0])); + TextComponent.Builder result = Component.text().content(altColor(format[0] + quoteReply())); + while (match.find()) { if (other.size() > matchCount) { // append the words other than "\\u56fe\\u7247" and clear events result.append( @@ -224,7 +230,8 @@ Component groupLink() { String pattern = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; Matcher match = Pattern.compile(pattern).matcher(messageString); List other = List.of(messageString.split(pattern)); - TextComponent.Builder result = Component.text().content(altColor(format[0])); + TextComponent.Builder result = Component.text().content(altColor(format[0] + quoteReply())); + while (match.find()) { if (other.size() > linkCount) { result.append( @@ -252,9 +259,22 @@ Component groupLink() { } Component groupRandom() { - return Component.text(altColor(format[0] + messageString)) + return Component.text(altColor(format[0] + quoteReply() + messageString)) .append(Component.text(format.length > 1 ? altColor(color + format[1]) : "")); } + + String quoteReply() { + if (quote == null) return ""; + try { + return i18n("quoteReply", + new MiraiUtils(phenyl).getUserNameOrNameCardOrNick(quote.getSource().getTargetId(), quote.getSource().getFromId()) + , quote.getSource().getOriginalMessage().contentToString()); + } catch (NoSuchElementException e) { + phenyl.getLogger().error(i18n("noSuchGroup", e.getLocalizedMessage())); + if (Config.debug) e.printStackTrace(); + return ""; + } + } } \ No newline at end of file diff --git a/common/src/main/java/live/turna/phenyl/common/utils/MiraiUtils.java b/common/src/main/java/live/turna/phenyl/common/utils/MiraiUtils.java index 12a6085..a3905e7 100644 --- a/common/src/main/java/live/turna/phenyl/common/utils/MiraiUtils.java +++ b/common/src/main/java/live/turna/phenyl/common/utils/MiraiUtils.java @@ -3,6 +3,7 @@ import live.turna.phenyl.common.config.Config; import live.turna.phenyl.common.plugin.AbstractPhenyl; import net.mamoe.mirai.contact.Group; +import net.mamoe.mirai.contact.Member; import net.mamoe.mirai.message.MessageReceipt; import net.mamoe.mirai.message.data.Image; import net.mamoe.mirai.message.data.MessageChain; @@ -94,6 +95,22 @@ public BotConfiguration.MiraiProtocol matchProtocol(String proString) throws Ill return protocol; } + public String getUserNameOrNameCardOrNick(Long groupId, Long senderId) throws NoSuchElementException { + String userName = null; + if (Config.forward_mode.equals("command") || Config.forward_mode.equals("bind")) + userName = phenyl.getStorage().getBinding(senderId).mcname(); + + if (userName == null) { + try { + Member from = phenyl.getMirai().getBot().getGroupOrFail(groupId).getOrFail(senderId); + userName = from.getNameCard().isEmpty() ? from.getNick() : from.getNameCard(); + } catch (NoSuchElementException e) { + throw new NoSuchElementException(String.valueOf(groupId)); + } + } + return userName; + } + /** * Send group message. *