From 4784f8773b6e19aca66f55a640fe84bd6aae4c12 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 3 Jul 2020 10:09:21 +0800 Subject: [PATCH 01/14] =?UTF-8?q?[Change]=20Pixiv=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E5=80=99=E9=80=89=E6=8E=A5=E5=8F=A3.md=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3=E5=86=85=E5=AE=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- documents/interfaces/Pixiv搜索推荐候选接口.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/documents/interfaces/Pixiv搜索推荐候选接口.md b/documents/interfaces/Pixiv搜索推荐候选接口.md index f59bc6f..e043215 100644 --- a/documents/interfaces/Pixiv搜索推荐候选接口.md +++ b/documents/interfaces/Pixiv搜索推荐候选接口.md @@ -7,9 +7,11 @@ GET https://www.pixiv.net/rpc/cps.php? ``` -- 是否需要登录: `是` +- 是否需要登录: `否` - 是否为Pixiv标准接口返回格式: `否` -- 是否需要Referer请求头: `否` +- 是否需要Referer请求头: `是` + +> 补充: Referer请求头只要是Pixiv的就可以了. ### 参数 ### Url参数: From 73a1caaf465a13daf7bb6271a73b00c4046fd540 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 9 Jul 2020 22:17:09 +0800 Subject: [PATCH 02/14] =?UTF-8?q?[Fix=20#21]=20=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?RandomIntervalSendTimer=E5=9C=A8=E9=AB=98=E7=89=88=E6=9C=ACJava?= =?UTF-8?q?=E4=B8=AD=E5=9B=A0=E9=9D=9E=E6=B3=95=E5=8F=8D=E5=B0=84=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=BA=94=E7=94=A8=E5=BC=82=E5=B8=B8=E7=BB=88=E6=AD=A2?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cgj/bot/RandomIntervalSendTimer.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java b/src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java index ce693ab..229e426 100644 --- a/src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java +++ b/src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java @@ -4,7 +4,6 @@ import com.google.common.base.Throwables; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -23,7 +22,7 @@ public class RandomIntervalSendTimer extends TimerTask { private final long time; private final int floatTime; private final AtomicBoolean loop = new AtomicBoolean(); - private final AtomicBoolean start = new AtomicBoolean(); + private final AtomicBoolean running = new AtomicBoolean(); private final String hashId = Integer.toHexString(this.hashCode()); @@ -88,7 +87,6 @@ public class RandomIntervalSendTimer extends TimerTask { this.sender = sender; this.time = time; this.floatTime = floatTime; - timerMap.put(timerId, this); if(startNow) { start(loop); } @@ -108,21 +106,18 @@ public class RandomIntervalSendTimer extends TimerTask { Date nextDate = new Date(); nextDate.setTime(nextDate.getTime() + nextDelay); log.info("定时器 {} 下一延迟: {}ms ({})", hashId, nextDelay, nextDate); - if(start.get()) { - try { - Field state = this.getClass().getSuperclass().getDeclaredField("state"); - state.setAccessible(true); - state.setInt(this, 0); - state.setAccessible(false); - } catch (NoSuchFieldException | IllegalAccessException e) { - e.printStackTrace(); - return; - } + if(running.get()) { + reset(); + return; } - start.set(true); + running.set(true); timer.schedule(this, nextDelay); } + public void reset() { + timerMap.put(timerId, (RandomIntervalSendTimer) clone()); + } + @Override public void run() { log.info("定时器 {} 开始执行...(Sender: {}@{})", this.hashId, sender.getClass().getSimpleName(), sender.hashCode()); @@ -145,7 +140,7 @@ public class RandomIntervalSendTimer extends TimerTask { */ @Override public boolean cancel() { - start.set(false); + running.set(false); loop.set(false); return super.cancel(); } @@ -158,4 +153,18 @@ public class RandomIntervalSendTimer extends TimerTask { timerMap.remove(this.timerId); } + /** + * 克隆一个参数完全一样的TimerTask对象. + * @return 返回对象不同, 参数相同的TimerTask对象. + */ + @Override + @SuppressWarnings("MethodDoesntCallSuperMethod") + public Object clone() { + RandomIntervalSendTimer newTimerTask = new RandomIntervalSendTimer( + this.timerId, this.sender, + time, floatTime, + running.get(), loop.get()); + this.destroy(); + return newTimerTask; + } } From 0727ef4f93ae650bad89c4fccec81fa5c2a17032 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 9 Jul 2020 22:19:35 +0800 Subject: [PATCH 03/14] =?UTF-8?q?[Clear]=20CQPluginMain=20=E6=95=B4?= =?UTF-8?q?=E7=90=86=E4=BB=A3=E7=A0=81;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d9122cd..28c43b1 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 @@ -17,12 +17,12 @@ import org.springframework.stereotype.Component; import java.util.concurrent.atomic.AtomicBoolean; @Component +@SuppressWarnings("unused") public class CQPluginMain extends CQPlugin implements EventHandler { private final static AtomicBoolean initialState = new AtomicBoolean(); public CQPluginMain() { - LoggerFactory.getLogger(CQPluginMain.class) .info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX); } From d1c7f6f973dce3d6bd84d9d3dc788e91528f0b5d Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 9 Jul 2020 22:45:36 +0800 Subject: [PATCH 04/14] =?UTF-8?q?[Version]=20=E6=9B=B4=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC(2.5.2-20200630.2-SNAPSHOT=20->=202.5.2-20200709.1-SNA?= =?UTF-8?q?PSHOT);?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index efc69c6..cfbdcf2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.lamgc ContentGrabbingJi - 2.5.2-20200630.2-SNAPSHOT + 2.5.2-20200709.1-SNAPSHOT From 6d55325fc7b273b99406db69eb1a08c036bfc04d Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 10 Jul 2020 09:53:41 +0800 Subject: [PATCH 05/14] =?UTF-8?q?[Fix]=20PixivUgoiraBuilder=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DZipInputStream=E5=9C=A8=E8=AF=BB=E5=8F=96=E5=AE=8C?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=B8=A7=E5=9B=BE=E5=83=8F=E5=90=8E=E8=A2=AB?= =?UTF-8?q?=E6=84=8F=E5=A4=96=E5=85=B3=E9=97=AD=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java b/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java index 5bc321e..73efdb7 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivUgoiraBuilder.java @@ -1,5 +1,6 @@ package net.lamgc.cgj.pixiv; +import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -12,7 +13,6 @@ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.util.EntityUtils; -import org.apache.tomcat.util.http.fileupload.util.Streams; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -127,14 +127,13 @@ public final class PixivUgoiraBuilder { HashMap frameMap = new HashMap<>(frames.size()); while((entry = zipInputStream.getNextEntry()) != null) { log.trace("ZipEntry {} 正在接收...", entry); - Streams.copy(zipInputStream, cacheOutputStream, false); + ByteStreams.copy(zipInputStream, cacheOutputStream); frameMap.put(entry.getName(), new ByteArrayInputStream(cacheOutputStream.toByteArray())); log.trace("ZipEntry {} 已接收完成.", entry); cacheOutputStream.reset(); } - - InputStream firstFrameInput = frameMap.get("000000.jpg"); + InputStream firstFrameInput = frameMap.get(frames.get(0).getAsJsonObject().get("file").getAsString()); BufferedImage firstFrame = ImageIO.read(firstFrameInput); firstFrameInput.reset(); if(width != firstFrame.getWidth() || height != firstFrame.getHeight()) { From 6fbbe522db32a678608d5b058460e7e481e124e9 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 12 Jul 2020 01:12:58 +0800 Subject: [PATCH 06/14] =?UTF-8?q?[Fix=20#22]=20PixivDownload=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=9B=A0InputStream=E6=84=8F=E5=A4=96=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BC=82=E5=B8=B8=E8=AF=AF?= =?UTF-8?q?=E5=88=A4;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java index 7c0f6df..a113c87 100644 --- a/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java +++ b/src/main/java/net/lamgc/cgj/pixiv/PixivDownload.java @@ -402,7 +402,7 @@ public class PixivDownload { if(resultObject.get("error").getAsBoolean()) { String message = resultObject.get("message").getAsString(); log.warn("作品页面接口请求错误, 错误信息: {}", message); - throw new HttpRequestException(response); + throw new HttpRequestException(response.getStatusLine(), resultObject.toString()); } JsonArray linkArray = resultObject.getAsJsonArray("body"); From 210aa84ed531420f61fbd5af6de9c6a21ef18ab1 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 12 Jul 2020 11:12:21 +0800 Subject: [PATCH 07/14] =?UTF-8?q?[Update]=20IssueTemplates=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8A=9F=E8=83=BD=E6=A8=A1=E6=9D=BF=E5=B9=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0Bug=E6=A8=A1=E6=9D=BF;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/ISSUE_TEMPLATE/--------.md | 11 +++++++ .github/ISSUE_TEMPLATE/Bug_Report.md | 33 +++++++++++++-------- .github/ISSUE_TEMPLATE/bug---.md | 35 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature-request.md | 20 +++++++++++++ 4 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/--------.md create mode 100644 .github/ISSUE_TEMPLATE/bug---.md create mode 100644 .github/ISSUE_TEMPLATE/feature-request.md diff --git a/.github/ISSUE_TEMPLATE/--------.md b/.github/ISSUE_TEMPLATE/--------.md new file mode 100644 index 0000000..9bc11b3 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/--------.md @@ -0,0 +1,11 @@ +--- +name: 功能/想法 提议 +about: 使用这个模板将你对应用的想法提出来,或许我们会采纳! +title: '' +labels: function, question +assignees: '' + +--- + + + diff --git a/.github/ISSUE_TEMPLATE/Bug_Report.md b/.github/ISSUE_TEMPLATE/Bug_Report.md index 939591d..083bbcb 100644 --- a/.github/ISSUE_TEMPLATE/Bug_Report.md +++ b/.github/ISSUE_TEMPLATE/Bug_Report.md @@ -7,20 +7,29 @@ assignees: '' --- -## Environmental information ## -OS(e.g: Windows 10 1909): -Java(e.g: Oracle Jdk 8.242): -Issue version(versionTag or commitId): +## 环境信息 ## +系统(例如: Windows 10 1909): `` +Java版本(例如: Oracle Jdk 8.242): `` + + +发生问题所在的版本: `` -## Problem description ## -// Describe the problem in as much detail as possible here +## 问题描述 ## + -## Expected behavior ## -// What will this function do under normal circumstances? +## 预期行为 ## + -## Actual behavior ## -// But what does this feature actually look like? +## 实际行为 ## + -## Recurrence steps ## -// What can we do to recreate this situation? +## 复现步骤 ## + 1. + +## 相关信息 ## +### 日志 ### + +``` + +``` diff --git a/.github/ISSUE_TEMPLATE/bug---.md b/.github/ISSUE_TEMPLATE/bug---.md new file mode 100644 index 0000000..ac9f6db --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug---.md @@ -0,0 +1,35 @@ +--- +name: Bug 反馈 +about: 使用这个模板反馈应用问题。 +title: '' +labels: bug +assignees: '' + +--- + +## 环境信息 ## +系统(例如: Windows 10 1909): `` +Java版本(例如: Oracle Jdk 8.242): `` + + +发生问题所在的版本: `` + +## 问题描述 ## + + +## 预期行为 ## + + +## 实际行为 ## + + +## 复现步骤 ## + +1. + +## 相关信息 ## +### 日志 ### + +``` + +``` diff --git a/.github/ISSUE_TEMPLATE/feature-request.md b/.github/ISSUE_TEMPLATE/feature-request.md new file mode 100644 index 0000000..3b1b286 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature-request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: function, question +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. From a606ec04239e2b934b125de149a2bed35b56950d Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 12 Jul 2020 11:18:29 +0800 Subject: [PATCH 08/14] =?UTF-8?q?[Fix]=20IssueTemplates=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=A8=A1=E6=9D=BF=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/ISSUE_TEMPLATE/Bug_Report.md | 4 +-- .../{--------.md => Feature_Report.md} | 0 .github/ISSUE_TEMPLATE/bug---.md | 35 ------------------- .github/ISSUE_TEMPLATE/feature-request.md | 20 ----------- 4 files changed, 2 insertions(+), 57 deletions(-) rename .github/ISSUE_TEMPLATE/{--------.md => Feature_Report.md} (100%) delete mode 100644 .github/ISSUE_TEMPLATE/bug---.md delete mode 100644 .github/ISSUE_TEMPLATE/feature-request.md diff --git a/.github/ISSUE_TEMPLATE/Bug_Report.md b/.github/ISSUE_TEMPLATE/Bug_Report.md index 083bbcb..ac9f6db 100644 --- a/.github/ISSUE_TEMPLATE/Bug_Report.md +++ b/.github/ISSUE_TEMPLATE/Bug_Report.md @@ -1,6 +1,6 @@ --- -name: Bug Report -about: Use this template to feedback bugs. +name: Bug 反馈 +about: 使用这个模板反馈应用问题。 title: '' labels: bug assignees: '' diff --git a/.github/ISSUE_TEMPLATE/--------.md b/.github/ISSUE_TEMPLATE/Feature_Report.md similarity index 100% rename from .github/ISSUE_TEMPLATE/--------.md rename to .github/ISSUE_TEMPLATE/Feature_Report.md diff --git a/.github/ISSUE_TEMPLATE/bug---.md b/.github/ISSUE_TEMPLATE/bug---.md deleted file mode 100644 index ac9f6db..0000000 --- a/.github/ISSUE_TEMPLATE/bug---.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -name: Bug 反馈 -about: 使用这个模板反馈应用问题。 -title: '' -labels: bug -assignees: '' - ---- - -## 环境信息 ## -系统(例如: Windows 10 1909): `` -Java版本(例如: Oracle Jdk 8.242): `` - - -发生问题所在的版本: `` - -## 问题描述 ## - - -## 预期行为 ## - - -## 实际行为 ## - - -## 复现步骤 ## - -1. - -## 相关信息 ## -### 日志 ### - -``` - -``` diff --git a/.github/ISSUE_TEMPLATE/feature-request.md b/.github/ISSUE_TEMPLATE/feature-request.md deleted file mode 100644 index 3b1b286..0000000 --- a/.github/ISSUE_TEMPLATE/feature-request.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project -title: '' -labels: function, question -assignees: '' - ---- - -**Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Additional context** -Add any other context or screenshots about the feature request here. From 0fc3e3ab48ab8ff7b552d76cd12135b6a16287be Mon Sep 17 00:00:00 2001 From: LamGC Date: Mon, 13 Jul 2020 09:42:33 +0800 Subject: [PATCH 09/14] =?UTF-8?q?[Change]=20=E8=B0=83=E6=95=B4=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=86=85=E4=BA=8B=E4=BB=B6=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E5=8F=82=E6=95=B0;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Change] BotEventHandler 调整线程池参数; --- src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 8975042..b2bcece 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -46,9 +46,9 @@ public class BotEventHandler implements EventHandler { * 消息事件执行器 */ private final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor( - 180000, // 3minThr - Math.max(Runtime.getRuntime().availableProcessors(), 4), - Math.min(Math.max(Runtime.getRuntime().availableProcessors(), 4), 32), + 180000, // 3min limit + Math.max(Runtime.getRuntime().availableProcessors(), 4), // 4 ~ processors + Math.min(Math.max(Runtime.getRuntime().availableProcessors() * 2, 8), 32),// (8 ~ processors * 2) ~ 32 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1536), From 4387da37f52f53424ebff36f56d0cdc6f9444b48 Mon Sep 17 00:00:00 2001 From: LamGC Date: Mon, 13 Jul 2020 09:47:08 +0800 Subject: [PATCH 10/14] =?UTF-8?q?[Change]=20=E8=B0=83=E6=95=B4=E7=BE=A4?= =?UTF-8?q?=E7=A6=81=E8=A8=80=E7=AE=A1=E7=90=86=E8=81=8C=E8=B4=A3;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Delete] BotEventHandler 移除禁言状态管理相关功能; [Add] GroupMuteManager, GroupMuteManagerTest 增加群禁言状态管理类; [Change] MiraiMain 将禁言状态对接由使用BotEventHandler改为独立管理; --- .../lamgc/cgj/bot/event/BotEventHandler.java | 51 +------------------ .../cgj/bot/framework/mirai/MiraiMain.java | 16 ++++-- .../lamgc/cgj/bot/util/GroupMuteManager.java | 48 +++++++++++++++++ .../cgj/bot/util/GroupMuteManagerTest.java | 30 +++++++++++ 4 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java create mode 100644 src/test/java/net/lamgc/cgj/bot/util/GroupMuteManagerTest.java 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 b2bcece..cfca657 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -26,7 +26,6 @@ import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,8 +39,6 @@ public class BotEventHandler implements EventHandler { private final static Logger log = LoggerFactory.getLogger(BotEventHandler.class); - private final static Map muteStateMap = new Hashtable<>(); - /** * 消息事件执行器 */ @@ -167,9 +164,6 @@ public class BotEventHandler implements EventHandler { log.debug(event.toString()); if(mismatch(msg)) { return; - } else if(isMute(event.getFromGroup())) { - log.debug("机器人已被禁言, 忽略请求."); - return; } Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+"); @@ -236,7 +230,7 @@ public class BotEventHandler implements EventHandler { } } long processTime = System.currentTimeMillis() - time; - if(!Objects.isNull(result) && result instanceof String && !isMute(event.getFromGroup())) { + if(!Objects.isNull(result) && result instanceof String) { try { int sendResult = event.sendMessage((String) result); if (sendResult < 0) { @@ -248,8 +242,6 @@ public class BotEventHandler implements EventHandler { } catch (Exception e) { log.error("发送消息时发生异常", e); } - } else if(isMute(event.getFromGroup())) { - log.warn("命令反馈时机器人已被禁言, 跳过反馈."); } long totalTime = System.currentTimeMillis() - time; log.info("命令反馈完成.(事件耗时: {}ms, P: {}%({}ms), R: {}%({}ms))", totalTime, @@ -266,45 +258,4 @@ public class BotEventHandler implements EventHandler { return !message.startsWith(COMMAND_PREFIX) && !message.startsWith(ADMIN_COMMAND_PREFIX); } - /** - * 查询某群组中Bot是否被禁言 - * @param groupId 待查询的群组号 - * @return 如果被禁言, 返回true, 如果未被禁言或禁言情况未知, 返回false - */ - private static boolean isMute(long groupId) { - Boolean mute = isMute(groupId, false); - return mute != null && mute; - } - - /** - * 查询某群是否被禁言. - * @param groupId 群组Id - * @param rawValue 是否返回原始值(当没有该群状态, 且本参数为true时, 将返回null) - * @return 返回状态值, 如无该群禁言记录且rawValue = true, 则返回null - */ - public static Boolean isMute(long groupId, boolean rawValue) { - if(groupId <= 0) { - return false; - } - AtomicBoolean state = muteStateMap.get(groupId); - if(state == null && rawValue) { - return null; - } - return state != null && state.get(); - } - - /** - * 设置机器人禁言状态. - *

设置该项可防止因机器人在禁言期间反馈请求导致被封号.

- * @param mute 如果被禁言, 传入true - */ - public static void setMuteState(long groupId, boolean mute) { - if(!muteStateMap.containsKey(groupId)) { - muteStateMap.put(groupId, new AtomicBoolean(mute)); - } else { - muteStateMap.get(groupId).set(mute); - } - log.warn("群组 {} 机器人禁言状态已变更: {}", groupId, mute ? "已禁言" : "已解除"); - } - } 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 e3241f3..d9f36e5 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 @@ -6,6 +6,7 @@ import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent; import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory; import net.lamgc.cgj.bot.message.MessageSenderBuilder; +import net.lamgc.cgj.bot.util.GroupMuteManager; import net.mamoe.mirai.Bot; import net.mamoe.mirai.BotFactoryJvm; import net.mamoe.mirai.event.events.BotMuteEvent; @@ -30,7 +31,9 @@ public class MiraiMain implements Closeable { private Bot bot; - private final static Properties botProperties = new Properties(); + private final Properties botProperties = new Properties(); + + private final GroupMuteManager muteManager = new GroupMuteManager(); public void init() { Runtime.getRuntime().addShutdownHook(new Thread(this::close)); @@ -73,9 +76,9 @@ public class MiraiMain implements Closeable { Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent); Events.subscribeAlways(TempMessageEvent.class, this::executeMessageEvent); Events.subscribeAlways(BotMuteEvent.class, - event -> BotEventHandler.setMuteState(event.getGroup().getId(), true)); + event -> muteManager.setMuteState(event.getGroup().getId(), true)); Events.subscribeAlways(BotUnmuteEvent.class, - event -> BotEventHandler.setMuteState(event.getGroup().getId(), false)); + event -> muteManager.setMuteState(event.getGroup().getId(), false)); bot.login(); MessageSenderBuilder.setCurrentMessageSenderFactory(new MiraiMessageSenderFactory(bot)); ApplicationBoot.initialBot(); @@ -90,9 +93,12 @@ public class MiraiMain implements Closeable { log.debug("Mirai Message: {}", message); if(message instanceof GroupMessageEvent) { GroupMessageEvent GroupMessageEvent = (GroupMessageEvent) message; - if(BotEventHandler.isMute(GroupMessageEvent.getGroup().getId(), true) == null) { - BotEventHandler.setMuteState(GroupMessageEvent.getGroup().getId(), + Boolean muteState = muteManager.isMute(GroupMessageEvent.getGroup().getId(), true); + if(muteState == null) { + muteManager.setMuteState(GroupMessageEvent.getGroup().getId(), ((GroupMessageEvent) message).getGroup().getBotMuteRemaining() != 0); + } else if(muteState) { + return; } } BotEventHandler.executeMessageEvent(MiraiMessageEvent.covertEventObject(message)); diff --git a/src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java b/src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java new file mode 100644 index 0000000..6193839 --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java @@ -0,0 +1,48 @@ +package net.lamgc.cgj.bot.util; + +import java.util.Hashtable; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * 群禁言管理器. + *

该管理器用于存取群组禁言状态.

+ */ +public class GroupMuteManager { + + private final Map muteStateMap = new Hashtable<>(); + + /** + * 查询某群是否被禁言. + * @param groupId 群组Id + * @param rawValue 是否返回原始值(当没有该群状态, 且本参数为true时, 将返回null) + * @return 返回状态值, 如无该群禁言记录且rawValue = true, 则返回null + */ + public Boolean isMute(long groupId, boolean rawValue) { + if(groupId <= 0) { + return false; + } + AtomicBoolean state = muteStateMap.get(groupId); + if(state == null && rawValue) { + return null; + } + return state != null && state.get(); + } + + /** + * 设置机器人禁言状态. + *

设置该项可防止因机器人在禁言期间反馈请求导致被封号.

+ * @param mute 如果被禁言, 传入true + */ + public void setMuteState(long groupId, boolean mute) { + if(groupId <= 0) { + return; + } + if(!muteStateMap.containsKey(groupId)) { + muteStateMap.put(groupId, new AtomicBoolean(mute)); + } else { + muteStateMap.get(groupId).set(mute); + } + } + +} diff --git a/src/test/java/net/lamgc/cgj/bot/util/GroupMuteManagerTest.java b/src/test/java/net/lamgc/cgj/bot/util/GroupMuteManagerTest.java new file mode 100644 index 0000000..3a04325 --- /dev/null +++ b/src/test/java/net/lamgc/cgj/bot/util/GroupMuteManagerTest.java @@ -0,0 +1,30 @@ +package net.lamgc.cgj.bot.util; + +import org.junit.Assert; +import org.junit.Test; + +public class GroupMuteManagerTest { + + @Test + public void muteStateTest() { + GroupMuteManager manager = new GroupMuteManager(); + Assert.assertNull(manager.isMute(1, true)); // 未设置的群组返回null + Assert.assertFalse(manager.isMute(1, false)); // 未设置就返回false + manager.setMuteState(1, true); // mute == true + Assert.assertNotNull(manager.isMute(1, true)); // 第一次设置后不为null + Assert.assertTrue(manager.isMute(1, false)); // 确保条件正常 + manager.setMuteState(2, true); // 不能出现不同群号的冲突 + manager.setMuteState(1, false); + Assert.assertTrue(manager.isMute(2, false)); + Assert.assertNotNull(manager.isMute(1, true)); // 变更为false后依然不能返回null + Assert.assertFalse(manager.isMute(1, false)); + } + + @Test + public void invalidGroupIdTest() { + GroupMuteManager manager = new GroupMuteManager(); + manager.setMuteState(-1, true); // 设置应该是无效的 + Assert.assertFalse(manager.isMute(-1, false)); // 由于设置无效, 返回false即可 + } + +} From 56ef463c639996177f2a6e46ba523646b2f8dd34 Mon Sep 17 00:00:00 2001 From: LamGC Date: Mon, 13 Jul 2020 20:35:39 +0800 Subject: [PATCH 11/14] =?UTF-8?q?[Change]=20MessageEvent=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4`toString`=E8=BE=93=E5=87=BA=E4=B8=AD`hashCode`?= =?UTF-8?q?=E7=9A=84=E6=A0=BC=E5=BC=8F;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/bot/event/MessageEvent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/lamgc/cgj/bot/event/MessageEvent.java b/src/main/java/net/lamgc/cgj/bot/event/MessageEvent.java index f27cdac..0abb79b 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/MessageEvent.java +++ b/src/main/java/net/lamgc/cgj/bot/event/MessageEvent.java @@ -57,7 +57,7 @@ public abstract class MessageEvent implements EventObject, MessageSender { @Override public String toString() { - return this.getClass().getSimpleName() + "@" + this.hashCode() + "{" + + return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode()) + "{" + "fromGroup=" + getFromGroup() + ", fromQQ=" + getFromQQ() + ", message='" + getMessage() + '\'' + From 575dc0c7fb1131fe9531bafaa4a6493bb3259d5d Mon Sep 17 00:00:00 2001 From: LamGC Date: Tue, 14 Jul 2020 11:01:21 +0800 Subject: [PATCH 12/14] =?UTF-8?q?[Fix]=20Dockerfile.sample=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BA=86=E5=AE=B9=E5=99=A8=E6=97=A0=E6=B3=95=E6=AD=A3?= =?UTF-8?q?=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84=E9=97=AE=E9=A2=98;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile.sample | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile.sample b/Dockerfile.sample index cacbf9a..dc72ffd 100644 --- a/Dockerfile.sample +++ b/Dockerfile.sample @@ -5,6 +5,7 @@ ENV jarFileName=ContentGrabbingJi-exec.jar ENV CGJ_REDIS_URI="127.0.0.1:6379" ENV CGJ_PROXY="" RUN mkdir /data/ -CMD [java, "-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"] +ENTRYPOINT ["/usr/java/openjdk-14/bin/java", "-Duser.timezone=Aisa/Shanghai"] +CMD ["-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"] COPY ${jarFileName} /CGJ.jar From a7c434da6161337a5328fdadce578332e8979f42 Mon Sep 17 00:00:00 2001 From: LamGC Date: Wed, 15 Jul 2020 10:35:31 +0800 Subject: [PATCH 13/14] =?UTF-8?q?[Change]=20=E8=B0=83=E6=95=B4Readme?= =?UTF-8?q?=E5=86=85=E5=AE=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Fix] README.md 补充管理员命令内容; --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef5b8ee..049c69e 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,14 @@ 你应该注意到了,在部署过程中,你需要设置一个管理员QQ的配置,色图姬支持通过命令来管理色图姬的运行。 目前支持的管理员命令: ```bash -# 清除缓存 +# 清除缓存(慎用) +# 该操作将会清除Redis服务器内的所有数据, 以及色图姬下载到本地的所有图片缓存. .cgjadmin cleanCache # 设置配置项 # 如果不使用group参数, 则设置全局配置 +# 注意: 配置项设置后需要使用`.cgjadmin saveProperties`才会保存到文件中, +# 如不保存, 则仅能在本次运行中生效(或使用`.cgjadmin loadProperties`重新加载后失效). .cgjadmin setProperty <-key 配置项名> <-value 配置项新值> [-group 指定群组] # 查询配置项 @@ -79,7 +82,8 @@ # 增加群组作品推送 # 如果增加了original参数, 则图片为原图发送 # 如果不指定group参数, 则群组为命令发送所在群组 -.cgjadmin addPushGroup [-group 指定群组号] [-minTime 最小] [-floatTime 随机时间范围] [-rankingStart 排行榜起始排名] +# 最长发送时间 = 最短发送时间 + 随机时间范围 +.cgjadmin addPushGroup [-group 指定群组号] [-minTime 最短发送时间] [-floatTime 随机时间范围] [-rankingStart 排行榜起始排名] [-rankingStop 排行榜结束排名] [-mode 排行榜模式] [-type 排行榜类型] [-original] # 删除群组推送功能 From bf8de1ac1eee4b107d22fa42ff0709b992db3c0b Mon Sep 17 00:00:00 2001 From: LamGC Date: Wed, 15 Jul 2020 14:09:47 +0800 Subject: [PATCH 14/14] =?UTF-8?q?[Fix]=20=E4=BF=AE=E5=A4=8D=E6=BD=9C?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E6=97=B6=E5=8C=BA=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Fix] Dockerfile.sample 将时区配置从`Asia/Shanghai`改成`GMT+8`; --- Dockerfile.sample | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.sample b/Dockerfile.sample index dc72ffd..fb1aa62 100644 --- a/Dockerfile.sample +++ b/Dockerfile.sample @@ -5,7 +5,7 @@ ENV jarFileName=ContentGrabbingJi-exec.jar ENV CGJ_REDIS_URI="127.0.0.1:6379" ENV CGJ_PROXY="" RUN mkdir /data/ -ENTRYPOINT ["/usr/java/openjdk-14/bin/java", "-Duser.timezone=Aisa/Shanghai"] +ENTRYPOINT ["/usr/java/openjdk-14/bin/java", "-Duser.timezone=GMT+8"] CMD ["-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"] COPY ${jarFileName} /CGJ.jar