Skip to content

Commit

Permalink
fix(fabric): openshamrock的消息链支持
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Mar 1, 2024
1 parent 5a1e8d1 commit 89eeabe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx8G

# Mod Properties
mod_version=2.1.9.1
mod_version=2.1.9.2
maven_group=cn.evole.mods
archives_base_name=McBot-fabric

Expand All @@ -11,5 +11,5 @@ archives_base_name=McBot-fabric
minecraft_version=1.20.1
loader_version=0.14.21
fabric_version=0.83.1+1.20.1
onebot_client_version=0.3.9
onebot_client_version=0.3.9.1
toml_version=0.1.5
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cn.evole.onebot.sdk.event.meta.LifecycleMetaEvent;
import cn.evole.onebot.sdk.event.notice.group.GroupDecreaseNoticeEvent;
import cn.evole.onebot.sdk.event.notice.group.GroupIncreaseNoticeEvent;
import cn.evole.onebot.sdk.util.BotUtils;
import cn.evole.onebot.sdk.util.MsgUtils;
import lombok.val;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void onMessage(GroupMessageEvent event) {
&& event.getUserId() != ModConfig.INSTANCE.getCommon().getBotId()//过滤机器人
) {

String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片
String send = CQUtils.replace(event);//暂时匹配仅符合字符串聊天内容与图片


if (ModConfig.INSTANCE.getCmd().isQqChatPrefixOn()) {
Expand Down Expand Up @@ -154,7 +155,7 @@ public void onMessage(GuildMessageEvent event) {
&& event.getUserId() != ModConfig.INSTANCE.getCommon().getBotId()
) {

String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片
String send = CQUtils.replace(event);//暂时匹配仅符合字符串聊天内容与图片
if (ModConfig.INSTANCE.getCmd().isQqChatPrefixOn()) {
val split = event.getMessage().split(" ");
if (ModConfig.INSTANCE.getCmd().getQqChatPrefix().equals(split[0])) //指定前缀发送
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static void register(Player player, String message) {
ModConfig.INSTANCE.getCmd().isMcChatPrefixOn()
&& ModConfig.INSTANCE.getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message);

msg = CQUtils.replace(msg);
if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) {
Const.sendGuildMsg(msg);
} else {
Expand Down
23 changes: 18 additions & 5 deletions fabric/src/main/java/cn/evole/mods/mcbot/util/onebot/CQUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.onebot.sdk.entity.ArrayMsg;
import cn.evole.onebot.sdk.event.message.MessageEvent;
import cn.evole.onebot.sdk.util.BotUtils;
import cn.evole.onebot.sdk.util.json.GsonUtils;
import com.google.gson.JsonObject;
import lombok.val;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cn.evole.onebot.sdk.util.BotUtils.arrayMsgToCode;

/**
* Project: Bot-Connect-fabric-1.18
* Author: cnlimiter
Expand All @@ -31,14 +38,20 @@ public static boolean hasImg(String msg) {
return m.find();
}

public static String replace(String msg) {
if (!ModConfig.INSTANCE.getCommon().isCQUtils()){
return msg;
public static void rawConvert(String message, MessageEvent event) {
if (message.startsWith("[") && !message.startsWith("[CQ:")) {//如果是消息链(消息链以[开头)
List<ArrayMsg> arrayMsg = GsonUtils.convertToList(message, ArrayMsg.class);
event.setArrayMsg(arrayMsg);
event.setMessage(arrayMsgToCode(arrayMsg));
}
}

if (msg.indexOf('[') == -1)
return BotUtils.unescape(msg);
public static String replace(MessageEvent event) {

//将消息链转化成cq码
rawConvert(event.getMessage(), event);
//获取转化完的
String msg = event.getMessage();

String back;
StringBuffer message = new StringBuffer();
Expand Down

1 comment on commit 89eeabe

@xia-mc
Copy link
Member

@xia-mc xia-mc commented on 89eeabe Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@A0000Xz 安息了

Please sign in to comment.