发现了default预设相关问题,将其设置为不可删除、添加了启动时检测该预设是否存在的功能;

将两个owner监听器进行了合并...(instanceof雀食好用)
This commit is contained in:
slhaf
2024-10-06 23:38:30 +08:00
parent 9a2165eb6d
commit e992d32ca9
4 changed files with 62 additions and 31 deletions

View File

@@ -16,7 +16,6 @@ import plugin.utils.ConfigUtil;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; 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); return ((msg.startsWith(".") && msg.length() != 1) || msg.startsWith("@" + bot) || customCommands.containsKey(msg.split(" ")[0] + ChatConstant.BLANK)) && !blacklist.contains(groupId);
}).registerListenerHost(new GroupMessageListener()); }).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) GlobalEventChannel.INSTANCE.filterIsInstance(FriendMessageEvent.class)
.filter(event -> { .filter(event -> {
String msg = event.getMessage().contentToString(); String msg = event.getMessage().contentToString();
String sender = String.valueOf(event.getFriend().getId()); String sender = String.valueOf(event.getFriend().getId());
return !(msg.startsWith(ChatConstant.SET) && sender.equals(owner)) && !msg.equals(ChatConstant.HELP); 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 -> { .filter(event -> {
String msg = event.getMessage().contentToString(); 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); return msg.startsWith(ChatConstant.SET) && sender.equals(owner);
}).registerListenerHost(new OwnerMessageListener()); }).registerListenerHost(new OwnerMessageListener());
@@ -94,7 +84,7 @@ public final class App extends JavaPlugin {
final String[] helpMsg = {""" final String[] helpMsg = {"""
————<群聊命令>———— ————<群聊命令>————
@<bot> <content> @<bot> <content> (仅限群聊)
/<command> <content> /<command> <content>
例: 例:
@@ -103,10 +93,10 @@ public final class App extends JavaPlugin {
————<控制命令>———— ————<控制命令>————
$ clearAll $ clearAll (仅限群聊)
$ shutUp $ shutUp (仅限群聊)
$ speak $ speak (仅限群聊)
$ 添加预设|<预设指令>|<模型名称>|<预设内容> $ 添加预设|<预设指令>|<模型名称/null>|<预设内容>
$ 切换模型|<预设指令>|<模型名称> $ 切换模型|<预设指令>|<模型名称>
$ 更改预设|<预设指令>|<预设内容> $ 更改预设|<预设指令>|<预设内容>
$ 删除预设|<预设指令> $ 删除预设|<预设指令>

View File

@@ -1,10 +1,12 @@
package plugin.listener; package plugin.listener;
import kotlin.coroutines.CoroutineContext; import kotlin.coroutines.CoroutineContext;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.event.EventHandler; import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.SimpleListenerHost; import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.event.events.FriendMessageEvent; import net.mamoe.mirai.event.events.FriendMessageEvent;
import net.mamoe.mirai.event.events.GroupMessageEvent; import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.At; import net.mamoe.mirai.message.data.At;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import plugin.constant.ChatConstant; import plugin.constant.ChatConstant;
@@ -28,7 +30,15 @@ public class OwnerMessageListener extends SimpleListenerHost {
} }
@EventHandler @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 msg = event.getMessage().contentToString();
String primaryContent = msg.split(ChatConstant.BLANK)[1]; String primaryContent = msg.split(ChatConstant.BLANK)[1];
if (!primaryContent.contains(ChatConstant.SPLIT)) { if (!primaryContent.contains(ChatConstant.SPLIT)) {
@@ -44,7 +54,7 @@ public class OwnerMessageListener extends SimpleListenerHost {
} }
String command = primaryContent.split(ConfigConstant.CUSTOM_SPLIT)[0]; 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(); long id = event.getSender().getId();
String response; String response;
try { try {
@@ -56,14 +66,13 @@ public class OwnerMessageListener extends SimpleListenerHost {
event.getGroup().sendMessage(new At(id).plus(response)); event.getGroup().sendMessage(new At(id).plus(response));
} }
@EventHandler private void onFriendMessageEvent(FriendMessageEvent event) throws IOException {
public void onFriendMessageEvent(FriendMessageEvent event) throws IOException {
String primaryContent = event.getMessage().contentToString().split(ChatConstant.BLANK)[1]; String primaryContent = event.getMessage().contentToString().split(ChatConstant.BLANK)[1];
String command; String command;
String arguments; String arguments;
try { try {
command = primaryContent.split(ConfigConstant.CUSTOM_SPLIT)[0]; 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) { } catch (ArrayIndexOutOfBoundsException e) {
event.getFriend().sendMessage("操作失败,缺少参数"); event.getFriend().sendMessage("操作失败,缺少参数");
return; return;
@@ -88,7 +97,7 @@ public class OwnerMessageListener extends SimpleListenerHost {
String customContent = arguments.split(ConfigConstant.CUSTOM_SPLIT)[2]; String customContent = arguments.split(ConfigConstant.CUSTOM_SPLIT)[2];
yield ConfigUtil.addCustom(instruction, customModel, customContent); yield ConfigUtil.addCustom(instruction, customModel, customContent);
} else { } 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 "切换模型" -> { case "切换模型" -> {

View File

@@ -6,6 +6,7 @@ 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;
@@ -74,6 +75,13 @@ public class ConfigUtil {
inputStream.close(); inputStream.close();
logger.info(config.toString()); logger.info(config.toString());
logger.info("读取配置文件完毕"); logger.info("读取配置文件完毕");
HashMap<String, String> 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.AIUtil");
Class.forName("plugin.utils.OCRUtil"); Class.forName("plugin.utils.OCRUtil");
} }
@@ -90,12 +98,18 @@ public class ConfigUtil {
if (!customCommands.containsKey(instruction)) { if (!customCommands.containsKey(instruction)) {
return "该预设不存在!"; return "该预设不存在!";
} }
if (ConfigConstant.DEFAULT.equals(instruction)){
config.setDefaultModel(customModel);
dump();
return "模型切换成功: [defaultModel -> "+config.getDefaultModel()+"]";
}else {
String customContent = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[1]; String customContent = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[1];
customCommands.put(instruction, customModel + "|" + customContent); customCommands.put(instruction, customModel + "|" + customContent);
dump(); dump();
return "模型切换成功: [" + instruction + "->" + customCommands.get(instruction) + "]"; return "模型切换成功: [" + instruction + "->" + customCommands.get(instruction) + "]";
} }
} }
}
private static void dump() throws IOException { private static void dump() throws IOException {
FileWriter writer = new FileWriter(CONFIG_PATH); FileWriter writer = new FileWriter(CONFIG_PATH);
@@ -157,8 +171,12 @@ public class ConfigUtil {
if (!customCommands.containsKey(instruction)) { if (!customCommands.containsKey(instruction)) {
return "该指令不存在!"; return "该指令不存在!";
} }
if (ConfigConstant.DEFAULT.equals(instruction)) {
customCommands.put(instruction, customContent);
}else {
String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0]; String modelName = customCommands.get(instruction).split(ConfigConstant.CUSTOM_SPLIT)[0];
customCommands.put(instruction, modelName + "|" + customContent); customCommands.put(instruction, modelName + "|" + customContent);
}
dump(); dump();
return "预设更改成功! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]"; return "预设更改成功! \r\n[" + instruction + "->" + customCommands.get(instruction) + "]";
} }
@@ -170,6 +188,9 @@ public class ConfigUtil {
if (!customCommands.containsKey(arguments + ChatConstant.BLANK)) { if (!customCommands.containsKey(arguments + ChatConstant.BLANK)) {
return "该预设不存在"; return "该预设不存在";
} }
if (ConfigConstant.DEFAULT.equals(arguments)){
return "默认配置不可删除!";
}
String content = customCommands.get(arguments + ChatConstant.BLANK); String content = customCommands.get(arguments + ChatConstant.BLANK);
customCommands.remove(arguments + ChatConstant.BLANK); customCommands.remove(arguments + ChatConstant.BLANK);
dump(); dump();

View File

@@ -169,6 +169,16 @@ public class MyTest {
System.out.println(str.matches(ConfigConstant.MODEL_CHANGE)); System.out.println(str.matches(ConfigConstant.MODEL_CHANGE));
} }
@Test
public void textBlockTest(){
String str = """
aliyun:
123:
111
""";
System.out.println(str);
}
@Test @Test
public void terminalTest() throws InterruptedException { public void terminalTest() throws InterruptedException {
MiraiConsoleTerminalLoader.INSTANCE.startAsDaemon(new MiraiConsoleImplementationTerminal()); MiraiConsoleTerminalLoader.INSTANCE.startAsDaemon(new MiraiConsoleImplementationTerminal());
@@ -176,4 +186,5 @@ public class MyTest {
PluginManager.INSTANCE.enablePlugin(App.INSTANCE); PluginManager.INSTANCE.enablePlugin(App.INSTANCE);
while (true); while (true);
} }
} }