From 60f49d5f38583974b85ab07326cc74115dcfe494 Mon Sep 17 00:00:00 2001 From: Dituon <2544193782@qq.com> Date: Sun, 7 Aug 2022 20:46:11 +0800 Subject: [PATCH] =?UTF-8?q?4.2/=E9=87=8D=E5=86=99=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=99=A8/=E6=9A=82=E6=97=B6=E7=A7=BB?= =?UTF-8?q?=E9=99=A4strictCommand=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 ++-- build.gradle | 2 +- config/xmmt.dituon.petpet/PetPet.yml | 1 - index.json | 2 +- .../java/xmmt/dituon/plugin/DataUpdater.java | 14 +- src/main/java/xmmt/dituon/plugin/Petpet.java | 138 +++++++----------- .../java/xmmt/dituon/plugin/PluginConfig.kt | 2 +- .../xmmt/dituon/plugin/PluginPetService.java | 2 +- 8 files changed, 83 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 4389d177..85175caf 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ java 编写,**未使用任何第三方库** :轻量,高效。 5. 使用 **戳一戳** 有 `30%` 的概率触发; 或发送 `pet @xxx` -> `key @xxx` 可返回指定图片 例如 `kiss @xxx` +> `pet key @xxx` 或 `key @xxx` 可返回指定图片 例如 `pet kiss @xxx` `kiss @xxx` -> 可通过发送的图片生成Petpet `kiss [图片]` +> 可通过发送的图片生成Petpet `kiss [图片]`, **支持GIF** > 可使用 `pet`指令 获取 `keyList` @@ -52,7 +52,6 @@ content: respondSelfNudge: false #响应机器人发出的戳一戳 keyListFormat: FORWARD #keyList响应格式 fuzzy: false #模糊匹配用户名 - strictCommand: true #严格匹配模式 synchronized: false #消息事件同步锁 headless: true #使用headless模式 @@ -136,14 +135,20 @@ content: > 例 (配置项为`true`时): `kiss @田所浩二`(响应) `kiss 浩二`(响应)
-- **strictCommand**: `true` +[//]: # (- **strictCommand**: `true`) -> 严格匹配指令, 默认为`true` -> -> ~~人话: 可以省略key后的空格~~ -> -> 例 (配置项为`false`时): `kiss 田所`(响应) `kiss田所`(响应) -
+[//]: # () +[//]: # (> 严格匹配指令, 默认为`true`) + +[//]: # (> ) + +[//]: # (> ~~人话: 可以省略key后的空格~~) + +[//]: # (> ) + +[//]: # (> 例 (配置项为`false`时): `kiss 田所`(响应) `kiss田所`(响应)) + +[//]: # (
) - **synchronized**: `false` @@ -465,6 +470,9 @@ content: - 自动更新下载速度慢? > 修改`Petpet.yml`中`repositoryUrl`的值为`'https://ghproxy.com/https://raw.githubusercontent.com/Dituon/petpet/main'`(高速镜像) +- 自动更新后 读取`data.json`出错? + > 自动更新时网络出错导致, 删除出错的文件 重新获取即可 + ## 分享你的作品 如果你想分享自定义的 Petpet, **欢迎Pr** diff --git a/build.gradle b/build.gradle index c6775368..6e7a3343 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ apply plugin: "java" group = 'xmmt.dituon' -version = '4.1-M2' +version = '4.2' repositories { maven { url 'https://maven.aliyun.com/repository/public' } diff --git a/config/xmmt.dituon.petpet/PetPet.yml b/config/xmmt.dituon.petpet/PetPet.yml index bece0665..2ceb5724 100644 --- a/config/xmmt.dituon.petpet/PetPet.yml +++ b/config/xmmt.dituon.petpet/PetPet.yml @@ -9,7 +9,6 @@ content: cachePoolSize: 10000 keyListFormat: FORWARD fuzzy: false - strictCommand: true synchronized: false headless: true autoUpdate: true diff --git a/index.json b/index.json index 91930bd7..a03f9dc5 100644 --- a/index.json +++ b/index.json @@ -1,5 +1,5 @@ { - "version": 4.1, + "version": 4.2, "dataList": [ "key_list", "up_symmetry", diff --git a/src/main/java/xmmt/dituon/plugin/DataUpdater.java b/src/main/java/xmmt/dituon/plugin/DataUpdater.java index 03468355..81a6753b 100644 --- a/src/main/java/xmmt/dituon/plugin/DataUpdater.java +++ b/src/main/java/xmmt/dituon/plugin/DataUpdater.java @@ -24,11 +24,11 @@ public static void autoUpdate() { public static void updateData() { System.out.println("开始更新PetData"); UpdateIndex index = UpdateIndex.getUpdate( - Objects.requireNonNull(getUrlText(Petpet.pluginPetService.repositoryUrl + "/index.json"))); + Objects.requireNonNull(getUrlText(Petpet.service.repositoryUrl + "/index.json"))); List newPetList = index.getDataList(); for (String pet : newPetList) { - if (Petpet.pluginPetService.getDataMap().containsKey(pet) - || Petpet.pluginPetService.updateIgnore.contains(pet)) continue; + if (Petpet.service.getDataMap().containsKey(pet) + || Petpet.service.updateIgnore.contains(pet)) continue; String petDataPath = "/data/xmmt.dituon.petpet/" + pet; if (!saveAs(petDataPath, "data.json")) { System.out.println("无法从远程仓库下载PetData: " + petDataPath); @@ -55,16 +55,16 @@ public static void updateData() { } System.out.println("PetData更新完毕, 正在重新加载"); - Petpet.pluginPetService.readData(Petpet.dataFolder); + Petpet.service.readData(Petpet.dataFolder); } public static boolean checkUpdate() { UpdateIndex update = UpdateIndex.getUpdate( - Objects.requireNonNull(getUrlText(Petpet.pluginPetService.repositoryUrl + "/index.json"))); + Objects.requireNonNull(getUrlText(Petpet.service.repositoryUrl + "/index.json"))); if (Petpet.VERSION != update.getVersion()) System.out.println("PetpetPlugin可更新到最新版本: " + update.getVersion() + " (当前版本 " + Petpet.VERSION + ")"); for (String pet : update.getDataList()) { - if (Petpet.pluginPetService.getDataMap().containsKey(pet)) continue; + if (Petpet.service.getDataMap().containsKey(pet)) continue; System.out.println("发现新增PetData"); return false; } @@ -89,7 +89,7 @@ private static String getUrlText(String url) { } private static boolean saveAs(String path, String fileName) { - try (InputStream ins = new URL(Petpet.pluginPetService.repositoryUrl + path + '/' + fileName).openStream()) { + try (InputStream ins = new URL(Petpet.service.repositoryUrl + path + '/' + fileName).openStream()) { Path target = Paths.get(new File(".").getCanonicalPath() + path, fileName); Files.createDirectories(target.getParent()); Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING); diff --git a/src/main/java/xmmt/dituon/plugin/Petpet.java b/src/main/java/xmmt/dituon/plugin/Petpet.java index c3833053..016c9709 100644 --- a/src/main/java/xmmt/dituon/plugin/Petpet.java +++ b/src/main/java/xmmt/dituon/plugin/Petpet.java @@ -17,10 +17,10 @@ public final class Petpet extends JavaPlugin { public static final Petpet INSTANCE = new Petpet(); - public static final float VERSION = 4.1F; + public static final float VERSION = 4.2F; private static final ArrayList disabledGroup = new ArrayList<>(); - public static PluginPetService pluginPetService; + public static PluginPetService service; public static File dataFolder; private static MessageSource previousMessage; @@ -33,36 +33,36 @@ private Petpet() { .name("PetPet") .author("Dituon") .build()); - pluginPetService = new PluginPetService(); + service = new PluginPetService(); } @Override public void onEnable() { try { this.reloadPluginConfig(PetPetAutoSaveConfig.INSTANCE); - pluginPetService.readConfigByPluginAutoSave(); + service.readConfigByPluginAutoSave(); } catch (NoClassDefFoundError ignored) { getLogger().error("Mirai 2.11.0 提供了新的 JavaAutoSaveConfig 方法, 请更新Mirai版本至 2.11.0 (不是2.11.0-M1)\n使用旧版本将无法配置config"); } dataFolder = getDataFolder(); - pluginPetService.readData(dataFolder); + service.readData(dataFolder); - if (pluginPetService.headless) System.setProperty("java.awt.headless", "true"); - if (pluginPetService.autoUpdate) new Thread(DataUpdater::autoUpdate).start(); + if (service.headless) System.setProperty("java.awt.headless", "true"); + if (service.autoUpdate) new Thread(DataUpdater::autoUpdate).start(); getLogger().info("\n _ _ \n _ __ ___| |_ _ __ ___| |_ \n" + " | '_ \\ / _ \\ __| | '_ \\ / _ \\ __|\n | |_) | __/ |_ | |_) | __/ |_ \n" + " | .__/ \\___|\\__| | .__/ \\___|\\__|\n |_| |_| v" + VERSION); GlobalEventChannel.INSTANCE.subscribeAlways(NudgeEvent.class, - pluginPetService.messageSynchronized ? this::onNudgeSynchronized : this::onNudge); + service.messageSynchronized ? this::onNudgeSynchronized : this::onNudge); GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, - pluginPetService.messageSynchronized ? this::onGroupMessageSynchronized : this::onGroupMessage); - if (pluginPetService.respondReply) { + service.messageSynchronized ? this::onGroupMessageSynchronized : this::onGroupMessage); + if (service.respondReply) { imageCachePool = new HashMap<>(); GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, this::cacheMessageImage); -// GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessagePostSendEvent.class, this::cacheMessageImage); + GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessagePostSendEvent.class, this::cacheMessageImage); } } @@ -82,13 +82,13 @@ private void responseNudge(NudgeEvent e) { // 如果禁用了petpet就返回 if (!(e.getSubject() instanceof Group) || isDisabled((Group) e.getSubject())) return; try { - pluginPetService.sendImage((Group) e.getSubject(), (Member) e.getFrom(), (Member) e.getTarget(), true); + service.sendImage((Group) e.getSubject(), (Member) e.getFrom(), (Member) e.getTarget(), true); } catch (Exception ex) { // 如果无法把被戳的对象转换为Member(只有Bot无法强制转换为Member对象) try { - pluginPetService.sendImage((Group) e.getSubject(), ((Group) e.getSubject()).getBotAsMember(), (Member) e.getFrom(), true); + service.sendImage((Group) e.getSubject(), ((Group) e.getSubject()).getBotAsMember(), (Member) e.getFrom(), true); } catch (Exception ignored) { // 如果bot戳了别人 - if (!pluginPetService.respondSelfNudge) return; - pluginPetService.sendImage((Group) e.getSubject(), ((Group) e.getSubject()).getBotAsMember(), (Member) e.getFrom(), true); + if (!service.respondSelfNudge) return; + service.sendImage((Group) e.getSubject(), ((Group) e.getSubject()).getBotAsMember(), (Member) e.getFrom(), true); } } } @@ -114,40 +114,40 @@ private void responseMessage(GroupMessageEvent e) { String messageString = e.getMessage().contentToString().trim(); - if (messageString.equals(pluginPetService.command + " off") && + if (messageString.equals(service.command + " off") && !isDisabled(e.getGroup()) && isPermission(e)) { disabledGroup.add(e.getGroup()); - sendReplyMessage(e, "已禁用 " + pluginPetService.command); + sendReplyMessage(e, "已禁用 " + service.command); return; } - if (messageString.equals(pluginPetService.command + " on") && + if (messageString.equals(service.command + " on") && isDisabled(e.getGroup()) && isPermission(e)) { disabledGroup.remove(e.getGroup()); - sendReplyMessage(e, "已启用 " + pluginPetService.command); + sendReplyMessage(e, "已启用 " + service.command); return; } - if (messageString.equals(pluginPetService.command)) { - switch (pluginPetService.replyFormat) { + if (messageString.equals(service.command)) { + switch (service.replyFormat) { case MESSAGE: - e.getGroup().sendMessage("Petpet KeyList: \n" + pluginPetService.getKeyAliasListString()); + e.getGroup().sendMessage("Petpet KeyList: \n" + service.getKeyAliasListString()); break; case FORWARD: ForwardMessageBuilder builder = new ForwardMessageBuilder(e.getGroup()); builder.add(e.getBot().getId(), "petpet!", - new PlainText("Petpet KeyList: \n" + pluginPetService.getKeyAliasListString())); + new PlainText("Petpet KeyList: \n" + service.getKeyAliasListString())); e.getGroup().sendMessage(builder.build()); break; case IMAGE: - if (pluginPetService.getDataMap().get("key_list") == null) { + if (service.getDataMap().get("key_list") == null) { getLogger().error("未找到PetData/key_list, 无法进行图片构造"); - e.getGroup().sendMessage("[ERROR]未找到PetData/key_list\n" + pluginPetService.getKeyAliasListString()); + e.getGroup().sendMessage("[ERROR]未找到PetData/key_list\n" + service.getKeyAliasListString()); return; } List keyList = new ArrayList<>(); - keyList.add(pluginPetService.getKeyAliasListString()); - pluginPetService.sendImage(e.getGroup(), "key_list", + keyList.add(service.getKeyAliasListString()); + service.sendImage(e.getGroup(), "key_list", BaseConfigFactory.getGifAvatarExtraDataFromUrls(null, null, null, null), new TextExtraData("", "", "", keyList)); break; @@ -155,47 +155,7 @@ private void responseMessage(GroupMessageEvent e) { return; } - if (messageString.startsWith(pluginPetService.command)) { //pet xxx key xxx - messageString = messageString.substring(pluginPetService.command.length()).trim(); - boolean ignore = true; - for (String commandKey : pluginPetService.getDataMap().keySet()) { - if (messageString.contains(commandKey)) { - messageString = commandKey + messageString.replace(commandKey, ""); - ignore = false; - } - } - if (ignore) for (String alia : pluginPetService.getAliaMap().keySet()) { - if (messageString.contains(alia)) messageString = alia + messageString.replace(alia, ""); - } - } - - boolean ignore = true; - String key = //解析后的key(随机初始值) - pluginPetService.randomableList.get(new Random().nextInt(pluginPetService.randomableList.size())); - String originKey = null; //解析前的key(可能为alia), 包含keyCommandHead - for (String commandKey : pluginPetService.getDataMap().keySet()) { //key - if (messageString.startsWith(pluginPetService.commandHead + commandKey - + (pluginPetService.strictCommand ? ' ' : "")) || - messageString.equals(pluginPetService.commandHead + commandKey)) { - originKey = commandKey; - key = commandKey.replace(pluginPetService.commandHead, ""); - ignore = false; - break; - } - } - if (ignore) for (String alia : pluginPetService.getAliaMap().keySet()) { //别名 - if (messageString.startsWith(pluginPetService.commandHead + alia - + (pluginPetService.strictCommand ? ' ' : "")) || - messageString.equals(pluginPetService.commandHead + alia)) { - originKey = alia; - String[] randomArray = pluginPetService.getAliaMap().get( - alia.substring(pluginPetService.commandHead.length())); - key = randomArray[new Random().nextInt(randomArray.length)]; - ignore = false; - break; - } - } - if (ignore) return; + String key = null; boolean fuzzyLock = false; //锁住模糊匹配 @@ -207,17 +167,13 @@ private void responseMessage(GroupMessageEvent e) { String fromUrl = e.getBot().getAvatarUrl(); String toUrl = e.getSender().getAvatarUrl(); for (SingleMessage singleMessage : e.getMessage()) { - if (singleMessage instanceof QuoteReply && pluginPetService.respondReply) { + if (singleMessage instanceof QuoteReply && service.respondReply) { long id = e.getGroup().getId() + ((QuoteReply) singleMessage).getSource().getIds()[0]; toUrl = imageCachePool.get(id) != null ? imageCachePool.get(id) : toUrl; continue; } if (singleMessage instanceof PlainText) { String text = singleMessage.contentToString(); - //过滤原始文本 只留下data - if (text.startsWith(pluginPetService.commandHead + pluginPetService.command)) - text = text.substring(pluginPetService.commandHead.length() + pluginPetService.command.length()).trim(); - if (text.startsWith(originKey)) text = text.substring(originKey.length()); messageText.append(text).append(' '); continue; } @@ -242,14 +198,28 @@ private void responseMessage(GroupMessageEvent e) { } String commandData = messageText.toString().trim(); + ArrayList spanList = new ArrayList<>(Arrays.asList(commandData.trim().split("\\s+"))); + if (spanList.isEmpty()) return; + + if (service.command.equals(spanList.get(0))) { + spanList.remove(0); //去掉指令头 + key = service.randomableList.get(new Random().nextInt(service.randomableList.size())); //随机key + } + + if (!spanList.isEmpty()) { + if (service.getDataMap().containsKey(spanList.get(0))) key = spanList.get(0); //key + else if (service.getAliaMap().containsKey(spanList.get(0))) { //别名 + String[] keys = service.getAliaMap().get(spanList.get(0)); + key = keys[new Random().nextInt(keys.length)]; + } + } - List strList = "".equals(commandData) ? new ArrayList<>() : - new ArrayList<>(Arrays.asList(commandData.trim().split("\\s+"))); //空格分割数据 + if (key == null) return; - if (pluginPetService.fuzzy && !strList.isEmpty() && !fuzzyLock) { + if (service.fuzzy && !spanList.isEmpty() && !fuzzyLock) { for (Member m : e.getGroup().getMembers()) { - if (m.getNameCard().toLowerCase().contains(strList.get(0).toLowerCase()) - || m.getNick().toLowerCase().contains(strList.get(0).toLowerCase())) { + if (m.getNameCard().toLowerCase().contains(spanList.get(0).toLowerCase()) + || m.getNick().toLowerCase().contains(spanList.get(0).toLowerCase())) { fromName = getNameOrNick(e.getSender()); fromUrl = e.getSender().getAvatarUrl(); toName = getNameOrNick(m); @@ -258,18 +228,18 @@ private void responseMessage(GroupMessageEvent e) { } } - pluginPetService.sendImage(e.getGroup(), key, + service.sendImage(e.getGroup(), key, BaseConfigFactory.getGifAvatarExtraDataFromUrls( fromUrl, toUrl, e.getGroup().getAvatarUrl(), e.getBot().getAvatarUrl() ), new TextExtraData( - fromName, toName, groupName, strList + fromName, toName, groupName, spanList )); } private void cacheMessageImage(GroupMessageEvent e) { for (SingleMessage singleMessage : e.getMessage()) { if (singleMessage instanceof Image) { - if (imageCachePool.size() >= pluginPetService.cachePoolSize) imageCachePool.clear(); + if (imageCachePool.size() >= service.cachePoolSize) imageCachePool.clear(); long id = e.getGroup().getId() + e.getMessage().get(MessageSource.Key).getIds()[0]; imageCachePool.put(id, Image.queryUrl((Image) singleMessage)); return; @@ -277,11 +247,11 @@ private void cacheMessageImage(GroupMessageEvent e) { } } - /* + private void cacheMessageImage(GroupMessagePostSendEvent e) { for (SingleMessage singleMessage : e.getMessage()) { if (singleMessage instanceof Image) { - if (imageCachePool.size() >= pluginPetService.cachePoolSize) imageCachePool.clear(); + if (imageCachePool.size() >= service.cachePoolSize) imageCachePool.clear(); //GroupMessagePostSendEvent获取的MessageChain不包含MessageSource long id = e.getTarget().getId() + e.getMessage().get(MessageSource.Key).getIds()[0]; imageCachePool.put(id, Image.queryUrl((Image) singleMessage)); @@ -289,7 +259,7 @@ private void cacheMessageImage(GroupMessagePostSendEvent e) { } } } - */ + private boolean isDisabled(Group group) { if (disabledGroup != null && !disabledGroup.isEmpty()) { diff --git a/src/main/java/xmmt/dituon/plugin/PluginConfig.kt b/src/main/java/xmmt/dituon/plugin/PluginConfig.kt index 208b1075..bdac8df5 100644 --- a/src/main/java/xmmt/dituon/plugin/PluginConfig.kt +++ b/src/main/java/xmmt/dituon/plugin/PluginConfig.kt @@ -22,7 +22,7 @@ data class PluginConfig( val cachePoolSize: Int? = 10000, val keyListFormat: ReplyFormat = ReplyFormat.FORWARD, val fuzzy: Boolean = false, - val strictCommand: Boolean = true, +// val strictCommand: Boolean = true, val synchronized: Boolean = false, val headless: Boolean = true, val autoUpdate: Boolean = true, diff --git a/src/main/java/xmmt/dituon/plugin/PluginPetService.java b/src/main/java/xmmt/dituon/plugin/PluginPetService.java index 4a128c3e..17861e2b 100644 --- a/src/main/java/xmmt/dituon/plugin/PluginPetService.java +++ b/src/main/java/xmmt/dituon/plugin/PluginPetService.java @@ -51,7 +51,7 @@ private void readPluginConfig(PluginConfig config) { cachePoolSize = config.getCachePoolSize() != null ? config.getCachePoolSize() : 10000; replyFormat = config.getKeyListFormat(); fuzzy = config.getFuzzy(); - strictCommand = config.getStrictCommand(); +// strictCommand = config.getStrictCommand(); messageSynchronized = config.getSynchronized(); headless = config.getHeadless(); autoUpdate = config.getAutoUpdate();