mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-01 21:07:25 +00:00
Compare commits
20 Commits
v2.5.2-202
...
add-framew
Author | SHA1 | Date | |
---|---|---|---|
5637ef30d4
|
|||
bf8de1ac1e | |||
6fc7d8ad78
|
|||
a7c434da61 | |||
575dc0c7fb
|
|||
56ef463c63
|
|||
4387da37f5
|
|||
0fc3e3ab48
|
|||
a606ec0423
|
|||
210aa84ed5 | |||
6fbbe522db | |||
6d55325fc7 | |||
9a7d16124a
|
|||
b754559187 | |||
a87735d9e0
|
|||
6ec99dbf17
|
|||
1599a5325a
|
|||
5c2b6b4ee5
|
|||
c2e8a07500
|
|||
1b937953c3
|
37
.github/ISSUE_TEMPLATE/Bug_Report.md
vendored
37
.github/ISSUE_TEMPLATE/Bug_Report.md
vendored
@ -1,26 +1,35 @@
|
||||
---
|
||||
name: Bug Report
|
||||
about: Use this template to feedback bugs.
|
||||
name: Bug 反馈
|
||||
about: 使用这个模板反馈应用问题。
|
||||
title: ''
|
||||
labels: bug
|
||||
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): ``
|
||||
<!-- 如果直接使用发布版,那么就填写发布版版本号(v开头) -->
|
||||
<!-- 如果你通过编译运行的方式运行开发版,请你填写运行所使用的Commit Id -->
|
||||
发生问题所在的版本: ``
|
||||
|
||||
## 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.
|
||||
|
||||
## 相关信息 ##
|
||||
### 日志 ###
|
||||
<!-- 如果日志涉及了问题,请务必将日志一同提交,这对排查问题非常有用 -->
|
||||
```
|
||||
|
||||
```
|
||||
|
11
.github/ISSUE_TEMPLATE/Feature_Report.md
vendored
Normal file
11
.github/ISSUE_TEMPLATE/Feature_Report.md
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
---
|
||||
name: 功能/想法 提议
|
||||
about: 使用这个模板将你对应用的想法提出来,或许我们会采纳!
|
||||
title: ''
|
||||
labels: function, question
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
<!-- 如果可以,尽可能的清晰、详细的表达你的想法 -->
|
||||
<!-- 没关系的,我们会进一步向你交流以尝试了解你的想法! -->
|
@ -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=GMT+8"]
|
||||
CMD ["-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"]
|
||||
|
||||
COPY ${jarFileName} /CGJ.jar
|
||||
|
@ -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]
|
||||
|
||||
# 删除群组推送功能
|
||||
|
@ -7,6 +7,7 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||
import net.lamgc.cgj.bot.framework.FrameworkManager;
|
||||
import net.lamgc.cgj.bot.framework.cli.ConsoleMain;
|
||||
import net.lamgc.cgj.bot.framework.coolq.SpringCQApplication;
|
||||
import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
|
||||
@ -88,19 +89,29 @@ public class Main {
|
||||
|
||||
@Command
|
||||
public static void botMode(@Argument(name = "args", force = false) String argsStr) {
|
||||
MiraiMain main = new MiraiMain();
|
||||
main.init();
|
||||
main.close();
|
||||
try {
|
||||
FrameworkManager.registerFramework(new MiraiMain()).join();
|
||||
} catch (InterruptedException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Command
|
||||
public static void consoleMode() throws IOException {
|
||||
ConsoleMain.start();
|
||||
public static void consoleMode() {
|
||||
try {
|
||||
FrameworkManager.registerFramework(new ConsoleMain()).join();
|
||||
} catch (InterruptedException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Command
|
||||
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
|
||||
new SpringCQApplication().start(argsStr);
|
||||
try {
|
||||
FrameworkManager.registerFramework(new SpringCQApplication()).join();
|
||||
} catch (InterruptedException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Command
|
||||
|
@ -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,15 +39,13 @@ public class BotEventHandler implements EventHandler {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(BotEventHandler.class);
|
||||
|
||||
private final static Map<Long, AtomicBoolean> muteStateMap = new Hashtable<>();
|
||||
|
||||
/**
|
||||
* 消息事件执行器
|
||||
*/
|
||||
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),
|
||||
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置机器人禁言状态.
|
||||
* <p>设置该项可防止因机器人在禁言期间反馈请求导致被封号.</p>
|
||||
* @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 ? "已禁言" : "已解除");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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() + '\'' +
|
||||
|
41
src/main/java/net/lamgc/cgj/bot/framework/Framework.java
Normal file
41
src/main/java/net/lamgc/cgj/bot/framework/Framework.java
Normal file
@ -0,0 +1,41 @@
|
||||
package net.lamgc.cgj.bot.framework;
|
||||
|
||||
public interface Framework {
|
||||
|
||||
/**
|
||||
* 框架初始化方法
|
||||
* @param resources 框架所分配到的资源.
|
||||
* @throws Exception 当框架抛出异常时, 将不会继续运行框架.
|
||||
* @see FrameworkResources
|
||||
*/
|
||||
void init(FrameworkResources resources) throws Exception;
|
||||
|
||||
/**
|
||||
* 框架运行方法
|
||||
* @throws Exception 当框架抛出异常时, 将会终止框架的所有活动.
|
||||
*/
|
||||
void run() throws Exception;
|
||||
|
||||
/**
|
||||
* 关闭框架
|
||||
* @throws Exception 即使该方法抛出异常, {@link FrameworkManager}依然会尝试向框架所属的线程发起中断, 以试图清除框架资源.
|
||||
*/
|
||||
void close() throws Exception;
|
||||
|
||||
/**
|
||||
* 获取框架标识名.
|
||||
* <p>可根据需要自行调整框架标识名.</p>
|
||||
* @return 返回标识名.
|
||||
*/
|
||||
default String getIdentify() {
|
||||
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取框架名称.
|
||||
* <p>框架名称不可更改.</p>
|
||||
* @return 返回框架名称.
|
||||
*/
|
||||
String getFrameworkName();
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package net.lamgc.cgj.bot.framework;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class FrameworkManager {
|
||||
|
||||
private FrameworkManager() {}
|
||||
|
||||
private final static Map<Framework, FrameworkResources> resourcesMap = new HashMap<>();
|
||||
|
||||
private final static ThreadGroup frameworkRootGroup = new ThreadGroup("FrameworkRootGroup");
|
||||
|
||||
static {
|
||||
Runtime.getRuntime()
|
||||
.addShutdownHook(new Thread(FrameworkManager::shutdownAllFramework, "FrameworkManager-Shutdown"));
|
||||
}
|
||||
|
||||
public static Thread registerFramework(Framework framework) {
|
||||
checkFramework(framework);
|
||||
FrameworkResources resources = new FrameworkResources(framework);
|
||||
resourcesMap.put(framework, resources);
|
||||
Thread frameworkThread = new Thread(resources.getFrameworkThreadGroup(),
|
||||
() -> FrameworkManager.runFramework(framework), "FrameworkThread-" + framework.getIdentify());
|
||||
|
||||
frameworkThread.start();
|
||||
return frameworkThread;
|
||||
}
|
||||
|
||||
private static final Pattern FRAMEWORK_NAME_CHECK_PATTERN = Pattern.compile("^[A-Za-z0-9_\\-$]+$");
|
||||
private static void checkFramework(Framework framework) {
|
||||
if(!FRAMEWORK_NAME_CHECK_PATTERN.matcher(framework.getFrameworkName()).matches()) {
|
||||
throw new IllegalStateException("Invalid Framework Name: " + framework.getFrameworkName());
|
||||
}
|
||||
}
|
||||
|
||||
public static Set<Framework> frameworkSet() {
|
||||
return new HashSet<>(resourcesMap.keySet());
|
||||
}
|
||||
|
||||
public static void shutdownAllFramework() {
|
||||
for (Framework framework : resourcesMap.keySet()) {
|
||||
FrameworkResources frameworkResources = resourcesMap.get(framework);
|
||||
Logger frameworkLogger = frameworkResources.getLogger();
|
||||
try {
|
||||
frameworkLogger.info("正在关闭框架...");
|
||||
framework.close();
|
||||
frameworkLogger.info("框架已关闭.");
|
||||
frameworkResources.getFrameworkThreadGroup().interrupt();
|
||||
resourcesMap.remove(framework);
|
||||
} catch(Throwable e) {
|
||||
frameworkLogger.error("退出框架时发生异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static ThreadGroup getFrameworkRootGroup() {
|
||||
return frameworkRootGroup;
|
||||
}
|
||||
|
||||
private static void runFramework(Framework framework) {
|
||||
FrameworkResources frameworkResources = resourcesMap.get(framework);
|
||||
try {
|
||||
framework.init(frameworkResources);
|
||||
framework.run();
|
||||
} catch(Throwable e) {
|
||||
frameworkResources.getLogger().error("框架未捕获异常, 导致异常退出.", e);
|
||||
} finally {
|
||||
frameworkResources.getFrameworkThreadGroup().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package net.lamgc.cgj.bot.framework;
|
||||
|
||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FrameworkResources {
|
||||
|
||||
private final static File frameworkDataStoreRootDir = new File(BotGlobal.getGlobal().getDataStoreDir(),
|
||||
"frameworks/");
|
||||
|
||||
private final ThreadGroup frameworkThreadGroup;
|
||||
|
||||
private final Logger logger;
|
||||
|
||||
private final File frameworkDataStoreDir;
|
||||
|
||||
public FrameworkResources(Framework framework) {
|
||||
frameworkThreadGroup = new ThreadGroup(FrameworkManager.getFrameworkRootGroup(),
|
||||
"Framework-" + framework.getIdentify());
|
||||
frameworkDataStoreDir = new File(frameworkDataStoreRootDir, framework.getClass().getSimpleName());
|
||||
logger = LoggerFactory.getLogger("Framework-" + framework.getIdentify());
|
||||
}
|
||||
|
||||
ThreadGroup getFrameworkThreadGroup() {
|
||||
return frameworkThreadGroup;
|
||||
}
|
||||
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,9 @@ package net.lamgc.cgj.bot.framework.cli;
|
||||
|
||||
import net.lamgc.cgj.bot.boot.ApplicationBoot;
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.framework.Framework;
|
||||
import net.lamgc.cgj.bot.framework.FrameworkManager;
|
||||
import net.lamgc.cgj.bot.framework.FrameworkResources;
|
||||
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageEvent;
|
||||
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageSenderFactory;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||
@ -12,13 +15,18 @@ import org.jline.terminal.TerminalBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class ConsoleMain {
|
||||
public class ConsoleMain implements Framework {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(ConsoleMain.class);
|
||||
private final AtomicBoolean quitState = new AtomicBoolean();
|
||||
|
||||
public static void start() throws IOException {
|
||||
@Override
|
||||
public void init(FrameworkResources resources) { }
|
||||
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
MessageSenderBuilder.setCurrentMessageSenderFactory(new ConsoleMessageSenderFactory());
|
||||
ApplicationBoot.initialBot();
|
||||
LineReader lineReader = LineReaderBuilder.builder()
|
||||
@ -45,7 +53,22 @@ public class ConsoleMain {
|
||||
} catch (InterruptedException e) {
|
||||
log.error("执行时发生中断", e);
|
||||
}
|
||||
} while(true);
|
||||
} while(!quitState.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
quitState.set(true);
|
||||
Thread.currentThread().getThreadGroup().interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentify() {
|
||||
return this.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFrameworkName() {
|
||||
return "console";
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package net.lamgc.cgj.bot.framework.coolq;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import net.lamgc.cgj.bot.boot.BotGlobal;
|
||||
import net.lamgc.cgj.bot.framework.Framework;
|
||||
import net.lamgc.cgj.bot.framework.FrameworkResources;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||
@ -12,28 +12,21 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextStoppedEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringCQApplication {
|
||||
public class SpringCQApplication implements Framework {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(SpringCQApplication.class);
|
||||
private Logger log;
|
||||
|
||||
private final Object quitLock = new Object();
|
||||
|
||||
public void start(String argsStr) {
|
||||
@Override
|
||||
public void init(FrameworkResources resources) {
|
||||
this.log = resources.getLogger();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath());
|
||||
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
||||
Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr));
|
||||
ArrayList<String> argsList = new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
argsList.add(matcher.group());
|
||||
}
|
||||
String[] args = new String[argsList.size()];
|
||||
argsList.toArray(args);
|
||||
ConfigurableApplicationContext context = SpringApplication.run(SpringCQApplication.class, args);
|
||||
ConfigurableApplicationContext context = SpringApplication.run(SpringCQApplication.class);
|
||||
registerShutdownHook(context);
|
||||
try {
|
||||
synchronized (quitLock) {
|
||||
@ -42,22 +35,30 @@ public class SpringCQApplication {
|
||||
} catch (InterruptedException e) {
|
||||
log.warn("发生中断, 退出SpringCQ...", e);
|
||||
}
|
||||
|
||||
context.stop();
|
||||
context.close();
|
||||
}
|
||||
|
||||
private void registerShutdownHook(ConfigurableApplicationContext context) {
|
||||
context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>)
|
||||
event -> notifyThread());
|
||||
event -> close());
|
||||
context.addApplicationListener((ApplicationListener<ContextClosedEvent>)
|
||||
event -> notifyThread());
|
||||
event -> close());
|
||||
context.addApplicationListener((ApplicationListener<ContextStoppedEvent>)
|
||||
event -> notifyThread());
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::notifyThread));
|
||||
event -> close());
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||
}
|
||||
|
||||
private void notifyThread() {
|
||||
public void close() {
|
||||
synchronized (quitLock) {
|
||||
quitLock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFrameworkName() {
|
||||
return "SpringCoolQ";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,9 +3,12 @@ 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.Framework;
|
||||
import net.lamgc.cgj.bot.framework.FrameworkResources;
|
||||
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;
|
||||
@ -24,15 +27,18 @@ import java.io.*;
|
||||
import java.util.Base64;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MiraiMain implements Closeable {
|
||||
public class MiraiMain implements Framework {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(MiraiMain.class);
|
||||
|
||||
private Bot bot;
|
||||
|
||||
private final static Properties botProperties = new Properties();
|
||||
private final Properties botProperties = new Properties();
|
||||
|
||||
public void init() {
|
||||
private final GroupMuteManager muteManager = new GroupMuteManager();
|
||||
|
||||
@Override
|
||||
public void init(FrameworkResources resources) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||
try {
|
||||
Class.forName(BotEventHandler.class.getName());
|
||||
@ -69,19 +75,26 @@ public class MiraiMain implements Closeable {
|
||||
|
||||
bot = BotFactoryJvm.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")),
|
||||
Base64.getDecoder().decode(botProperties.getProperty("bot.password", "")), configuration);
|
||||
// TODO: 看看能不能单独订阅某个Bot?
|
||||
Events.subscribeAlways(GroupMessageEvent.class, this::executeMessageEvent);
|
||||
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();
|
||||
bot.join();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
bot.login();
|
||||
bot.join();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理消息事件
|
||||
* @param message 消息事件对象
|
||||
@ -90,9 +103,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));
|
||||
@ -101,6 +117,7 @@ public class MiraiMain implements Closeable {
|
||||
/**
|
||||
* 关闭机器人
|
||||
*/
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if(bot == null) {
|
||||
return;
|
||||
@ -111,4 +128,9 @@ public class MiraiMain implements Closeable {
|
||||
log.warn("机器人已关闭.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFrameworkName() {
|
||||
return "MiraiQQ";
|
||||
}
|
||||
|
||||
}
|
||||
|
48
src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java
Normal file
48
src/main/java/net/lamgc/cgj/bot/util/GroupMuteManager.java
Normal file
@ -0,0 +1,48 @@
|
||||
package net.lamgc.cgj.bot.util;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 群禁言管理器.
|
||||
* <p>该管理器用于存取群组禁言状态.</p>
|
||||
*/
|
||||
public class GroupMuteManager {
|
||||
|
||||
private final Map<Long, AtomicBoolean> 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置机器人禁言状态.
|
||||
* <p>设置该项可防止因机器人在禁言期间反馈请求导致被封号.</p>
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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");
|
||||
|
@ -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<String, InputStream> 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()) {
|
||||
|
@ -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即可
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user