Skip to content

Commit

Permalink
2024/12/16 添加全局管理员强制对话功能和全局屏蔽黑名单
Browse files Browse the repository at this point in the history
  • Loading branch information
haishen668 committed Dec 16, 2024
1 parent b007940 commit e57589e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ltd.dreamcraft</groupId>
<artifactId>XinxinCustomMessage</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>jar</packaging>

<name>XinxinCustomMessage</name>
Expand Down Expand Up @@ -94,7 +94,7 @@
<artifactId>XinxinBotApi</artifactId>
<version>2.0.6-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/XinxinBotApi-2.0.6-SNAPSHOT.jar</systemPath>
<systemPath>${project.basedir}/libs/XinxinBotApi-2.1.4-SNAPSHOT.jar</systemPath>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ public void onMsg(GroupMessageEvent event) {
if (XinxinCustomMessage.LOG)
XinxinCustomMessage.getInstance().getLogger().info("§a群[" + event.getGroup_id() + "]: §b" + event.getMessage());


// 设置用户代理对象
long userIdProxy = event.getUser_id();

// 设置消息代理对象
String messageProxy = event.getMessage();

List<String> blackList = XinxinCustomMessage.getInstance().getConfig().getStringList("global-setting.black_list");
// 屏蔽黑名单用户的消息
if (blackList != null && !blackList.isEmpty() && blackList.contains(String.valueOf(userIdProxy))) {
return;
}

// 全局管理员 控制对话
List<String> adminsList = XinxinCustomMessage.getInstance().getConfig().getStringList("global-setting.admins");
if (adminsList != null && !adminsList.isEmpty() && adminsList.contains(String.valueOf(userIdProxy))) {
// 去除@的CQ码 及其前后空白字符串
if (messageProxy.startsWith("[CQ:at,qq=")) {
String regex = "\\[CQ:at,qq=(\\d+)\\]";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(messageProxy);
if (matcher.find()) {
// 赋值给代理对象
userIdProxy = Long.parseLong(matcher.group(1));
// 替换掉匹配到的 CQ 码 并且去除空白字符
messageProxy = matcher.replaceAll("").trim();
}
}
}

for (CustomMessage customMessage : XinxinCustomMessage.customMessageList) {//遍历所有信息
// 检查信息触发群 是否在监听的列表中
if (!customMessage.groups.isEmpty() && !customMessage.groups.contains(event.getGroup_id())) {
Expand All @@ -63,7 +94,7 @@ public void onMsg(GroupMessageEvent event) {
boolean regex = false; //标记:是否是正则表达式
if (customMessage.trigger.startsWith("[regex]")) { //是否以正则表达式 开头
String pattern = customMessage.trigger.replace("[regex]", "").trim();
Matcher matcher = Pattern.compile(pattern).matcher(event.getMessage());
Matcher matcher = Pattern.compile(pattern).matcher(messageProxy);
if (matcher.find())
regex = true;
}
Expand All @@ -76,9 +107,9 @@ public void onMsg(GroupMessageEvent event) {
// continue;
// }
//
if (event.getMessage().equalsIgnoreCase(customMessage.trigger) || regex || customMessage.trigger.contains("{extra}")) {
if (messageProxy.equalsIgnoreCase(customMessage.trigger) || regex || customMessage.trigger.contains("{extra}")) {
//信息和关键词完全匹配 || 符合正则表达式 || (是否包含{extra} => 关键词中包含触发器)
String message = event.getMessage();
String message = messageProxy;
String trigger = customMessage.trigger;

if (trigger.contains("{extra}")) {
Expand All @@ -99,14 +130,14 @@ public void onMsg(GroupMessageEvent event) {
}
}

if (customMessage.admins.isEmpty() || customMessage.admins.contains(event.getUser_id())) {
String bindPlayerName = BotBind.getBindPlayerName(String.valueOf(event.getUser_id()));
if (customMessage.admins.isEmpty() || customMessage.admins.contains(userIdProxy)) {
String bindPlayerName = BotBind.getBindPlayerName(String.valueOf(userIdProxy));
String extra = !regex ? message : ""; //正则没匹配到就返回 message
if (customMessage.unbind_messages.isEmpty() || bindPlayerName != null) {
if (customMessage.scripts.isEmpty()) {
MessageUtil.sendMessage(customMessage.responses, event.getGroup_id(), event.getUser_id(), bindPlayerName, extra);
MessageUtil.sendMessage(customMessage.responses, event.getGroup_id(), userIdProxy, bindPlayerName, extra);
return;
}else {
} else {
ScriptEngine scriptEngine = XinxinCustomMessage.getScriptEngine();
List<String> scripts = customMessage.getScripts();
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(bindPlayerName);
Expand Down Expand Up @@ -137,11 +168,11 @@ public void onMsg(GroupMessageEvent event) {
}
}
}
MessageUtil.sendMessage(customMessage.responses, event.getGroup_id(), event.getUser_id(), bindPlayerName, extra);
MessageUtil.sendMessage(customMessage.responses, event.getGroup_id(), userIdProxy, bindPlayerName, extra);
return;
}
}
MessageUtil.sendMessage(customMessage.unbind_messages, event.getGroup_id(), event.getUser_id(), null, extra);
MessageUtil.sendMessage(customMessage.unbind_messages, event.getGroup_id(), userIdProxy, null, extra);
} else {
BotAction.sendGroupMessage(event.getGroup_id(), "你没有权限使用该指令", true);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
debug: false
#是否开启图片识别(功能暂未启用)
ocr-scan: true
#消息分类存放的文件夹
messagefolders:
- messages
#全局设置
global-setting:
#全局管理员(可以通过@用户来控制其执行对话)
admins:
- "2821396723" #请自行修改为自己的
#全局黑名单(黑名单中的人无法触发任何对话)
black_list:
- "1740023584"
custom_messages:
帮助:
trigger: "help"
Expand Down

0 comments on commit e57589e

Please sign in to comment.