Skip to content

Commit

Permalink
refactor(robot): 指令管理员检测
Browse files Browse the repository at this point in the history
  • Loading branch information
msojocs committed Nov 28, 2021
1 parent 46c2550 commit 38ff146
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cn/wecuit/robot/PluginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static void register(){
if(clazz.isAnnotationPresent(MainCmd.class)){
// 有主指令
MainCmd mainCmd = clazz.getAnnotation(MainCmd.class);
// 触发关键词
String keyword = mainCmd.keyword();
StringBuilder desc = new StringBuilder(mainCmd.desc()).append("\n");
cmd2plugin.put(keyword, cmdMap);
Expand All @@ -91,7 +92,6 @@ public static void register(){
}else {
// 非指令类型插件
for (Method method : methods) {
//method.getAnnotation();
if (method.isAnnotationPresent(RobotEventHandle.class)) {
RobotEventHandle annotation = method.getAnnotation(RobotEventHandle.class);
for (EventType eventType : annotation.event()) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cn/wecuit/robot/entity/SubCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@
* 是否注册为一级指令
**/
boolean regAsMainCmd() default false;

/**
* 是否需要管理员权限
*/
boolean requireAdmin() default false;
}
84 changes: 35 additions & 49 deletions src/main/java/cn/wecuit/robot/plugins/MessagePlugin.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cn.wecuit.robot.plugins;

import cn.wecuit.robot.PluginHandler;
import cn.wecuit.robot.entity.CmdList;
import cn.wecuit.robot.entity.EventType;
import cn.wecuit.robot.entity.RobotEventHandle;
import cn.wecuit.robot.entity.RobotPlugin;
import cn.wecuit.robot.entity.*;
import cn.wecuit.robot.plugins.msg.AdminPlugin;
import cn.wecuit.robot.plugins.msg.MsgPlugin;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.event.events.GroupMessageEvent;
Expand Down Expand Up @@ -82,28 +80,7 @@ public void handleMsg(MessageEvent event) {
try {
if (action instanceof Method) {
// 一级指令对应方法
Method method = (Method) action;
Parameter[] parameters = method.getParameters();
if(parameters.length > 0) {
Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
String name = parameters[i].getType().getSimpleName();
if (EventType.MessageEvent.name().equals(name)
|| (event instanceof GroupMessageEvent && EventType.GroupMessageEvent.name().equals(name))
|| (event instanceof GroupTempMessageEvent && EventType.GroupTempMessageEvent.name().equals(name))) {
args[i] = event;
} else if ("CmdList".equals(name)) {
args[i] = cmdList;
} else {
log.info("无法识别的参数类型:{} - {}", name, parameters[i].getType().getName());
event.getSubject().sendMessage("该指令只能通过如下渠道发送:" + EventType.valueOf(name).getMessage());
return;
}
}
method.invoke(method.getDeclaringClass().newInstance(), args);
}else{
method.invoke(method.getDeclaringClass().newInstance());
}
callMethod(event, cmdList, (Method) action);
} else if (action instanceof Map) {
Map<String, Object> subCmd = ((Map<String, Object>) action);
String c = cmdList.get(0);
Expand All @@ -124,29 +101,7 @@ public void handleMsg(MessageEvent event) {
// 指令匹配
if (ac instanceof Method) {
// 指令对应方法
Method method = (Method) ac;
Parameter[] parameters = method.getParameters();
MsgPlugin o = (MsgPlugin) method.getDeclaringClass().newInstance();
if(parameters.length > 0) {
Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
String name = parameters[i].getType().getSimpleName();
if (EventType.MessageEvent.name().equals(name)
|| (event instanceof GroupMessageEvent && EventType.GroupMessageEvent.name().equals(name))
|| (event instanceof GroupTempMessageEvent && EventType.GroupTempMessageEvent.name().equals(name))) {
args[i] = event;
} else if ("CmdList".equals(name)) {
args[i] = cmdList;
} else {
log.info("无法识别的参数类型:{} - {}", name, parameters[i].getType().getName());
event.getSubject().sendMessage("该指令只能通过如下渠道发送:" + EventType.valueOf(name).getMessage());
return;
}
}
method.invoke(o, args);
}else{
method.invoke(o);
}
callMethod(event, cmdList, (Method) ac);
}
}

Expand All @@ -156,4 +111,35 @@ public void handleMsg(MessageEvent event) {

}

private void callMethod(MessageEvent event, CmdList cmdList, Method action) throws InstantiationException, IllegalAccessException, InvocationTargetException {
Method method = action;
Parameter[] parameters = method.getParameters();
SubCmd annotation = method.getAnnotation(SubCmd.class);
if(annotation.requireAdmin() && !AdminPlugin.isSuperAdmin(String.valueOf(event.getSender().getId()))){
event.getSubject().sendMessage("权限不足");
return;
}
MsgPlugin o = (MsgPlugin) method.getDeclaringClass().newInstance();
if(parameters.length > 0) {
Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
String name = parameters[i].getType().getSimpleName();
if (EventType.MessageEvent.name().equals(name)
|| (event instanceof GroupMessageEvent && EventType.GroupMessageEvent.name().equals(name))
|| (event instanceof GroupTempMessageEvent && EventType.GroupTempMessageEvent.name().equals(name))) {
args[i] = event;
} else if ("CmdList".equals(name)) {
args[i] = cmdList;
} else {
log.info("无法识别的参数类型:{} - {}", name, parameters[i].getType().getName());
event.getSubject().sendMessage("该指令只能通过如下渠道发送:" + EventType.valueOf(name).getMessage());
return;
}
}
method.invoke(o, args);
}else{
method.invoke(o);
}
}

}
29 changes: 11 additions & 18 deletions src/main/java/cn/wecuit/robot/plugins/msg/AdminPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ public class AdminPlugin extends MsgPluginImpl {
put("adminList", adminList);
}};

@SubCmd(keyword = "添加管理", desc = "添加机器人管理员")
@SubCmd(keyword = "添加管理", desc = "添加机器人管理员", requireAdmin = true)
public boolean addAdmin(GroupMessageEvent event, CmdList cmds){
if(!isSuperAdmin(event)){
event.getSubject().sendMessage("权限不足");
return true;
}
String newAdmin = cmds.get(0);
if(!adminList.contains(newAdmin)) {
adminList.add(newAdmin);
Expand All @@ -42,24 +38,16 @@ public boolean addAdmin(GroupMessageEvent event, CmdList cmds){
}
return true;
}
@SubCmd(keyword = "删除管理", desc = "删除指定机器人管理员")
@SubCmd(keyword = "删除管理", desc = "删除指定机器人管理员", requireAdmin = true)
public boolean delAdmin(GroupMessageEvent event, CmdList cmds){
if(!isSuperAdmin(event)){
event.getSubject().sendMessage("权限不足");
return true;
}
String newAdmin = cmds.get(0);
adminList.remove(newAdmin);
updatePluginData();
event.getSubject().sendMessage("OK");
return true;
}
@SubCmd(keyword = "列出管理", desc = "列出所有机器人管理员")
@SubCmd(keyword = "列出管理", desc = "列出所有机器人管理员", requireAdmin = true)
public boolean listAdmin(GroupMessageEvent event){
if(!isSuperAdmin(event)){
event.getSubject().sendMessage("权限不足");
return true;
}
StringBuilder list = new StringBuilder();
for (String s : adminList) {
list.append(s).append("\n");
Expand All @@ -72,15 +60,20 @@ public boolean listAdmin(GroupMessageEvent event){
public void initPluginData(Map<String, Object> config){
List<String> al = (List<String>) config.get("adminList");
if(al != null){
// 将默认管理员合并
al.addAll(adminList);
// 唯一化
al = al.stream().distinct().collect(Collectors.toList());
// 清空默认
adminList.clear();
// 加入管理
adminList.addAll(al);
}
}
void updatePluginData(){
updatePluginData(pluginData);
}
private boolean isSuperAdmin(GroupMessageEvent event){
long id = event.getSender().getId();
return id == 1690127128L;
public static boolean isSuperAdmin(String id){
return adminList.contains(id);
}
}
21 changes: 5 additions & 16 deletions src/main/java/cn/wecuit/robot/plugins/msg/BanPlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.wecuit.robot.plugins.msg;

import cn.wecuit.robot.data.DataHandle;
import cn.wecuit.robot.entity.CmdList;
import cn.wecuit.robot.entity.MainCmd;
import cn.wecuit.robot.entity.RobotPlugin;
Expand Down Expand Up @@ -37,9 +36,8 @@ public class BanPlugin extends MsgPluginImpl {
// put("enabledList", enabledList);
}};

@SubCmd(keyword = "添加违禁表达式")
@SubCmd(keyword = "添加违禁表达式", requireAdmin = true)
public void addRuleItem(GroupMessageEvent event, CmdList cmds){
if(!isAdmin(event))return;

String cmd = cmds.get(0);
String groupId = Long.toString(event.getSubject().getId());
Expand All @@ -55,9 +53,8 @@ public void addRuleItem(GroupMessageEvent event, CmdList cmds){
}
}

@SubCmd(keyword = "查看违禁表达式")
@SubCmd(keyword = "查看违禁表达式", requireAdmin = true)
public void viewRuleItem(GroupMessageEvent event){
if(!isAdmin(event))return;

List<String> list = banRuleItems.get(Long.toString(event.getSubject().getId()));
StringBuilder msg = new StringBuilder("以下为违禁规则:\n");
Expand All @@ -67,9 +64,8 @@ public void viewRuleItem(GroupMessageEvent event){
event.getSubject().sendMessage(msg.toString());
}

@SubCmd(keyword = "删除违禁表达式")
@SubCmd(keyword = "删除违禁表达式", requireAdmin = true)
public void delRuleItem(GroupMessageEvent event, CmdList cmds){
if(!isAdmin(event))return;

String id = cmds.get(0);
String groupId = Long.toString(event.getSubject().getId());
Expand All @@ -90,9 +86,8 @@ public void delRuleItem(GroupMessageEvent event, CmdList cmds){

}

@SubCmd(keyword = "清空违禁表达式")
@SubCmd(keyword = "清空违禁表达式", requireAdmin = true)
public void clearRuleItem(GroupMessageEvent event){
if(!isAdmin(event))return;

String groupId = Long.toString(event.getSubject().getId());
banRuleItems.remove(groupId);
Expand All @@ -101,9 +96,8 @@ public void clearRuleItem(GroupMessageEvent event){

}

@SubCmd(keyword = "测试违禁表达式", desc = "测试违禁表达式 内容")
@SubCmd(keyword = "测试违禁表达式", desc = "测试违禁表达式 内容", requireAdmin = true)
public void testRuleItem(GroupMessageEvent event){
if(!isAdmin(event))return;

String s = event.getMessage().contentToString();
String groupId = Long.toString(event.getSubject().getId());
Expand Down Expand Up @@ -156,9 +150,4 @@ public void updatePluginData(){
updatePluginData(pluginData);
}

private boolean isAdmin(GroupMessageEvent event){
String id = Long.toString(event.getSender().getId());
if(!DataHandle.isAdmin(id))event.getSubject().sendMessage("你的权限不够啊~(>_<。)\");
return DataHandle.isAdmin(id);
}
}
5 changes: 0 additions & 5 deletions src/main/java/cn/wecuit/robot/plugins/msg/ChatPlugin.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package cn.wecuit.robot.plugins.msg;

import cn.wecuit.backen.utils.SpringUtil;
import cn.wecuit.robot.entity.CmdList;
import cn.wecuit.robot.entity.MainCmd;
import cn.wecuit.robot.entity.RobotPlugin;
import cn.wecuit.robot.entity.SubCmd;
import cn.wecuit.robot.provider.WSeg;
import cn.wecuit.robot.services.RbDictService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.code.MiraiCode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -45,7 +42,6 @@ public boolean chat(GroupMessageEvent event){
// 是否开启聊天模式
if(!enabledList.contains(subjectId))return false;


log.info("开始分词");
List<String> keys = WSeg.seg(msg);

Expand All @@ -64,7 +60,6 @@ public boolean chat(GroupMessageEvent event){
if(msgList.size() > 0) {
int i = (int) (Math.random() * (msgList.size()));
msg = msgList.get(i);

event.getSubject().sendMessage(MiraiCode.deserializeMiraiCode(msg));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/wecuit/robot/plugins/msg/HPicPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class HPicPlugin extends MsgPluginImpl {
put("config", config);
}};

@SubCmd(keyword = "开启")
@SubCmd(keyword = "开启", requireAdmin = true)
public boolean enablePic(GroupMessageEvent event, CmdList cmds){
Contact subject = event.getSubject();
String subjectId = Long.toString(subject.getId());
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/cn/wecuit/robot/plugins/msg/MenuPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ public boolean getMenu(MessageEvent event){
}

@Override
public void initPluginData(Map<String, Object> config) {

}
public void initPluginData(Map<String, Object> config) {}
}
24 changes: 4 additions & 20 deletions src/main/java/cn/wecuit/robot/plugins/msg/NewsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ public class NewsPlugin extends MsgPluginImpl {
put("enabledList", enabledList);
}};

@SubCmd(keyword = "开启推送", desc = "开启对应群聊的新闻推送功能")
@SubCmd(keyword = "开启推送", desc = "开启对应群聊的新闻推送功能", requireAdmin = true)
public boolean enablePush(GroupMessageEvent event){
long senderId = event.getSender().getId();
if(!checkAdmin(senderId))
event.getSubject().sendMessage("没有权限");

String fromId = Long.toString(event.getSubject().getId());
boolean newsNotice = enabledList.contains(fromId);
Expand All @@ -53,11 +50,8 @@ public boolean enablePush(GroupMessageEvent event){
return true;
}

@SubCmd(keyword = "关闭推送", desc = "关闭对应群聊的新闻推送功能")
@SubCmd(keyword = "关闭推送", desc = "关闭对应群聊的新闻推送功能", requireAdmin = true)
public boolean disablePush(GroupMessageEvent event){
long senderId = event.getSender().getId();
if(!checkAdmin(senderId))
event.getSubject().sendMessage("没有权限");

String fromId = Long.toString(event.getSubject().getId());
boolean newsNotice = enabledList.contains(fromId);
Expand All @@ -71,11 +65,8 @@ public boolean disablePush(GroupMessageEvent event){
return true;
}

@SubCmd(keyword = "添加推送目标", desc = "参数[群号],添加指定群号到推送列表")
@SubCmd(keyword = "添加推送目标", desc = "参数[群号],添加指定群号到推送列表", requireAdmin = true)
public boolean addPushTarget(GroupMessageEvent event, CmdList cmds){
long senderId = event.getSender().getId();
if(!checkAdmin(senderId))
event.getSubject().sendMessage("没有权限");

if(cmds.size() != 1){
event.getSubject().sendMessage("阁下的指令格式好像不太对呢(・∀・(・∀・(・∀・*)");
Expand All @@ -93,11 +84,8 @@ public boolean addPushTarget(GroupMessageEvent event, CmdList cmds){
}

// 推送测试
@SubCmd(keyword = "推送测试", desc = "在当前群进行一次推送测试")
@SubCmd(keyword = "推送测试", desc = "在当前群进行一次推送测试", requireAdmin = true)
public boolean pushTest(GroupMessageEvent event) throws IOException {
long senderId = event.getSender().getId();
if(!checkAdmin(senderId))
event.getSubject().sendMessage("没有权限");
String subjectId = Long.toString(event.getSubject().getId());
NewsService newsService = SpringUtil.getBean(NewsService.class);
newsService.newsNotice(new ArrayList<String>(){{add(subjectId);}});
Expand Down Expand Up @@ -139,10 +127,6 @@ public boolean query(GroupMessageEvent event, List<String> cmds){
}
return true;
}
public static boolean checkAdmin(Long id){
return id == 1690127128L;
}

public void updatePluginData(){
updatePluginData(pluginData);
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/cn/wecuit/robot/plugins/msg/RepeatPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ public boolean enableMode(GroupMessageEvent event){
}

@Override
public void initPluginData(Map<String, Object> config) {

}
public void initPluginData(Map<String, Object> config) {}
}
Loading

0 comments on commit 38ff146

Please sign in to comment.