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

修复了截取消息时出现的错误
This commit is contained in:
slhaf
2024-10-27 12:49:31 +08:00
parent e992d32ca9
commit 3d814547f0
8 changed files with 23 additions and 20 deletions

View File

@@ -19,7 +19,7 @@ dependencies{
implementation ("org.apache.logging.log4j:log4j-core:2.23.1") implementation ("org.apache.logging.log4j:log4j-core:2.23.1")
implementation ("org.apache.logging.log4j:log4j-api:2.23.1") implementation ("org.apache.logging.log4j:log4j-api:2.23.1")
implementation("top.mrxiaom.mirai:overflow-core:0.9.9.515-f8d867b-SNAPSHOT") implementation("top.mrxiaom.mirai:overflow-core:0.9.9.515-f8d867b-SNAPSHOT")
implementation ("com.alibaba:fastjson:1.2.73")
} }

View File

@@ -4,4 +4,4 @@ pluginManagement {
gradlePluginPortal() gradlePluginPortal()
} }
} }
rootProject.name = "ChatAI-InGroup-v2" rootProject.name = "ChatAI-InGroup"

View File

@@ -25,8 +25,8 @@ public final class App extends JavaPlugin {
public static final App INSTANCE = new App(); public static final App INSTANCE = new App();
private App() { private App() {
super(new JvmPluginDescriptionBuilder("com.plugin.chatAI-InGroup-v2", "0.1.0") super(new JvmPluginDescriptionBuilder("com.plugin.chatAI-InGroup", "0.1.0")
.name("ChatAI-InGroup-v2") .name("ChatAI-InGroup")
.author("SLHAF") .author("SLHAF")
.build()); .build());
} }
@@ -49,7 +49,7 @@ public final class App extends JavaPlugin {
} catch (IOException | ClassNotFoundException | InterruptedException e) { } catch (IOException | ClassNotFoundException | InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
getLogger().info("ChatAI-InGroup-v2 loaded!"); getLogger().info("ChatAI-InGroup loaded!");
//群聊监听器 //群聊监听器
GlobalEventChannel.INSTANCE.filterIsInstance(GroupMessageEvent.class) GlobalEventChannel.INSTANCE.filterIsInstance(GroupMessageEvent.class)
@@ -115,6 +115,6 @@ public final class App extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
getLogger().info("ChatAI-InGroup-v2 disabled!"); getLogger().info("ChatAI-InGroup disabled!");
} }
} }

View File

@@ -43,7 +43,7 @@ public class FriendMessageListener extends SimpleListenerHost {
String[] split = content.split(ChatConstant.BLANK); String[] split = content.split(ChatConstant.BLANK);
chatCommand = split[0] + ChatConstant.BLANK; chatCommand = split[0] + ChatConstant.BLANK;
method = MethodsConstant.CUSTOM; method = MethodsConstant.CUSTOM;
content = split[1]; content = content.substring((content.split(ChatConstant.BLANK)[0]+ChatConstant.BLANK).length());
} else if (content.startsWith(ChatConstant.ONCE_MESSAGE_START)) { } else if (content.startsWith(ChatConstant.ONCE_MESSAGE_START)) {
content = content.substring(1); content = content.substring(1);
method = MethodsConstant.ONCE; method = MethodsConstant.ONCE;

View File

@@ -71,7 +71,7 @@ public class GroupMessageListener extends SimpleListenerHost {
String[] split = content.split(ChatConstant.BLANK); String[] split = content.split(ChatConstant.BLANK);
chatCommand = split[0] + ChatConstant.BLANK; chatCommand = split[0] + ChatConstant.BLANK;
method = MethodsConstant.CUSTOM; method = MethodsConstant.CUSTOM;
content = split[1]; content = content.substring(chatCommand.length());
} }
//消息内容处理 //消息内容处理
if (content.isBlank()) { if (content.isBlank()) {

View File

@@ -107,11 +107,14 @@ public class AIUtil {
if (!userCustomMessages.containsKey(id)) { if (!userCustomMessages.containsKey(id)) {
return "不存在消息记录"; return "不存在消息记录";
} }
for (UserCustomMessage userCustomMessage : userCustomMessages.get(id)) { ArrayList<Integer> indexToRemove = new ArrayList<>();
if (userCustomMessage.getCommand().equals(chatCommand)) { List<UserCustomMessage> userCustomMessageList = userCustomMessages.get(id);
userCustomMessage.getMessages().clear(); for (int i = 0; i < userCustomMessageList.size(); i++) {
if (userCustomMessageList.get(i).getCommand().equals(chatCommand)) {
indexToRemove.add(i);
} }
} }
indexToRemove.forEach(i -> userCustomMessageList.remove(i.intValue()));
return "消息记录已清空"; return "消息记录已清空";
} else if (AIConstant.CURRENT_MODEL.equals(content.replace(ChatConstant.BLANK, ""))) { } else if (AIConstant.CURRENT_MODEL.equals(content.replace(ChatConstant.BLANK, ""))) {
String modelName = customCommands.get(chatCommand).split(ConfigConstant.CUSTOM_SPLIT)[0]; String modelName = customCommands.get(chatCommand).split(ConfigConstant.CUSTOM_SPLIT)[0];
@@ -173,8 +176,8 @@ public class AIUtil {
if (!userDefaultMessages.containsKey(id)) { if (!userDefaultMessages.containsKey(id)) {
//创建消息list //创建消息list
List<ChatMessage> chatMessages = new ArrayList<>(); List<ChatMessage> chatMessages = new ArrayList<>();
if (customCommands.containsKey(ConfigConstant.DEFAULT) && !ConfigConstant.NULL.equals(customCommands.get(ConfigConstant.DEFAULT))) { if (customCommands.containsKey(ConfigConstant.DEFAULT+ChatConstant.BLANK) && !ConfigConstant.NULL.equals(customCommands.get(ConfigConstant.DEFAULT+ChatConstant.BLANK))) {
ChatMessage customMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), customCommands.get(ConfigConstant.DEFAULT)); ChatMessage customMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), customCommands.get(ConfigConstant.DEFAULT+ChatConstant.BLANK));
chatMessages.add(customMessage); chatMessages.add(customMessage);
} }
userDefaultMessages.put(id, chatMessages); userDefaultMessages.put(id, chatMessages);

View File

@@ -6,7 +6,6 @@ import net.mamoe.mirai.utils.MiraiLogger;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import plugin.constant.AIConstant;
import plugin.constant.ChatConstant; import plugin.constant.ChatConstant;
import plugin.constant.ConfigConstant; import plugin.constant.ConfigConstant;
import plugin.pojo.Config; import plugin.pojo.Config;
@@ -98,7 +97,7 @@ public class ConfigUtil {
if (!customCommands.containsKey(instruction)) { if (!customCommands.containsKey(instruction)) {
return "该预设不存在!"; return "该预设不存在!";
} }
if (ConfigConstant.DEFAULT.equals(instruction)){ if ((ConfigConstant.DEFAULT+ChatConstant.BLANK).equals(instruction)){
config.setDefaultModel(customModel); config.setDefaultModel(customModel);
dump(); dump();
return "模型切换成功: [defaultModel -> "+config.getDefaultModel()+"]"; return "模型切换成功: [defaultModel -> "+config.getDefaultModel()+"]";
@@ -171,7 +170,7 @@ public class ConfigUtil {
if (!customCommands.containsKey(instruction)) { if (!customCommands.containsKey(instruction)) {
return "该指令不存在!"; return "该指令不存在!";
} }
if (ConfigConstant.DEFAULT.equals(instruction)) { if ((ConfigConstant.DEFAULT+ChatConstant.BLANK).equals(instruction)) {
customCommands.put(instruction, customContent); customCommands.put(instruction, customContent);
}else { }else {
String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0]; String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0];

View File

@@ -23,6 +23,7 @@ import plugin.utils.ConfigUtil;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Scanner;
import static kotlin.io.ConsoleKt.readln; import static kotlin.io.ConsoleKt.readln;
@@ -55,7 +56,7 @@ public class MyTest {
HttpPost httpPost = new HttpPost("https://open.bigmodel.cn/api/paas/v4/assistant"); HttpPost httpPost = new HttpPost("https://open.bigmodel.cn/api/paas/v4/assistant");
httpPost.setHeaders(new Header[]{ httpPost.setHeaders(new Header[]{
new BasicHeader("content-type", "application/json"), new BasicHeader("content-type", "application/json"),
new BasicHeader("authorization", "") new BasicHeader("authorization", "f638f59b6b5961bbcd685c85caa0e7e7.Uz5nTEQS2X8LXcxF")
}); });
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
@@ -181,10 +182,10 @@ public class MyTest {
@Test @Test
public void terminalTest() throws InterruptedException { public void terminalTest() throws InterruptedException {
MiraiConsoleTerminalLoader.INSTANCE.startAsDaemon(new MiraiConsoleImplementationTerminal()); MiraiConsoleImplementationTerminal terminal = new MiraiConsoleImplementationTerminal();
MiraiConsoleTerminalLoader.INSTANCE.startAsDaemon(terminal);
PluginManager.INSTANCE.loadPlugin(App.INSTANCE); PluginManager.INSTANCE.loadPlugin(App.INSTANCE);
PluginManager.INSTANCE.enablePlugin(App.INSTANCE); PluginManager.INSTANCE.enablePlugin(App.INSTANCE);
while (true); new Scanner(System.in).nextLine();
} }
} }