Skip to content

Commit

Permalink
feat: convert @id to @username
Browse files Browse the repository at this point in the history
  • Loading branch information
xslingcn committed May 10, 2022
1 parent c1ebcbe commit 3817323
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ 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.message = processAt(message);
this.messageString = this.message.contentToString();
this.images = this.message.stream().filter(Image.class::isInstance).toList();
List<SingleMessage> replyList = this.message.stream().filter(QuoteReply.class::isInstance).toList();
if (!replyList.isEmpty()) this.quote = (QuoteReply) replyList.get(0);
Expand Down Expand Up @@ -277,4 +277,22 @@ String quoteReply() {
return "";
}
}

MessageChain processAt(MessageChain message) {
MessageSource source = message.get(MessageSource.Key);
if (source == null) return message;
// convert @id to @username
for (SingleMessage singleMessage : message) {
if (singleMessage instanceof At at) {
MessageChainBuilder builder = new MessageChainBuilder();
SingleMessage atName = new PlainText("@" + new MiraiUtils(phenyl).getUserNameOrNameCardOrNick(source.getTargetId(), at.getTarget()));
builder.addAll(message.subList(0, message.indexOf(at)));
builder.append(atName);
if (message.indexOf(at) < message.size())
builder.addAll(message.subList(message.indexOf(at) + 1, message.size()));
message = builder.build();
}
}
return message;
}
}

0 comments on commit 3817323

Please # to comment.