mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Change] 调整群禁言管理职责;
[Delete] BotEventHandler 移除禁言状态管理相关功能; [Add] GroupMuteManager, GroupMuteManagerTest 增加群禁言状态管理类; [Change] MiraiMain 将禁言状态对接由使用BotEventHandler改为独立管理;
This commit is contained in:
parent
0fc3e3ab48
commit
4387da37f5
@ -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,8 +39,6 @@ public class BotEventHandler implements EventHandler {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(BotEventHandler.class);
|
||||
|
||||
private final static Map<Long, AtomicBoolean> muteStateMap = new Hashtable<>();
|
||||
|
||||
/**
|
||||
* 消息事件执行器
|
||||
*/
|
||||
@ -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 ? "已禁言" : "已解除");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
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;
|
||||
@ -30,7 +31,9 @@ public class MiraiMain implements Closeable {
|
||||
|
||||
private Bot bot;
|
||||
|
||||
private final static Properties botProperties = new Properties();
|
||||
private final Properties botProperties = new Properties();
|
||||
|
||||
private final GroupMuteManager muteManager = new GroupMuteManager();
|
||||
|
||||
public void init() {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||
@ -73,9 +76,9 @@ public class MiraiMain implements Closeable {
|
||||
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();
|
||||
@ -90,9 +93,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));
|
||||
|
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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即可
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user