Skip to content

Commit

Permalink
perf: 添加导出csv命令
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Jan 19, 2024
1 parent 22d67c4 commit 1283549
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/main/java/cn/evole/onebot/mirai/OneBotMirai.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.evole.onebot.mirai;

import cn.evole.onebot.mirai.cmd.OneBotMiraiCmd;
import cn.evole.onebot.mirai.config.PluginConfig;
import cn.evole.onebot.mirai.core.session.SessionManager;
import cn.evole.onebot.mirai.database.NanoDb;
Expand All @@ -10,6 +11,7 @@
import lombok.val;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.Mirai;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.contact.Friend;
import net.mamoe.mirai.contact.Group;
Expand Down Expand Up @@ -44,9 +46,10 @@ private OneBotMirai() {
VERSION = getDescription().getVersion().toString();
}

private final File imageFolder = new File(getDataFolder(), "image");
private final File recordFolder = new File(getDataFolder(), "record");
public NanoDb<Integer, String> db = null;
public final File imageFolder = new File(getDataFolder(), "image");
public final File recordFolder = new File(getDataFolder(), "record");
public final File dbFolder = new File(getDataFolder(), "db");
public NanoDb<Long, DBUtils.MessageNode> db = null;

@Override
public void onEnable() {
Expand All @@ -56,11 +59,13 @@ public void onEnable() {
FileUtils.checkFolder(recordFolder.toPath());
if (PluginConfig.INSTANCE.getDb().getEnable()) {
try {
db = new NanoDb<>(getDataFolderPath() + "/db.udb");
db = new NanoDb<>(dbFolder + "/db.obm");
} catch (Exception ignored) {
}
}

CommandManager.INSTANCE.registerCommand(OneBotMiraiCmd.INSTANCE, false);

logger.info("Plugin loaded!");
logger.info("插件当前版本: " + VERSION);
logger.info("开发交流群: 720975019");
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/cn/evole/onebot/mirai/cmd/OneBotMiraiCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.evole.onebot.mirai.cmd;

import cn.evole.onebot.mirai.OneBotMirai;
import cn.evole.onebot.mirai.util.BaseUtils;
import cn.evole.onebot.mirai.util.DBUtils;
import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.console.command.java.JCompositeCommand;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Project: onebot-mirai
* @Author: cnlimiter
* @CreateTime: 2024/1/19 12:04
* @Description:
*/

public class OneBotMiraiCmd extends JCompositeCommand {

public static OneBotMiraiCmd INSTANCE = new OneBotMiraiCmd();
public OneBotMiraiCmd() {
super(OneBotMirai.INSTANCE, "onebot", "ob");
}

@SubCommand
public void dbToCsv(CommandSender sender) {
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
var csv= OneBotMirai.INSTANCE.db.toCSV(DBUtils.MessageNode.class, "机器人Id", DBUtils.Locates.getLocalizer());
var file = OneBotMirai.INSTANCE.dbFolder.toPath().resolve(dateFormat.format(date) + ".csv");
BaseUtils.safeRun(() -> {
try {
Files.writeString(file, csv, StandardOpenOption.CREATE_NEW);
} catch (IOException ignored) {
sender.sendMessage("导出csv失败");
}
});

sender.sendMessage("导出csv成功:" + file.toFile().getAbsolutePath());
}




}
33 changes: 31 additions & 2 deletions src/main/java/cn/evole/onebot/mirai/util/DBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.evole.onebot.mirai.OneBotMirai;
import cn.evole.onebot.mirai.config.PluginConfig;
import cn.evole.onebot.mirai.database.csv.Localizer;
import cn.evole.onebot.sdk.util.DataBaseUtils;
import lombok.val;
import net.mamoe.mirai.event.events.MessageEvent;
Expand All @@ -15,13 +16,41 @@
*/

public class DBUtils {
public static class MessageNode {
long contactId;
int messageId;
String content;
public MessageNode(long contactId, int messageId, String content) {
this.contactId = contactId;
this.messageId = messageId;
this.content = content;
}
}

public static class Locates {
private static Localizer l = null;

public static Localizer getLocalizer() {
if (l == null) {
l = new Localizer();
l.add("contactId", "群或者陌生人或者好友id");
l.add("messageId", "消息id");
l.add("content", "内容");
}
return l;
}
}
public static void saveMessageToDB(MessageEvent event) {
if (PluginConfig.INSTANCE.getDb().getEnable()){
val messageId = DataBaseUtils.toMessageId(event.getSource().getInternalIds(), event.getBot().getId(), event.getSource().getFromId());
if (OneBotMirai.INSTANCE.db!=null){
OneBotMirai.INSTANCE.db.set(
messageId,
MessageChain.serializeToJsonString(event.getMessage()));
event.getBot().getId(),
new MessageNode(
event.getSubject().getId(),
messageId,
MessageChain.serializeToJsonString(event.getMessage()))
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.concurrent.atomic.AtomicReference;

/**
* Description:正向websocket服务器
* Description:反向websocket服务器
* Author: cnlimiter
* Date: 2022/10/14 18:44
* Version: 1.0
Expand Down Expand Up @@ -100,7 +100,7 @@ public void onClose(int code, String reason, boolean remote) {

@Override
public void onError(Exception ex) {
miraiLogger.warning(String.format("Bot: %d 反向Websocket服务端 / 出现错误 \n %s", botSession.getBot().getId(), ex.getMessage()), ex);
miraiLogger.warning(String.format("Bot: %d 反向Websocket服务端 / 出现错误 \n %s", botSession.getBot().getId(), ex.getMessage()));
}


Expand Down

0 comments on commit 1283549

Please sign in to comment.