[Add] 增加控制台框架以进行本地测试, 或非聊天机器人使用;

This commit is contained in:
LamGC 2020-06-04 11:25:08 +08:00
parent e803d2161b
commit 9ce18469e6
5 changed files with 89 additions and 0 deletions

View File

@ -7,6 +7,9 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; 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.cli.ConsoleMain;
import net.lamgc.cgj.bot.framework.coolq.CQConfig; import net.lamgc.cgj.bot.framework.coolq.CQConfig;
import net.lamgc.cgj.bot.framework.mirai.MiraiMain; import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivDownload;
@ -104,6 +107,11 @@ public class Main {
main.close(); main.close();
} }
@Command
public static void consoleMode() {
ConsoleMain.start();
}
@Command @Command
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) { public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath());

View File

@ -0,0 +1,30 @@
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.message.MessageSenderBuilder;
import java.util.Scanner;
public class ConsoleMain {
public static void start() {
MessageSenderBuilder.setCurrentMessageSenderFactory(new ConsoleMessageSenderFactory());
ApplicationBoot.initialBot();
Scanner scanner = new Scanner(System.in);
boolean isGroup = false;
do {
String input = scanner.nextLine();
if(input.equalsIgnoreCase("#exit")) {
System.out.println("退出应用...");
break;
} else if(input.equalsIgnoreCase("#setgroup")) {
isGroup = !isGroup;
System.out.println("System: 群模式状态已变更: " + isGroup);
continue;
}
BotEventHandler.executeMessageEvent(new ConsoleMessageEvent(input, isGroup));
} while(true);
}
}

View File

@ -0,0 +1,23 @@
package net.lamgc.cgj.bot.framework.cli;
import net.lamgc.cgj.bot.event.MessageEvent;
import java.util.Date;
public class ConsoleMessageEvent extends MessageEvent {
public ConsoleMessageEvent(String message, boolean isGroup) {
super(isGroup ? 1 : 0, 1, message);
}
@Override
public int sendMessage(String message) {
System.out.println(new Date() + " Bot: " + message);
return 0;
}
@Override
public String getImageUrl(String image) {
return null;
}
}

View File

@ -0,0 +1,13 @@
package net.lamgc.cgj.bot.framework.cli;
import net.lamgc.cgj.bot.message.MessageSender;
import java.util.Date;
public class ConsoleMessageSender implements MessageSender {
@Override
public synchronized int sendMessage(String message) {
System.out.println(new Date() + " Bot: " + message);
return 0;
}
}

View File

@ -0,0 +1,15 @@
package net.lamgc.cgj.bot.framework.cli;
import net.lamgc.cgj.bot.message.MessageSender;
import net.lamgc.cgj.bot.message.MessageSenderFactory;
import net.lamgc.cgj.bot.message.MessageSource;
public class ConsoleMessageSenderFactory implements MessageSenderFactory {
private final static ConsoleMessageSender sender = new ConsoleMessageSender();
@Override
public MessageSender createMessageSender(MessageSource source, long id) {
return sender;
}
}