diff --git a/src/main/java/plugin/App.java b/src/main/java/plugin/App.java index 1c9022e..1290808 100644 --- a/src/main/java/plugin/App.java +++ b/src/main/java/plugin/App.java @@ -16,7 +16,6 @@ import plugin.utils.ConfigUtil; import java.io.IOException; import java.util.HashMap; import java.util.List; -import java.util.function.BiConsumer; /** @@ -60,28 +59,19 @@ public final class App extends JavaPlugin { return ((msg.startsWith(".") && msg.length() != 1) || msg.startsWith("@" + bot) || customCommands.containsKey(msg.split(" ")[0] + ChatConstant.BLANK)) && !blacklist.contains(groupId); }).registerListenerHost(new GroupMessageListener()); - //所有者监听--群聊 - GlobalEventChannel.INSTANCE.filterIsInstance(GroupMessageEvent.class) - .filter(event -> { - String msg = event.getMessage().contentToString(); - String sender = String.valueOf(event.getSender().getId()); - return msg.startsWith(ChatConstant.SET) && sender.equals(owner); - }).registerListenerHost(new OwnerMessageListener()); - //私聊监听器 GlobalEventChannel.INSTANCE.filterIsInstance(FriendMessageEvent.class) .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); - }) - .registerListenerHost(new FriendMessageListener()); + }).registerListenerHost(new FriendMessageListener()); - //所有者监听--私聊 - GlobalEventChannel.INSTANCE.filterIsInstance(FriendMessageEvent.class) + //所有者监听 + GlobalEventChannel.INSTANCE.filterIsInstance(MessageEvent.class) .filter(event -> { String msg = event.getMessage().contentToString(); - String sender = String.valueOf(event.getFriend().getId()); + String sender = String.valueOf(event.getSender().getId()); return msg.startsWith(ChatConstant.SET) && sender.equals(owner); }).registerListenerHost(new OwnerMessageListener()); @@ -94,7 +84,7 @@ public final class App extends JavaPlugin { final String[] helpMsg = {""" ————<群聊命令>———— - @ + @ (仅限群聊) / 例: @@ -103,10 +93,10 @@ public final class App extends JavaPlugin { ————<控制命令>———— - $ clearAll - $ shutUp - $ speak - $ 添加预设|<预设指令>|<模型名称>|<预设内容> + $ clearAll (仅限群聊) + $ shutUp (仅限群聊) + $ speak (仅限群聊) + $ 添加预设|<预设指令>|<模型名称/null>|<预设内容> $ 切换模型|<预设指令>|<模型名称> $ 更改预设|<预设指令>|<预设内容> $ 删除预设|<预设指令> diff --git a/src/main/java/plugin/listener/OwnerMessageListener.java b/src/main/java/plugin/listener/OwnerMessageListener.java index 6c1044f..25d43f7 100644 --- a/src/main/java/plugin/listener/OwnerMessageListener.java +++ b/src/main/java/plugin/listener/OwnerMessageListener.java @@ -1,10 +1,12 @@ package plugin.listener; import kotlin.coroutines.CoroutineContext; +import net.mamoe.mirai.contact.Group; import net.mamoe.mirai.event.EventHandler; import net.mamoe.mirai.event.SimpleListenerHost; import net.mamoe.mirai.event.events.FriendMessageEvent; import net.mamoe.mirai.event.events.GroupMessageEvent; +import net.mamoe.mirai.event.events.MessageEvent; import net.mamoe.mirai.message.data.At; import org.jetbrains.annotations.NotNull; import plugin.constant.ChatConstant; @@ -28,7 +30,15 @@ public class OwnerMessageListener extends SimpleListenerHost { } @EventHandler - public void onGroupMessageEvent(GroupMessageEvent event) { + public void messageClassifier(MessageEvent event) throws IOException { + if (event.getSubject() instanceof Group) { + onGroupMessageEvent((GroupMessageEvent) event); + } else { + onFriendMessageEvent((FriendMessageEvent) event); + } + } + + private void onGroupMessageEvent(GroupMessageEvent event) { String msg = event.getMessage().contentToString(); String primaryContent = msg.split(ChatConstant.BLANK)[1]; if (!primaryContent.contains(ChatConstant.SPLIT)) { @@ -44,7 +54,7 @@ public class OwnerMessageListener extends SimpleListenerHost { } String command = primaryContent.split(ConfigConstant.CUSTOM_SPLIT)[0]; - String arguments = primaryContent.substring(primaryContent.indexOf(ChatConstant.SPLIT)+1); + String arguments = primaryContent.substring(primaryContent.indexOf(ChatConstant.SPLIT) + 1); long id = event.getSender().getId(); String response; try { @@ -56,14 +66,13 @@ public class OwnerMessageListener extends SimpleListenerHost { event.getGroup().sendMessage(new At(id).plus(response)); } - @EventHandler - public void onFriendMessageEvent(FriendMessageEvent event) throws IOException { + private void onFriendMessageEvent(FriendMessageEvent event) throws IOException { String primaryContent = event.getMessage().contentToString().split(ChatConstant.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(ConfigConstant.CUSTOM_SPLIT) + 1); } catch (ArrayIndexOutOfBoundsException e) { event.getFriend().sendMessage("操作失败,缺少参数"); return; @@ -88,7 +97,7 @@ public class OwnerMessageListener extends SimpleListenerHost { String customContent = arguments.split(ConfigConstant.CUSTOM_SPLIT)[2]; yield ConfigUtil.addCustom(instruction, customModel, customContent); } else { - yield "格式不正确! 格式如下: \r\n" + ChatConstant.SET + "命令|预设指令|模型名称|预设\r\n例: \r\n"+ChatConstant.SPLIT+"/example|glm-4-flash|你是...\r\n注:如果不需要预设,可以将预设写为null"; + yield "格式不正确! 格式如下: \r\n" + ChatConstant.SET + "命令|预设指令|模型名称|预设\r\n例: \r\n" + ChatConstant.SPLIT + "/example|glm-4-flash|你是...\r\n注:如果不需要预设,可以将预设写为null"; } } case "切换模型" -> { diff --git a/src/main/java/plugin/utils/ConfigUtil.java b/src/main/java/plugin/utils/ConfigUtil.java index b72ee57..784f3ef 100644 --- a/src/main/java/plugin/utils/ConfigUtil.java +++ b/src/main/java/plugin/utils/ConfigUtil.java @@ -6,6 +6,7 @@ import net.mamoe.mirai.utils.MiraiLogger; import org.slf4j.Logger; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; +import plugin.constant.AIConstant; import plugin.constant.ChatConstant; import plugin.constant.ConfigConstant; import plugin.pojo.Config; @@ -74,6 +75,13 @@ public class ConfigUtil { inputStream.close(); logger.info(config.toString()); logger.info("读取配置文件完毕"); + HashMap customCommands = config.getCustomCommands(); + if (!customCommands.containsKey(ConfigConstant.DEFAULT+ChatConstant.BLANK)){ + logger.warning("未找到default预设!"); + customCommands.put("default ","null"); + dump(); + logger.warning("已自动添加default预设!"); + } Class.forName("plugin.utils.AIUtil"); Class.forName("plugin.utils.OCRUtil"); } @@ -90,10 +98,16 @@ public class ConfigUtil { if (!customCommands.containsKey(instruction)) { return "该预设不存在!"; } - String customContent = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[1]; - customCommands.put(instruction, customModel + "|" + customContent); - dump(); - return "模型切换成功: [" + instruction + "->" + customCommands.get(instruction) + "]"; + if (ConfigConstant.DEFAULT.equals(instruction)){ + config.setDefaultModel(customModel); + dump(); + return "模型切换成功: [defaultModel -> "+config.getDefaultModel()+"]"; + }else { + String customContent = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[1]; + customCommands.put(instruction, customModel + "|" + customContent); + dump(); + return "模型切换成功: [" + instruction + "->" + customCommands.get(instruction) + "]"; + } } } @@ -157,8 +171,12 @@ public class ConfigUtil { if (!customCommands.containsKey(instruction)) { return "该指令不存在!"; } - String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0]; - customCommands.put(instruction, modelName + "|" + customContent); + if (ConfigConstant.DEFAULT.equals(instruction)) { + customCommands.put(instruction, customContent); + }else { + String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0]; + customCommands.put(instruction, modelName + "|" + customContent); + } dump(); return "预设更改成功! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]"; } @@ -170,6 +188,9 @@ public class ConfigUtil { if (!customCommands.containsKey(arguments + ChatConstant.BLANK)) { return "该预设不存在"; } + if (ConfigConstant.DEFAULT.equals(arguments)){ + return "默认配置不可删除!"; + } String content = customCommands.get(arguments + ChatConstant.BLANK); customCommands.remove(arguments + ChatConstant.BLANK); dump(); diff --git a/src/test/java/MyTest.java b/src/test/java/MyTest.java index 413ae2e..e74a383 100644 --- a/src/test/java/MyTest.java +++ b/src/test/java/MyTest.java @@ -169,6 +169,16 @@ public class MyTest { System.out.println(str.matches(ConfigConstant.MODEL_CHANGE)); } + @Test + public void textBlockTest(){ + String str = """ + aliyun: + 123: + 111 + """; + System.out.println(str); + } + @Test public void terminalTest() throws InterruptedException { MiraiConsoleTerminalLoader.INSTANCE.startAsDaemon(new MiraiConsoleImplementationTerminal()); @@ -176,4 +186,5 @@ public class MyTest { PluginManager.INSTANCE.enablePlugin(App.INSTANCE); while (true); } + }