修复了clear指令不能正确清除相关记录的bug

修复了截取消息时出现的错误
This commit is contained in:
2024-12-01 19:37:53 +08:00
parent 3d814547f0
commit d8932aba5c
5 changed files with 73 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import net.mamoe.mirai.event.events.FriendMessageEvent;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.event.events.MessageEvent;
import plugin.constant.ChatConstant;
import plugin.constant.Constant;
import plugin.listener.FriendMessageListener;
import plugin.listener.GroupMessageListener;
import plugin.listener.OwnerMessageListener;
@@ -56,7 +57,7 @@ public final class App extends JavaPlugin {
.filter(event -> {
String msg = event.getMessage().contentToString();
long groupId = event.getGroup().getId();
return ((msg.startsWith(".") && msg.length() != 1) || msg.startsWith("@" + bot) || customCommands.containsKey(msg.split(" ")[0] + ChatConstant.BLANK)) && !blacklist.contains(groupId);
return ((msg.startsWith(Constant.Order.PREFIX_ONECE) && msg.length() != 1) || msg.startsWith(Constant.Order.PREFIX_DEFAULT + bot) || customCommands.containsKey(msg.split(Constant.Order.SPLIT_BLANK)[0] + Constant.Order.SPLIT_BLANK)) && !blacklist.contains(groupId);
}).registerListenerHost(new GroupMessageListener());
//私聊监听器
@@ -64,7 +65,7 @@ public final class App extends JavaPlugin {
.filter(event -> {
String msg = event.getMessage().contentToString();
String sender = String.valueOf(event.getFriend().getId());
return !(msg.startsWith(ChatConstant.SET) && sender.equals(owner)) && !msg.equals(ChatConstant.HELP);
return !(msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) && !msg.equals(Constant.Order.MSG_HELP);
}).registerListenerHost(new FriendMessageListener());
//所有者监听
@@ -72,13 +73,13 @@ public final class App extends JavaPlugin {
.filter(event -> {
String msg = event.getMessage().contentToString();
String sender = String.valueOf(event.getSender().getId());
return msg.startsWith(ChatConstant.SET) && sender.equals(owner);
return msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner);
}).registerListenerHost(new OwnerMessageListener());
//帮助监听
GlobalEventChannel.INSTANCE
.filterIsInstance(MessageEvent.class)
.filter(event -> event.getMessage().contentToString().equals(ChatConstant.HELP))
.filter(event -> event.getMessage().contentToString().equals(Constant.Order.MSG_HELP))
.subscribeAlways(MessageEvent.class, event -> {
synchronized (customCommands) {
final String[] helpMsg = {"""

View File

@@ -0,0 +1,50 @@
package plugin.constant;
public class Constant {
public static class Character{
public static final String USER = "user";
public static final String SYSTEM = "system";
public static final String ASSISTANT = "assistant";
}
public static class Model{
public static final String DEEPSEEK_CHAT = "deepseek-chat";
public static final String GLM_4_FLASH = "glm-4-flash";
public static final String GLM_4_0520 = "glm-4-0520";
public static final String GLM_4_PLUS = "glm-4-Plus";
}
public static class Status{
public static final String SUCCESS = "success";
public static final String ERROR = "error";
}
public static class Regex{
public static final String MATCH_IMAGE = "\\[mirai:image:(.*?)]";
public static final String MATCH_MESSAGE = ".*[mirai:image:(https?://[\\w./?&=]+)].*";
public static final String CUSTOM_SPLIT = "\\|";
public static final String ADD_CUSTOM = "/[a-zA-Z]+\\|.*\\|.*";
public static final String MODEL_CHANGE = ".*\\|.*";
}
public static class Order{
public static final String PREFIX_ONECE = ".";
public static final String PREFIX_DEFAULT = "@";
public static final String PREFIX_SET = "$";
public static final String SPLIT_BLANK = " ";
public static final String SPLIT_GUN = "|";
public static final String SPLIT_CUSTOM = "\\|";
public static final String MSG_CLEAR = "clear";
public static final String MSG_CURRENT_MODLE = "当前模型";
public static final String MSG_HELP = "/chatHelp";
public static final String DEFAULT = "default";
public static final String NULL = "null";
}
public static class StatusCode {
public static final int OK = "200";
}
}

View File

@@ -7,6 +7,7 @@ import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.data.At;
import org.jetbrains.annotations.NotNull;
import plugin.constant.ChatConstant;
import plugin.constant.Constant;
import plugin.constant.MethodsConstant;
import plugin.pojo.Config;
import plugin.utils.AIUtil;
@@ -41,9 +42,8 @@ public class GroupMessageListener extends SimpleListenerHost {
String miraiCode = event.getMessage().serializeToMiraiCode();
String url = null;
String chatCommand = null;
if (miraiCode.matches(ChatConstant.MATCH_MESSAGE)) {
String regex = ChatConstant.MATCH_IMAGE;
Pattern pattern = Pattern.compile(regex);
if (miraiCode.matches(Constant.Regex.MATCH_MESSAGE)) {
Pattern pattern = Pattern.compile(Constant.Regex.MATCH_IMAGE);
// 创建Matcher对象
Matcher matcher = pattern.matcher(miraiCode);
@@ -58,18 +58,18 @@ public class GroupMessageListener extends SimpleListenerHost {
MethodsConstant method = MethodsConstant.NONE;
//消息头处理
if (content.startsWith(ChatConstant.ONCE_MESSAGE_START)) {
if (content.startsWith(Constant.Order.PREFIX_ONECE)) {
//单次对话
content = content.substring(1);
method = MethodsConstant.ONCE;
} else if (content.startsWith(ChatConstant.DEFAULT_MESSAGE_START + event.getBot().getId())) {
} else if (content.startsWith(Constant.Order.PREFIX_DEFAULT + event.getBot().getId())) {
//默认对话
content = content.substring((ChatConstant.DEFAULT_MESSAGE_START + event.getBot().getId()).length());
content = content.substring((Constant.Order.PREFIX_DEFAULT + event.getBot().getId()).length());
method = MethodsConstant.NORMAL;
} else if (config.getCustomCommands().containsKey(content.split(ChatConstant.BLANK)[0]+ChatConstant.BLANK)) {
} else if (config.getCustomCommands().containsKey(content.split(Constant.Order.SPLIT_BLANK)[0]+Constant.Order.SPLIT_BLANK)) {
//预设对话
String[] split = content.split(ChatConstant.BLANK);
chatCommand = split[0] + ChatConstant.BLANK;
String[] split = content.split(Constant.Order.SPLIT_BLANK);
chatCommand = split[0] + Constant.Order.SPLIT_BLANK;
method = MethodsConstant.CUSTOM;
content = content.substring(chatCommand.length());
}
@@ -82,7 +82,7 @@ public class GroupMessageListener extends SimpleListenerHost {
case CUSTOM -> AIUtil.customChat(Long.valueOf(id), content, url,chatCommand);
case NORMAL -> AIUtil.defaultChat(Long.valueOf(id), content, url);
case ONCE -> AIUtil.chatOnce(content, url);
default -> "ERROR!";
default -> Constant.Status.ERROR;
};
event.getGroup().sendMessage(new At(Long.parseLong(id)).plus("\r\n").plus(response));
}

View File

@@ -10,6 +10,7 @@ import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.At;
import org.jetbrains.annotations.NotNull;
import plugin.constant.ChatConstant;
import plugin.constant.Constant;
import plugin.constant.ConfigConstant;
import plugin.utils.AIUtil;
import plugin.utils.ConfigUtil;
@@ -40,8 +41,8 @@ public class OwnerMessageListener extends SimpleListenerHost {
private void onGroupMessageEvent(GroupMessageEvent event) {
String msg = event.getMessage().contentToString();
String primaryContent = msg.split(ChatConstant.BLANK)[1];
if (!primaryContent.contains(ChatConstant.SPLIT)) {
String primaryContent = msg.split(Constant.Order.SPLIT_BLANK)[1];
if (!primaryContent.contains(Constant.Order.SPLIT_GUN)) {
String response;
response = switch (primaryContent) {
case "clearAll" -> AIUtil.clearAll();
@@ -53,8 +54,8 @@ public class OwnerMessageListener extends SimpleListenerHost {
return;
}
String command = primaryContent.split(ConfigConstant.CUSTOM_SPLIT)[0];
String arguments = primaryContent.substring(primaryContent.indexOf(ChatConstant.SPLIT) + 1);
String command = primaryContent.split(Constant.Order.SPLIT_CUSTOM)[0];
String arguments = primaryContent.substring(primaryContent.indexOf(Constant.Order.SPLIT_GUN) + 1);
long id = event.getSender().getId();
String response;
try {
@@ -67,12 +68,12 @@ public class OwnerMessageListener extends SimpleListenerHost {
}
private void onFriendMessageEvent(FriendMessageEvent event) throws IOException {
String primaryContent = event.getMessage().contentToString().split(ChatConstant.BLANK)[1];
String primaryContent = event.getMessage().contentToString().split(Constant.Order.SPLIT_BLANK)[1];
String command;
String arguments;
try {
command = primaryContent.split(ConfigConstant.CUSTOM_SPLIT)[0];
arguments = primaryContent.substring(primaryContent.indexOf(ConfigConstant.CUSTOM_SPLIT) + 1);
arguments = primaryContent.substring(primaryContent.indexOf(Constant.Order.SPLIT_CUSTOM) + 1);
} catch (ArrayIndexOutOfBoundsException e) {
event.getFriend().sendMessage("操作失败,缺少参数");
return;