From 9a2165eb6d4fb10aa36e45a4ebb3a671511f254b Mon Sep 17 00:00:00 2001 From: slhaf <2998813882@qq.com> Date: Sun, 6 Oct 2024 22:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=B8=AE=E5=8A=A9?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=EF=BC=88=E6=84=9F=E8=A7=89=E6=B2=A1=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E6=8F=90=E4=BA=A4=E8=BF=99=E4=B8=AA=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/plugin/App.java | 40 ++++++++++++- .../java/plugin/constant/ChatConstant.java | 1 + .../plugin/listener/OwnerMessageListener.java | 6 +- src/main/java/plugin/utils/ConfigUtil.java | 59 +++++++++++-------- 4 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/main/java/plugin/App.java b/src/main/java/plugin/App.java index ee61c5e..1c9022e 100644 --- a/src/main/java/plugin/App.java +++ b/src/main/java/plugin/App.java @@ -5,6 +5,7 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder; import net.mamoe.mirai.event.GlobalEventChannel; 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.listener.FriendMessageListener; import plugin.listener.GroupMessageListener; @@ -15,6 +16,7 @@ import plugin.utils.ConfigUtil; import java.io.IOException; import java.util.HashMap; import java.util.List; +import java.util.function.BiConsumer; /** @@ -71,7 +73,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)); + return !(msg.startsWith(ChatConstant.SET) && sender.equals(owner)) && !msg.equals(ChatConstant.HELP); }) .registerListenerHost(new FriendMessageListener()); @@ -83,6 +85,42 @@ public final class App extends JavaPlugin { return msg.startsWith(ChatConstant.SET) && sender.equals(owner); }).registerListenerHost(new OwnerMessageListener()); + //帮助监听 + GlobalEventChannel.INSTANCE + .filterIsInstance(MessageEvent.class) + .filter(event -> event.getMessage().contentToString().equals(ChatConstant.HELP)) + .subscribeAlways(MessageEvent.class, event -> { + synchronized (customCommands) { + final String[] helpMsg = {""" + ————<群聊命令>———— + + @ + / + + 例: + @机器人 你好 + /c 你好 + + ————<控制命令>———— + + $ clearAll + $ shutUp + $ speak + $ 添加预设|<预设指令>|<模型名称>|<预设内容> + $ 切换模型|<预设指令>|<模型名称> + $ 更改预设|<预设指令>|<预设内容> + $ 删除预设|<预设指令> + + 例: + $ 添加预设|/c|glm-4-flash|你是一只猫娘... + + """}; + helpMsg[0] += "————<预设列表>————"; + customCommands.forEach((s, s2) -> helpMsg[0] += "\r\n\r\n" + s + "-> \r\n" + s2); + event.getSubject().sendMessage(helpMsg[0]); + } + }); + } @Override diff --git a/src/main/java/plugin/constant/ChatConstant.java b/src/main/java/plugin/constant/ChatConstant.java index 992601d..e1d44ba 100644 --- a/src/main/java/plugin/constant/ChatConstant.java +++ b/src/main/java/plugin/constant/ChatConstant.java @@ -42,4 +42,5 @@ public class ChatConstant { */ public static final String CURRENT_MODEL = "当前模型"; public static final String SPLIT = "|"; + public static final String HELP = "/chatHelp"; } diff --git a/src/main/java/plugin/listener/OwnerMessageListener.java b/src/main/java/plugin/listener/OwnerMessageListener.java index c974754..6c1044f 100644 --- a/src/main/java/plugin/listener/OwnerMessageListener.java +++ b/src/main/java/plugin/listener/OwnerMessageListener.java @@ -88,7 +88,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 "切换模型" -> { @@ -97,7 +97,7 @@ public class OwnerMessageListener extends SimpleListenerHost { String modelName = arguments.split(ConfigConstant.CUSTOM_SPLIT)[1]; yield ConfigUtil.customModelChange(instruction, modelName); } else { - yield "格式不正确! 参数格式如下: \r\n" + ChatConstant.SET + "命令|预设指令|指令对应模型"; + yield "格式不正确! 格式如下: \r\n" + ChatConstant.SET + "命令|预设指令|指令对应模型"; } } case "更改预设" -> { @@ -106,7 +106,7 @@ public class OwnerMessageListener extends SimpleListenerHost { String customContent = arguments.split(ConfigConstant.CUSTOM_SPLIT)[1]; yield ConfigUtil.customContentChange(instruction, customContent); } else { - yield "格式不正确! 参数格式如下: \r\n" + ChatConstant.SET + "命令|预设指令|指令对应预设"; + yield "格式不正确! 格式如下: \r\n" + ChatConstant.SET + "命令|预设指令|指令对应预设"; } } case "删除预设" -> ConfigUtil.removeCustom(arguments); diff --git a/src/main/java/plugin/utils/ConfigUtil.java b/src/main/java/plugin/utils/ConfigUtil.java index 4e1b353..b72ee57 100644 --- a/src/main/java/plugin/utils/ConfigUtil.java +++ b/src/main/java/plugin/utils/ConfigUtil.java @@ -61,9 +61,8 @@ public class ConfigUtil { blacklist.add(987654321L); config.setBlacklist(blacklist); LinkedHashMap commands = new LinkedHashMap<>(); - commands.put("default", "null"); + commands.put("default ", "null"); commands.put("/c ", "glm-4-flash|你是一位智能编程助手,你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。 请用中文回答。"); - commands.put("/example ", "模型名称|预设内容"); config.setCustomCommands(commands); dump(); logger.warning("配置文件创建成功,请关闭后进行配置"); @@ -87,13 +86,15 @@ public class ConfigUtil { public static String customModelChange(String instruction, String customModel) throws IOException { HashMap customCommands = config.getCustomCommands(); - if (!customCommands.containsKey(instruction)) { - return "该预设不存在!"; + synchronized (customCommands) { + 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) + "]"; } - String customContent = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[1]; - customCommands.put(instruction, customModel + "|" + customContent); - dump(); - return "模型切换成功: [" + instruction + "->" + customCommands.get(instruction) + "]"; } private static void dump() throws IOException { @@ -132,13 +133,15 @@ public class ConfigUtil { */ public static String addCustom(String instruction, String customModel, String customContent) throws IOException { HashMap customCommands = config.getCustomCommands(); - if (customCommands.containsKey(instruction)) { - return "已存在当前指令! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]"; + synchronized (customCommands) { + if (customCommands.containsKey(instruction)) { + return "已存在当前指令! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]"; + } + String content = customModel + "|" + customContent; + customCommands.put(instruction, content); + dump(); + return "预设添加完毕! \r\n[" + instruction + "->" + content + "]"; } - String content = customModel + "|" + customContent; - customCommands.put(instruction, content); - dump(); - return "预设添加完毕! \r\n[" + instruction + "->" + content + "]"; } /** @@ -150,23 +153,27 @@ public class ConfigUtil { */ public static String customContentChange(String instruction, String customContent) throws IOException { HashMap customCommands = config.getCustomCommands(); - if (!customCommands.containsKey(instruction)) { - return "该指令不存在!"; + synchronized (customCommands) { + if (!customCommands.containsKey(instruction)) { + return "该指令不存在!"; + } + String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0]; + customCommands.put(instruction, modelName + "|" + customContent); + dump(); + return "预设更改成功! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]"; } - String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0]; - customCommands.put(instruction, modelName + "|" + customContent); - dump(); - return "预设更改成功! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]"; } public static String removeCustom(String arguments) throws IOException { HashMap customCommands = getConfig().getCustomCommands(); - if (!customCommands.containsKey(arguments + ChatConstant.BLANK)) { - return "该预设不存在"; + synchronized (customCommands) { + if (!customCommands.containsKey(arguments + ChatConstant.BLANK)) { + return "该预设不存在"; + } + String content = customCommands.get(arguments + ChatConstant.BLANK); + customCommands.remove(arguments + ChatConstant.BLANK); + dump(); + return "删除预设[" + arguments + "->" + content + "]成功"; } - String content = customCommands.get(arguments + ChatConstant.BLANK); - customCommands.remove(arguments + ChatConstant.BLANK); - dump(); - return "删除预设[" + arguments + "->" + content + "]成功"; } }