Skip to content

Commit

Permalink
feat(fabric): 撤回?消息记录api
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Feb 21, 2024
1 parent cbd7432 commit 3d0e64a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 12 deletions.
3 changes: 3 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.evole.mods.mcbot;

import cn.evole.mods.mcbot.data.UserBindApi;
import cn.evole.mods.mcbot.data.ChatRecordApi;
import cn.evole.mods.mcbot.init.callbacks.IEvents;
import cn.evole.mods.mcbot.init.event.*;
import cn.evole.mods.mcbot.init.config.ModConfig;
Expand Down Expand Up @@ -73,6 +74,7 @@ public void init() {
CONFIG_FILE = CONFIG_FOLDER.resolve("config.toml");
I18n.init();
UserBindApi.load(CONFIG_FOLDER);
ChatRecordApi.load(CONFIG_FOLDER);
Runtime.getRuntime().addShutdownHook(new Thread(McBot::killOutThreads));
}

Expand Down Expand Up @@ -103,6 +105,7 @@ public void onServerStopping(MinecraftServer server) {
Const.isShutdown = true;
Const.LOGGER.info("▌ §c正在关闭群服互联 §a┈━═☆");
UserBindApi.save(CONFIG_FOLDER);
ChatRecordApi.save(CONFIG_FOLDER);
CustomCmdHandler.INSTANCE.clear();//自定义命令持久层清空
listenerFactory.stop();//分发器关闭
service.stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.McBot;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;

public class RecallCommand {

public static int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
int id = context.getArgument("MessageId", Integer.class);
McBot.bot.deleteMsg(id);
return 1;
}
}
29 changes: 29 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/data/ChatRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cn.evole.mods.mcbot.data;

import com.xykj.easycsv.entity.CsvProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
* @Project: McBot-fabric
* @Author: cnlimiter
* @CreateTime: 2024/2/21 13:59
* @Description:
*/

@Getter
@Setter
@AllArgsConstructor
public class ChatRecord {
@CsvProperty("消息ID")
private String messageId;
@CsvProperty("添加日期")
private long createTime;
@CsvProperty("qq")
private String qqId;
@CsvProperty("群号")
private String groupId;
@CsvProperty("消息")
private String message;
}
60 changes: 60 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/data/ChatRecordApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cn.evole.mods.mcbot.data;

import com.xykj.easycsv.EasyCsv;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

/**
* @Project: McBot-fabric
* @Author: cnlimiter
* @CreateTime: 2024/2/20 15:35
* @Description:
*/

public class ChatRecordApi {
private static final String FILE_NAME = "chat.csv";
private static final EasyCsv easyCsv = new EasyCsv();
public static Map<String, ChatRecord> users;

public static void load(Path folder){
Path bindFile = folder.resolve(FILE_NAME);
List<ChatRecord> chatRecords = new ArrayList<>();
Map<String, ChatRecord> chatRecordMap = new HashMap<>();
try {
if (!bindFile.toFile().isFile()) Files.createFile(bindFile);
chatRecords = easyCsv.readAll(bindFile.toFile().getAbsolutePath()
, ChatRecord.class);
for (ChatRecord chatRecord : chatRecords){
chatRecordMap.put(chatRecord.getMessageId(), chatRecord);
}
} catch (IOException ignored) {
}

users = chatRecordMap;
}

public static void save(Path folder){
Path bindFile = folder.resolve(FILE_NAME);
try {
Files.deleteIfExists(bindFile);
easyCsv.write(bindFile.toFile().getAbsolutePath()
, Arrays.asList(users.values().toArray()));
} catch (IOException ignored) {
}
}

public static boolean has(String message_id){
return users.containsKey(message_id);
}

public static void add(String message_id, String group_name, String qq_id, String message){
if (!has(qq_id)) users.put(qq_id, new ChatRecord(message_id, System.currentTimeMillis(), qq_id, group_name, message));
}

public static void del(String message_id){
if (has(message_id)) users.remove(message_id);
}
}
44 changes: 41 additions & 3 deletions fabric/src/main/java/cn/evole/mods/mcbot/init/event/IBotEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.cmds.CmdApi;
import cn.evole.mods.mcbot.data.ChatRecordApi;
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.util.onebot.CQUtils;
import cn.evole.onebot.client.factory.ListenerFactory;
import cn.evole.onebot.client.interfaces.handler.DefaultHandler;
import cn.evole.onebot.client.interfaces.listener.SimpleListener;
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import cn.evole.onebot.sdk.event.message.GuildMessageEvent;
import cn.evole.onebot.sdk.event.message.MessageEvent;
import cn.evole.onebot.sdk.event.meta.LifecycleMetaEvent;
import cn.evole.onebot.sdk.event.notice.group.GroupDecreaseNoticeEvent;
import cn.evole.onebot.sdk.event.notice.group.GroupIncreaseNoticeEvent;
import cn.evole.onebot.sdk.util.MsgUtils;
import lombok.val;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.Component;
//#if MC <11900
import net.minecraft.network.chat.TextComponent;
//#endif

/**
* Description:
Expand Down Expand Up @@ -47,22 +54,45 @@ public void onMessage(GroupMessageEvent event) {

String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片


if (ModConfig.INSTANCE.getCmd().isQqChatPrefixOn()) {
val split = event.getMessage().split(" ");
if (ModConfig.INSTANCE.getCmd().getQqChatPrefix().equals(split[0])) //指定前缀发送
send = split[1];
else return;
}

val nick = McBot.bot.getGroupMemberInfo(event.getGroupId(), event.getUserId(), true);
String groupNick = ModConfig.INSTANCE.getCmd().isGroupNickOn() // 是否使用群昵称
? nick == null ? event.getSender().getCard() : nick.getData().getCard() // 防止api返回为空
: event.getSender().getNickname();

String toSend = ModConfig.INSTANCE.getCmd().isGamePrefixOn()
String finalMsg = ModConfig.INSTANCE.getCmd().isGamePrefixOn()
? ModConfig.INSTANCE.getCmd().isIdGamePrefixOn()
? String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ModConfig.INSTANCE.getCmd().getQqGamePrefix(), event.getGroupId(), groupNick, send)
: String.format("§b[§l%s§b]§a<%s>§f %s", ModConfig.INSTANCE.getCmd().getQqGamePrefix(), groupNick, send)
: String.format("§a<%s>§f %s", groupNick, send);

ChatRecordApi.add(String.valueOf(event.getMessageId()), String.valueOf(event.getGroupId()), String.valueOf(event.getSelfId()), finalMsg);


//todo 撤回机制?
// val recallCmd = "/mcbot recall" + event.getMessageId();
// val end = " [撤回]";
// //#if MC >= 11900
// //$$ val recall = Component.literal(end).withStyle(ChatFormatting.BLUE).setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, recallCmd)));
// //$$ val toSend = Component.literal(finalMsg).append(recall);
// //#else
// val recall = new TextComponent(end).withStyle(ChatFormatting.BLUE).setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, recallCmd)));
// val toSend = new TextComponent(finalMsg).append(recall);

//#endif
//#if MC >= 11900
//$$ val toSend = Component.literal(finalMsg);
//#else
val toSend = new TextComponent(finalMsg);
//#endif

ITickEvent.getSendQueue().add(toSend);
}
}
Expand Down Expand Up @@ -137,11 +167,19 @@ public void onMessage(GuildMessageEvent event) {
: event.getSender().getNickname();


String toSend = ModConfig.INSTANCE.getCmd().isGamePrefixOn()
String finalMsg = ModConfig.INSTANCE.getCmd().isGamePrefixOn()
? ModConfig.INSTANCE.getCmd().isIdGamePrefixOn()
? String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ModConfig.INSTANCE.getCmd().getGuildGamePrefix(), event.getChannelId(), guildNick, send)
: String.format("§b[§l%s§b]§a<%s>§f %s", ModConfig.INSTANCE.getCmd().getGuildGamePrefix(), guildNick, send)
: String.format("§a<%s>§f %s", guildNick, send);


//#if MC >= 11900
//$$ val toSend = Component.literal(finalMsg);
//#else
val toSend = new TextComponent(finalMsg);
//#endif

ITickEvent.getSendQueue().add(toSend);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import cn.evole.mods.mcbot.command.*;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.LongArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.arguments.*;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
Expand Down Expand Up @@ -59,6 +56,9 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher){
.then(Commands.argument("GuildId", StringArgumentType.greedyString())
.executes(GuildIDCommand::execute)))
.then(Commands.literal("help").executes(HelpCommand::execute))
.then(Commands.literal("recall")
.then(Commands.argument("MessageId", IntegerArgumentType.integer())
.executes(RecallCommand::execute)))
.then(Commands.literal("debug")
.then(Commands.argument("enabled", BoolArgumentType.bool())
.executes(DebugCommand::execute)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.evole.mods.mcbot.init.event;

import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
import java.util.LinkedList;
import java.util.Queue;
Expand All @@ -21,24 +22,24 @@
* Version: 1.0
*/
public class ITickEvent {
private static final Queue<String> SEND_QUEUE = new LinkedList<>();
private static final Queue<MutableComponent> SEND_QUEUE = new LinkedList<>();

public static Queue<String> getSendQueue() {
public static Queue<MutableComponent> getSendQueue() {
return SEND_QUEUE;
}


public static void register(MinecraftServer server) {
String toSend = SEND_QUEUE.poll();
MutableComponent toSend = SEND_QUEUE.poll();
if (ModConfig.INSTANCE != null
&& server != null
&& server.isDedicatedServer()
&& toSend != null
) {
//#if MC >= 11900
//$$ server.getPlayerList().broadcastSystemMessage(Component.literal(toSend), false);
//$$ server.getPlayerList().broadcastSystemMessage(toSend, false);
//#else
server.getPlayerList().broadcastMessage(new TextComponent(toSend), ChatType.SYSTEM, Util.NIL_UUID);
server.getPlayerList().broadcastMessage(toSend, ChatType.SYSTEM, Util.NIL_UUID);
//#endif
}
}
Expand Down

0 comments on commit 3d0e64a

Please sign in to comment.