mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Change] ConsoleMain, MiraiMain 尝试为平台框架实现Framework接口;
[Change] Framework 补充Javadoc, 增加"getName"方法的默认方法体; [Change] FrameworkManager 调整向frameworkThreadGroup发起中断的时机;
This commit is contained in:
parent
1b937953c3
commit
c2e8a07500
@ -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.CQConfig;
|
||||
import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
|
||||
@ -93,14 +94,12 @@ public class Main {
|
||||
|
||||
@Command
|
||||
public static void botMode(@Argument(name = "args", force = false) String argsStr) {
|
||||
MiraiMain main = new MiraiMain();
|
||||
main.init();
|
||||
main.close();
|
||||
FrameworkManager.registerFramework(new MiraiMain());
|
||||
}
|
||||
|
||||
@Command
|
||||
public static void consoleMode() throws IOException {
|
||||
ConsoleMain.start();
|
||||
public static void consoleMode() {
|
||||
FrameworkManager.registerFramework(new ConsoleMain());
|
||||
}
|
||||
|
||||
@Command
|
||||
|
@ -12,11 +12,21 @@ public interface Framework {
|
||||
|
||||
/**
|
||||
* 框架运行方法
|
||||
* @throws Exception
|
||||
* @throws Exception 当框架抛出异常时, 将会终止框架的所有活动.
|
||||
*/
|
||||
void run() throws Exception;
|
||||
|
||||
/**
|
||||
* 关闭框架
|
||||
* @throws Exception 即使该方法抛出异常, {@link FrameworkManager}依然会尝试向框架所属的线程发起中断, 以试图清除框架资源.
|
||||
*/
|
||||
void close() throws Exception;
|
||||
|
||||
String getName();
|
||||
/**
|
||||
* 获取框架标识名
|
||||
* @return 返回标识名
|
||||
*/
|
||||
default String getName() {
|
||||
return this.toString();
|
||||
}
|
||||
}
|
||||
|
@ -46,9 +46,10 @@ public final class FrameworkManager {
|
||||
try {
|
||||
framework.init(frameworkResources);
|
||||
framework.run();
|
||||
frameworkResources.getFrameworkThreadGroup().interrupt();
|
||||
} catch(Throwable e) {
|
||||
frameworkResources.getLogger().error("框架未捕获异常, 导致异常退出.", e);
|
||||
} finally {
|
||||
frameworkResources.getFrameworkThreadGroup().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,8 @@ 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.cli.message.ConsoleMessageEvent;
|
||||
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageSenderFactory;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||
@ -12,13 +14,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(FrameworkManager.FrameworkResources resources) { }
|
||||
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
MessageSenderBuilder.setCurrentMessageSenderFactory(new ConsoleMessageSenderFactory());
|
||||
ApplicationBoot.initialBot();
|
||||
LineReader lineReader = LineReaderBuilder.builder()
|
||||
@ -45,7 +52,17 @@ 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 getName() {
|
||||
return this.toString();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ 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.FrameworkManager;
|
||||
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;
|
||||
@ -24,7 +26,7 @@ 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);
|
||||
|
||||
@ -32,7 +34,8 @@ public class MiraiMain implements Closeable {
|
||||
|
||||
private final static Properties botProperties = new Properties();
|
||||
|
||||
public void init() {
|
||||
@Override
|
||||
public void init(FrameworkManager.FrameworkResources resources) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||
try {
|
||||
Class.forName(BotEventHandler.class.getName());
|
||||
@ -69,6 +72,7 @@ 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);
|
||||
@ -82,6 +86,12 @@ public class MiraiMain implements Closeable {
|
||||
bot.join();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
bot.login();
|
||||
bot.join();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理消息事件
|
||||
* @param message 消息事件对象
|
||||
@ -101,6 +111,7 @@ public class MiraiMain implements Closeable {
|
||||
/**
|
||||
* 关闭机器人
|
||||
*/
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if(bot == null) {
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user