From 8b8ef7e7443ac2636ae406a1c90794f9ce369154 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 4 Jun 2020 11:19:18 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20Issue=20#12=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=90=AF=E5=8A=A8=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=9A=84=E6=96=B9=E5=BC=8F,=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=B8=AA=E7=94=A8=E4=BA=8E=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E4=BA=BA=E5=BA=94=E7=94=A8=E5=86=85=E9=83=A8=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E7=B1=BB;=20[Fix]=20BotEventHandler?= =?UTF-8?q?,=20ImageCacheStore=20=E5=A2=9E=E5=8A=A0ShutdownHook=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=85=B3=E9=97=AD=E7=BA=BF=E7=A8=8B=E6=B1=A0,=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BA=BF=E7=A8=8B=E6=B1=A0=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=BF=87=E7=A8=8B=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?;=20[Change]=20BotEventHandler=20=E8=B0=83=E6=95=B4'match(Strin?= =?UTF-8?q?g)'=E6=96=B9=E6=B3=95;=20[Change]=20BotAdminCommandProcess=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4'savePushList()'=E6=96=B9=E6=B3=95=E5=AF=B9?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=9B=E5=BB=BA=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/Main.java | 80 +++++-------------- .../lamgc/cgj/bot/BotAdminCommandProcess.java | 7 +- .../net/lamgc/cgj/bot/BotCommandProcess.java | 20 +++-- .../net/lamgc/cgj/bot/SettingProperties.java | 3 +- .../lamgc/cgj/bot/boot/ApplicationBoot.java | 43 ++++++++++ .../net/lamgc/cgj/bot/boot/BotGlobal.java | 56 +++++++++++++ .../lamgc/cgj/bot/cache/ImageCacheStore.java | 6 ++ .../lamgc/cgj/bot/event/BotEventHandler.java | 38 +++------ .../cgj/bot/framework/coolq/CQPluginMain.java | 5 +- .../cgj/bot/framework/mirai/MiraiMain.java | 6 +- .../mirai/message/MiraiMessageSender.java | 4 +- 11 files changed, 163 insertions(+), 105 deletions(-) create mode 100644 src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java create mode 100644 src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java diff --git a/src/main/java/net/lamgc/cgj/Main.java b/src/main/java/net/lamgc/cgj/Main.java index 582f35a..79df9bb 100644 --- a/src/main/java/net/lamgc/cgj/Main.java +++ b/src/main/java/net/lamgc/cgj/Main.java @@ -12,6 +12,7 @@ import net.lamgc.cgj.bot.framework.mirai.MiraiMain; import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivSearchBuilder; import net.lamgc.cgj.pixiv.PixivURL; +import net.lamgc.cgj.util.PropertiesUtils; import net.lamgc.plps.PixivLoginProxyServer; import net.lamgc.utils.base.ArgumentsProperties; import net.lamgc.utils.base.runner.Argument; @@ -54,11 +55,13 @@ public class Main { log.trace("ContentGrabbingJi 正在启动..."); log.debug("Args: {}, LogsPath: {}", Arrays.toString(args), System.getProperty("cgj.logsPath")); log.debug("运行目录: {}", System.getProperty("user.dir")); + + ApplicationBoot.initialApplication(args); + log.debug("botDataDir: {}", System.getProperty("cgj.botDataDir")); ArgumentsProperties argsProp = new ArgumentsProperties(args); - - if(!getSettingToSysProp(argsProp, "proxy", null)) { - getEnvSettingToSysProp("CGJ_PROXY", "proxy", null); + if(!PropertiesUtils.getSettingToSysProp(argsProp, "proxy", null)) { + PropertiesUtils.getEnvSettingToSysProp("CGJ_PROXY", "proxy", null); } String proxyAddress = System.getProperty("cgj.proxy"); @@ -70,20 +73,7 @@ public class Main { proxy = null; } - if(!storeDir.exists() && !storeDir.mkdirs()) { - log.error("创建文件夹失败!"); - } - - if(!getSettingToSysProp(argsProp, "botDataDir", "./") && - !getEnvSettingToSysProp("CGJ_BOT_DATA_DIR", "botDataDir", "./")) { - log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录."); - } - if(!getSettingToSysProp(argsProp, "redisAddress", "127.0.0.1") && - !getEnvSettingToSysProp("CGJ_REDIS_URI", "redisAddress", "127.0.0.1")) { - log.warn("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)"); - } - - File cookieStoreFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store"); + File cookieStoreFile = new File(BotGlobal.getGlobal().getDataStoreDir(), "cookies.store"); if(!cookieStoreFile.exists()) { log.warn("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)"); try(Scanner scanner = new Scanner(System.in)) { @@ -104,37 +94,7 @@ public class Main { log.debug("传入参数: {}", Arrays.toString(args)); ArgumentsRunner.run(Main.class, args); - } - - /** - * 从ArgumentsProperties获取设置项到System Properties - * @param prop ArgumentsProperties对象 - * @param key 设置项key - * @param defaultValue 默认值 - * @return 如果成功从ArgumentsProperties获得设置项, 返回true, 如未找到(使用了defaultValue或null), 返回false; - */ - private static boolean getSettingToSysProp(ArgumentsProperties prop, String key, String defaultValue) { - if(prop.containsKey(key)) { - log.info("{}: {}", key, prop.getValue(key)); - System.setProperty("cgj." + key, prop.getValue(key)); - return true; - } else { - if(defaultValue != null) { - System.setProperty("cgj." + key, defaultValue); - } - return false; - } - } - - private static boolean getEnvSettingToSysProp(String envKey, String sysPropKey, String defaultValue) { - String env = System.getenv(envKey); - if(env != null) { - System.setProperty("cgj." + sysPropKey, env); - return true; - } else if(defaultValue != null) { - System.setProperty("cgj." + sysPropKey, defaultValue); - } - return false; + System.exit(0); } @Command @@ -146,10 +106,7 @@ public class Main { @Command public static void pluginMode(@Argument(name = "args", force = false) String argsStr) { - if(!System.getProperty("cgj.botDataDir").endsWith("\\") && !System.getProperty("cgj.botDataDir").endsWith("/")) { - System.setProperty("cgj.botDataDir", System.getProperty("cgj.botDataDir") + "/"); - } - log.info("酷Q机器人根目录: {}", System.getProperty("cgj.botDataDir")); + log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); CQConfig.init(); Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+"); Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr)); @@ -165,7 +122,7 @@ public class Main { @Command public static void collectionDownload() throws IOException { PixivDownload pixivDownload = new PixivDownload(Objects.requireNonNull(cookieStore), proxy); - File outputFile = new File(storeDir, "collection.zip"); + File outputFile = new File(getStoreDir(), "collection.zip"); if(!outputFile.exists() && !outputFile.createNewFile()) { log.error("文件创建失败: " + outputFile.getAbsolutePath()); } @@ -192,10 +149,10 @@ public class Main { PixivDownload pixivDownload = new PixivDownload(Objects.requireNonNull(cookieStore), proxy); String date = new SimpleDateFormat("yyyyMMdd").format(new Date()); int id = 1; - File outputFile = new File(storeDir, "recommends-" + date + "-" + id + ".zip"); + File outputFile = new File(getStoreDir(), "recommends-" + date + "-" + id + ".zip"); while(outputFile.exists()) { id++; - outputFile = new File(storeDir, "recommends-" + date + "-" + id + ".zip"); + outputFile = new File(getStoreDir(), "recommends-" + date + "-" + id + ".zip"); } if(!outputFile.createNewFile()) { @@ -259,10 +216,10 @@ public class Main { } int id = 1; - File outputFile = new File(storeDir, "ranking" + rankingMode.modeParam + "-" + date + "-" + id + ".zip"); + File outputFile = new File(getStoreDir(), "ranking" + rankingMode.modeParam + "-" + date + "-" + id + ".zip"); while(outputFile.exists()) { id++; - outputFile = new File(storeDir, "ranking" + rankingMode.modeParam + "-" + date + "-" + id + ".zip"); + outputFile = new File(getStoreDir(), "ranking" + rankingMode.modeParam + "-" + date + "-" + id + ".zip"); } if(!outputFile.createNewFile()) { @@ -401,7 +358,7 @@ public class Main { private static void saveCookieStoreToFile() throws IOException { log.info("正在保存CookieStore..."); - File outputFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store"); + File outputFile = new File(BotGlobal.getGlobal().getDataStoreDir(), "cookies.store"); if(!outputFile.exists() && !outputFile.createNewFile()){ log.error("保存CookieStore失败."); return; @@ -450,4 +407,11 @@ public class Main { } } + private static File getStoreDir() { + if(!storeDir.exists() && !storeDir.mkdirs()) { + log.error("创建文件夹失败!"); + } + return storeDir; + } + } diff --git a/src/main/java/net/lamgc/cgj/bot/BotAdminCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotAdminCommandProcess.java index 478207a..25f3357 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotAdminCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotAdminCommandProcess.java @@ -5,6 +5,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; +import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.message.MessageSenderBuilder; import net.lamgc.cgj.bot.message.MessageSource; import net.lamgc.cgj.pixiv.PixivDownload; @@ -22,7 +23,7 @@ public class BotAdminCommandProcess { private final static Logger log = LoggerFactory.getLogger(BotAdminCommandProcess.class.getName()); - private final static File pushListFile = new File(System.getProperty("cgj.botDataDir"), "pushList.json"); + private final static File pushListFile = new File(BotGlobal.getGlobal().getDataStoreDir(), "pushList.json"); private final static Hashtable pushInfoMap = new Hashtable<>(); @@ -182,8 +183,8 @@ public class BotAdminCommandProcess { @Command public static String savePushList() { try { - if(!pushListFile.exists()) { - pushListFile.createNewFile(); + if(!pushListFile.exists() && !pushListFile.createNewFile()) { + throw new IOException("文件夹创建失败!(Path: " + pushListFile.getPath() + ")"); } } catch (IOException e) { log.error("PushList.json文件创建失败", e); diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index 0403640..3dc6e29 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -5,8 +5,8 @@ import com.google.common.base.Throwables; import com.google.gson.*; import io.netty.handler.codec.http.HttpHeaderNames; import net.lamgc.cgj.Main; +import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.cache.*; -import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.event.BufferMessageEvent; import net.lamgc.cgj.bot.sort.PreLoadDataComparator; import net.lamgc.cgj.pixiv.PixivDownload; @@ -41,7 +41,7 @@ public class BotCommandProcess { private final static Logger log = LoggerFactory.getLogger(BotCommandProcess.class.getName()); - private final static File imageStoreDir = new File(System.getProperty("cgj.botDataDir"), "data/image/cgj/"); + private final static File imageStoreDir = new File(BotGlobal.getGlobal().getDataStoreDir(), "data/image/cgj/"); private final static Gson gson = new GsonBuilder() .serializeNulls() .create(); @@ -54,38 +54,38 @@ public class BotCommandProcess { * 作品信息缓存 - 不过期 */ private final static CacheStore illustInfoCache = - new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson); + new JsonRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "illustInfo", gson); /** * 作品信息预加载数据 - 有效期 2 小时, 本地缓存有效期1 ± 0.25 */ private final static CacheStore illustPreLoadDataCache = CacheStoreUtils.hashLocalHotDataStore( - new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson), + new JsonRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "illustPreLoadData", gson), 3600000, 900000); /** * 搜索内容缓存, 有效期 2 小时 */ private final static CacheStore searchBodyCache = - new JsonRedisCacheStore(BotEventHandler.redisServer, "searchBody", gson); + new JsonRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "searchBody", gson); /** * 排行榜缓存, 不过期 */ private final static CacheStore> rankingCache = - new JsonObjectRedisListCacheStore(BotEventHandler.redisServer, "ranking", gson); + new JsonObjectRedisListCacheStore(BotGlobal.getGlobal().getRedisServer(), "ranking", gson); /** * 作品页面下载链接缓存 - 不过期 */ private final static CacheStore> pagesCache = - new StringListRedisCacheStore(BotEventHandler.redisServer, "imagePages"); + new StringListRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "imagePages"); /** * 作品报告存储 - 不过期 */ public final static CacheStore reportStore = - new JsonRedisCacheStore(BotEventHandler.redisServer, "report", gson); + new JsonRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "report", gson); private final static RankingUpdateTimer updateTimer = new RankingUpdateTimer(); @@ -462,8 +462,6 @@ public class BotCommandProcess { PixivURL.getPixivRefererLink(illustId) ); - //pageCount - String imageMsg = getImageById(fromGroup, illustId, PixivDownload.PageQuality.REGULAR, 1); if (isNoSafe(illustId, SettingProperties.getProperties(fromGroup), true)) { log.warn("作品Id {} 为R-18作品, 跳过.", illustId); @@ -663,7 +661,7 @@ public class BotCommandProcess { illustPreLoadDataCache.clear(); pagesCache.clear(); searchBodyCache.clear(); - File imageStoreDir = new File(System.getProperty("cgj.botDataDir") + "data/image/cgj/"); + File imageStoreDir = new File(BotGlobal.getGlobal().getDataStoreDir(), "data/image/cgj/"); File[] listFiles = imageStoreDir.listFiles(); if (listFiles == null) { log.debug("图片缓存目录为空或内部文件获取失败!"); diff --git a/src/main/java/net/lamgc/cgj/bot/SettingProperties.java b/src/main/java/net/lamgc/cgj/bot/SettingProperties.java index 945299b..59e2319 100644 --- a/src/main/java/net/lamgc/cgj/bot/SettingProperties.java +++ b/src/main/java/net/lamgc/cgj/bot/SettingProperties.java @@ -1,6 +1,7 @@ package net.lamgc.cgj.bot; import com.google.common.base.Throwables; +import net.lamgc.cgj.bot.boot.BotGlobal; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -157,7 +158,7 @@ public final class SettingProperties { * @return 返回目录File对象. */ private static File getPropertiesDir() { - File propDir = new File(System.getProperty("cgj.botDataDir"), "/setting/"); + File propDir = new File(BotGlobal.getGlobal().getDataStoreDir(), "/setting/"); if(!propDir.exists() && !propDir.mkdirs()) { log.warn("Setting文件夹创建失败!"); } diff --git a/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java b/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java new file mode 100644 index 0000000..78d06a2 --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java @@ -0,0 +1,43 @@ +package net.lamgc.cgj.bot.boot; + +import net.lamgc.cgj.bot.event.BotEventHandler; +import net.lamgc.cgj.util.PropertiesUtils; +import net.lamgc.utils.base.ArgumentsProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class ApplicationBoot { + + private final static Logger log = LoggerFactory.getLogger(ApplicationBoot.class); + + private ApplicationBoot() {} + + /** + * 初始化应用. + *

该方法不会初始化机器人, 仅初始化应用所需的配置信息.

+ */ + public static void initialApplication(String[] args) { + ArgumentsProperties argsProp = new ArgumentsProperties(args); + if(!PropertiesUtils.getSettingToSysProp(argsProp, "botDataDir", "./") && + !PropertiesUtils.getEnvSettingToSysProp("CGJ_BOT_DATA_DIR", "botDataDir", "./")) { + log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录."); + } + if(!PropertiesUtils.getSettingToSysProp(argsProp, "redisAddress", "127.0.0.1") && + !PropertiesUtils.getEnvSettingToSysProp("CGJ_REDIS_URI", "redisAddress", "127.0.0.1")) { + log.warn("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)"); + } + + // 初始化 BotGlobal + //noinspection ResultOfMethodCallIgnored 这里仅仅是加载BotGlobal而已 + BotGlobal.getGlobal(); + } + + /** + * 初始化机器人. + *

本方法由框架调用.

+ */ + public static void initialBot() { + BotEventHandler.initial(); + } + +} diff --git a/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java b/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java new file mode 100644 index 0000000..87f28ae --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java @@ -0,0 +1,56 @@ +package net.lamgc.cgj.bot.boot; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import redis.clients.jedis.JedisPool; + +import java.io.File; +import java.net.URI; + +public final class BotGlobal { + + private final static BotGlobal instance = new BotGlobal(); + + public static BotGlobal getGlobal() { + if(instance == null) { + throw new IllegalStateException(""); + } + return instance; + } + + private final static Logger log = LoggerFactory.getLogger(BotGlobal.class); + + private final URI redisUri; + + /** + * 所有缓存共用的JedisPool + */ + private final JedisPool redisServer; + + private final File dataStoreDir; + + private BotGlobal() { + this.redisUri = URI.create("redis://" + System.getProperty("cgj.redisAddress")); + this.redisServer = new JedisPool( + getRedisUri().getHost(), + getRedisUri().getPort() == -1 ? 6379 : getRedisUri().getPort()); + String dataStoreDirPath = System.getProperty("cgj.botDataDir"); + this.dataStoreDir = new File((!dataStoreDirPath.endsWith("/") || !dataStoreDirPath.endsWith("\\")) ? + dataStoreDirPath + System.getProperty("file.separator") : dataStoreDirPath); + } + + public URI getRedisUri() { + return redisUri; + } + + public File getDataStoreDir() { + if(!dataStoreDir.exists() && !dataStoreDir.mkdirs()) { + log.error("DataStoreDir 创建失败, 数据存储可能失效!"); + } + return dataStoreDir; + } + + public JedisPool getRedisServer() { + return redisServer; + } +} diff --git a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java index 7657838..2d696a3 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java @@ -33,6 +33,12 @@ public final class ImageCacheStore { private final static ImageCacheHandler handler = new ImageCacheHandler(); + static { + Thread shutdownThread = new Thread(imageCacheExecutor::shutdownNow); + shutdownThread.setName("Thread-ImageCacheShutdown"); + Runtime.getRuntime().addShutdownHook(shutdownThread); + } + private ImageCacheStore() {} /** diff --git a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java index 9668b4d..1ecaab2 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -20,10 +20,8 @@ import net.lamgc.utils.event.EventObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.JedisPool; import java.lang.reflect.Method; -import java.net.URI; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; @@ -44,12 +42,6 @@ public class BotEventHandler implements EventHandler { private final static Map muteStateMap = new Hashtable<>(); - /** - * 所有缓存共用的JedisPool - */ - private final static URI redisServerUri = URI.create("redis://" + System.getProperty("cgj.redisAddress")); - public final static JedisPool redisServer = new JedisPool(redisServerUri.getHost(), redisServerUri.getPort() == -1 ? 6379 : redisServerUri.getPort()); - /** * 消息事件执行器 */ @@ -66,10 +58,10 @@ public class BotEventHandler implements EventHandler { )); private static boolean initialled = false; - static { - initial(); - } + /** + * 初始化BotEventHandler + */ public synchronized static void initial() { if(initialled) { Logger logger = LoggerFactory.getLogger("BotEventHandler@"); @@ -92,25 +84,19 @@ public class BotEventHandler implements EventHandler { }); try { executor.addHandler(new BotEventHandler()); + Thread shutdownThread = new Thread(() -> executor.shutdown(true)); + shutdownThread.setName("Thread-EventHandlerShutdown"); + Runtime.getRuntime().addShutdownHook(shutdownThread); } catch (IllegalAccessException e) { LoggerFactory.getLogger("BotEventHandler@Static").error("添加Handler时发生异常", e); } - initialled = true; - } - private final static AtomicBoolean preLoaded = new AtomicBoolean(); - /** - * 预加载 - */ - public synchronized static void preLoad() { - if(preLoaded.get()) { - return; - } try { BotAdminCommandProcess.loadPushList(); - } finally { - preLoaded.set(true); + } catch(Throwable e) { + log.error("加载推送列表失败", e); } + initialled = true; } private BotEventHandler() { @@ -159,7 +145,7 @@ public class BotEventHandler implements EventHandler { public void processMessage(MessageEvent event) { String msg = event.getMessage(); log.debug(event.toString()); - if(!match(msg)) { + if(mismatch(msg)) { return; } else if(isMute(event.getFromGroup())) { log.debug("机器人已被禁言, 忽略请求."); @@ -253,8 +239,8 @@ public class BotEventHandler implements EventHandler { * @param message 要检查的消息 * @return 如果为true则提交 */ - public static boolean match(String message) { - return message.startsWith(COMMAND_PREFIX) || message.startsWith(ADMIN_COMMAND_PREFIX); + public static boolean mismatch(String message) { + return !message.startsWith(COMMAND_PREFIX) && !message.startsWith(ADMIN_COMMAND_PREFIX); } private static boolean isMute(long groupId) { diff --git a/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java b/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java index c35ff64..d334a4a 100644 --- a/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java +++ b/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java @@ -1,5 +1,6 @@ package net.lamgc.cgj.bot.framework.coolq; +import net.lamgc.cgj.bot.boot.ApplicationBoot; import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.framework.coolq.message.SpringCQMessageEvent; import net.lamgc.utils.event.EventHandler; @@ -18,7 +19,7 @@ public class CQPluginMain extends CQPlugin implements EventHandler { public CQPluginMain() { // TODO(LamGC, 2020.04.21): SpringCQ无法适配MessageSenderBuilder // MessageSenderBuilder.setCurrentMessageSenderFactory(new SpringCQMessageSenderFactory()); - BotEventHandler.preLoad(); + ApplicationBoot.initialBot(); LoggerFactory.getLogger(CQPluginMain.class.getName()) .info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX); } @@ -45,7 +46,7 @@ public class CQPluginMain extends CQPlugin implements EventHandler { * @return 是否拦截消息 */ private static int processMessage(CoolQ cq, CQMessageEvent event) { - if(!BotEventHandler.match(event.getMessage())) { + if(BotEventHandler.mismatch(event.getMessage())) { return MESSAGE_IGNORE; } BotEventHandler.executeMessageEvent(new SpringCQMessageEvent(cq, event)); diff --git a/src/main/java/net/lamgc/cgj/bot/framework/mirai/MiraiMain.java b/src/main/java/net/lamgc/cgj/bot/framework/mirai/MiraiMain.java index 66c812a..b7a55f2 100644 --- a/src/main/java/net/lamgc/cgj/bot/framework/mirai/MiraiMain.java +++ b/src/main/java/net/lamgc/cgj/bot/framework/mirai/MiraiMain.java @@ -1,5 +1,7 @@ package net.lamgc.cgj.bot.framework.mirai; +import net.lamgc.cgj.bot.boot.ApplicationBoot; +import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent; import net.lamgc.cgj.bot.message.MessageSenderBuilder; @@ -35,7 +37,7 @@ public class MiraiMain implements Closeable { return; } - File botPropFile = new File(System.getProperty("cgj.botDataDir"), "./bot.properties"); + File botPropFile = new File(BotGlobal.getGlobal().getDataStoreDir(), "./bot.properties"); try (Reader reader = new BufferedReader(new FileReader(botPropFile))) { botProperties.load(reader); } catch (IOException e) { @@ -55,7 +57,7 @@ public class MiraiMain implements Closeable { event -> BotEventHandler.setMuteState(event.getGroup().getId(), false)); bot.login(); MessageSenderBuilder.setCurrentMessageSenderFactory(new MiraiMessageSenderFactory(bot)); - BotEventHandler.preLoad(); + ApplicationBoot.initialBot(); bot.join(); } diff --git a/src/main/java/net/lamgc/cgj/bot/framework/mirai/message/MiraiMessageSender.java b/src/main/java/net/lamgc/cgj/bot/framework/mirai/message/MiraiMessageSender.java index 22fdc98..33f7d3c 100644 --- a/src/main/java/net/lamgc/cgj/bot/framework/mirai/message/MiraiMessageSender.java +++ b/src/main/java/net/lamgc/cgj/bot/framework/mirai/message/MiraiMessageSender.java @@ -2,11 +2,11 @@ package net.lamgc.cgj.bot.framework.mirai.message; import com.google.common.base.Strings; import net.lamgc.cgj.bot.BotCode; +import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.cache.CacheStore; import net.lamgc.cgj.bot.cache.HotDataCacheStore; import net.lamgc.cgj.bot.cache.LocalHashCacheStore; import net.lamgc.cgj.bot.cache.StringRedisCacheStore; -import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.message.MessageSender; import net.lamgc.cgj.bot.message.MessageSource; import net.mamoe.mirai.Bot; @@ -28,7 +28,7 @@ public class MiraiMessageSender implements MessageSender { private final MessageSource source; private final static Logger log = LoggerFactory.getLogger(MiraiMessageSender.class.getName()); private final static CacheStore imageIdCache = new HotDataCacheStore<>( - new StringRedisCacheStore(BotEventHandler.redisServer, "mirai.imageId"), + new StringRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "mirai.imageId"), new LocalHashCacheStore<>(), 5400000, 1800000, true);