Files
ChatAI-InGroup/src/main/java/plugin/App.java
slhaf dba16e99c2 修改为OpenAI规范
去除了单次对话
配置文件格式修改
待测试
2024-12-07 15:08:56 +08:00

142 lines
6.4 KiB
Java

package plugin;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
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.chat.constant.Constant;
import plugin.config.Config;
import plugin.config.CustomCommandTemplate;
import plugin.listener.FriendMessageListener;
import plugin.listener.GroupMessageListener;
import plugin.listener.OwnerMessageListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author SLHAF
*/
public final class App extends JavaPlugin {
public static final App INSTANCE = new App();
private App() {
super(new JvmPluginDescriptionBuilder("com.plugin.chatAI-InGroup", "0.1.0")
.name("ChatAI-InGroup")
.author("SLHAF")
.build());
}
@Override
public void onEnable() {
String owner, bot;
ArrayList<CustomCommandTemplate> customCommands;
List<Long> blacklist;
//加载配置
try {
Config config = Config.ConfigLoader.load();
App.class.getClassLoader().loadClass("plugin.utils.OCRUtil");
App.class.getClassLoader().loadClass("plugin.utils.ChatUtil");
owner = config.getOwner().substring(1);
bot = config.getBot().substring(1);
customCommands = config.getCustomCommandTemplates();
blacklist = config.getBlacklist();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
getLogger().info("ChatAI loaded!");
//群聊监听器
GlobalEventChannel.INSTANCE.filterIsInstance(GroupMessageEvent.class)
.filter(event -> {
String msg = event.getMessage().contentToString();
long groupId = event.getGroup().getId();
String prefix = msg.split(Constant.Order.SPLIT_BLANK)[0];
return (msg.startsWith(Constant.Order.PREFIX_DEFAULT + bot) || checkCommandExist(prefix,customCommands)) && !blacklist.contains(groupId);
}).registerListenerHost(new GroupMessageListener());
//私聊监听器
GlobalEventChannel.INSTANCE.filterIsInstance(FriendMessageEvent.class)
.filter(event -> {
String msg = event.getMessage().contentToString();
String sender = String.valueOf(event.getFriend().getId());
String prefix = msg.split(Constant.Order.SPLIT_BLANK)[0];
// return !(msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) && !msg.equals(Constant.Order.MSG_HELP);
return checkCommandExist(prefix,customCommands) //包含指令前缀
|| !((msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner)) || msg.startsWith(Constant.Order.PREFIX_CUSTOM)); //如果不包含指令前缀,则不能以设置指令、"/"开头
}).registerListenerHost(new FriendMessageListener());
//所有者监听
GlobalEventChannel.INSTANCE.filterIsInstance(MessageEvent.class)
.filter(event -> {
String msg = event.getMessage().contentToString();
String sender = String.valueOf(event.getSender().getId());
return msg.startsWith(Constant.Order.PREFIX_SET) && sender.equals(owner);
}).registerListenerHost(new OwnerMessageListener());
//帮助监听
GlobalEventChannel.INSTANCE
.filterIsInstance(MessageEvent.class)
.filter(event -> event.getMessage().contentToString().equals(Constant.Order.MSG_HELP))
.subscribeAlways(MessageEvent.class, event -> {
synchronized (customCommands) {
final String[] helpMsg = {"""
————<群聊命令>————
@<bot> <content> (仅限群聊)
/<command> <content>
例:
@机器人 你好
/c 你好
————<控制命令>————
$ clearAll (仅限群聊)
$ shutUp (仅限群聊)
$ speak (仅限群聊)
$ 添加预设|<预设指令>|<模型模板ID>|<模型名称>|<预设内容>
$ 切换模型|<预设指令>|<模型模板ID>|<模型名称>
$ 更改预设|<预设指令>|<预设内容>
$ 删除预设|<预设指令>
例:
$ 添加预设|/c|<模型模板ID>|glm-4-flash|你是一只猫娘...
"""};
helpMsg[0] += "————<预设列表>————";
for (CustomCommandTemplate customCommand : customCommands) {
helpMsg[0] += "\r\n\r\n"+customCommand.toString();
}
helpMsg[0] += "\r\n";
helpMsg[0] += "————<模型列表>————";
for (int i = 0; i < Config.ConfigLoader.getConfig().getModelConfigTemplates().size(); i++) {
helpMsg[0] += "\r\n\r\n"+"TemplateIndex: "+i+"\r\n"+Config.ConfigLoader.getConfig().getModelConfigTemplates().get(i).toString();
}
event.getSubject().sendMessage(helpMsg[0]);
}
});
}
private boolean checkCommandExist(String prefix, ArrayList<CustomCommandTemplate> customCommands) {
for (CustomCommandTemplate customCommand : customCommands) {
if (customCommand.getCommand().equals(prefix)) {
return true;
}
}
return false;
}
@Override
public void onDisable() {
getLogger().info("ChatAI-InGroup disabled!");
}
}