From 48237d9aad15c91d1a3bb6cfb872abfd49f88a96 Mon Sep 17 00:00:00 2001 From: dullwolf <2547170055@qq.com> Date: Fri, 3 Jun 2022 22:56:54 +0800 Subject: [PATCH] update --- src/main/java/com/las/cmd/admin/ResetFun.java | 2 +- src/main/java/com/las/cmd/admin/ResetGp.java | 47 +++++++++++++++++++ .../java/com/las/cmd/admin/ResetWeight.java | 36 +++++++++----- .../java/com/las/cmd/group/GroupJoinTip.java | 23 +++++---- src/main/java/com/las/dao/UserDao.java | 23 ++++++++- .../las/strategy/handle/BotMsgHandler.java | 22 +++++++-- .../strategy/handle/MemberJoinMsgHandler.java | 16 ++++--- 7 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/las/cmd/admin/ResetGp.java diff --git a/src/main/java/com/las/cmd/admin/ResetFun.java b/src/main/java/com/las/cmd/admin/ResetFun.java index bb96ff9..5129eea 100644 --- a/src/main/java/com/las/cmd/admin/ResetFun.java +++ b/src/main/java/com/las/cmd/admin/ResetFun.java @@ -23,7 +23,7 @@ public void execute(Long userId, Long id, Integer type, String command, ArrayLis if (null != userId) { long superQQ = Long.parseLong(AppConfigs.SUPER_QQ); if (userId != superQQ) { - CmdUtil.sendMessage("必须是超管才可以更新机器人权限", userId, id, type); + CmdUtil.sendMessage("必须是超管才可以更新机器人功能", userId, id, type); } else { initBotFun(); CmdUtil.sendMessage("更新成功", userId, id, type); diff --git a/src/main/java/com/las/cmd/admin/ResetGp.java b/src/main/java/com/las/cmd/admin/ResetGp.java new file mode 100644 index 0000000..c9973b8 --- /dev/null +++ b/src/main/java/com/las/cmd/admin/ResetGp.java @@ -0,0 +1,47 @@ +package com.las.cmd.admin; + +import cn.hutool.core.collection.CollectionUtil; +import com.las.annotation.BotCmd; +import com.las.cmd.Command; +import com.las.common.Constant; +import com.las.config.AppConfigs; +import com.las.model.GroupExt; +import com.las.utils.CmdUtil; +import org.apache.log4j.Logger; + +import java.util.ArrayList; + +@BotCmd(funName = "超管功能", funWeight = 999) +public class ResetGp extends Command { + + private static Logger logger = Logger.getLogger(ResetGp.class); + + + public ResetGp() { + super("群设置", "GP"); + } + + @Override + public void execute(Long userId, Long id, Integer type, String command, ArrayList args) throws Exception { + long superQQ = Long.parseLong(AppConfigs.SUPER_QQ); + if (userId != superQQ) { + CmdUtil.sendMessage("必须是超管才可以设置群配置", userId, id, type); + } else { + if (Constant.MESSAGE_TYPE_GROUP == type) { + if (CollectionUtil.isNotEmpty(args)) { + // 获得前缀 + String prefix = args.get(0).trim(); + if ("空".equals(prefix) || "None".equalsIgnoreCase(prefix)) { + prefix = ""; + } + GroupExt groupExt = getGroupExtDao().findByGid(id); + if (null != groupExt) { + groupExt.setAttribute2(prefix); + } + getGroupExtDao().saveOrUpdate(groupExt); + CmdUtil.sendMessage("更新成功", userId, id, type); + } + } + } + } +} diff --git a/src/main/java/com/las/cmd/admin/ResetWeight.java b/src/main/java/com/las/cmd/admin/ResetWeight.java index bf3f3c6..a1eba07 100644 --- a/src/main/java/com/las/cmd/admin/ResetWeight.java +++ b/src/main/java/com/las/cmd/admin/ResetWeight.java @@ -18,7 +18,7 @@ public class ResetWeight extends Command { public ResetWeight() { - super("权限赋能", "WeightUp"); + super("权限赋能", "权限更新"); } @Override @@ -28,25 +28,35 @@ public void execute(Long userId, Long id, Integer type, String command, ArrayLis if (userId != superQQ) { CmdUtil.sendMessage("必须是超管才可以更新机器人权限", userId, id, type); } else { - // 给用户设置权限 1~995 - // 996、997、998 分别是普通管理员、中级管理员、高级管理员 if (args.size() == 2) { // 必须只传两个参数,例如 权限赋能 用户QQ号 权限值 String qq = args.get(0); String weight = args.get(1); if (StrUtils.isNumeric(qq) && StrUtils.isNumeric(weight)) { - User qqUser = getUserDao().findByUid(Long.parseLong(qq)); - if (null == qqUser) { - // 数据库不存在,直接创建 - qqUser = new User(); - qqUser.setRemark("初次初始化权限赋能"); + User qqUser = null; + if (command.startsWith("权限赋能") && Integer.parseInt(weight) > 995) { + // 权限赋能 996、997、998 分别是普通管理员、中级管理员、高级管理员 + qqUser = getUserDao().findGroupUser(Long.parseLong(qq)); + if (null == qqUser) { + // 数据库不存在,直接创建并且一定要设置备注 + qqUser = new User(); + qqUser.setRemark("初次初始化权限赋能"); + } + } else if (command.startsWith("权限更新") && Integer.parseInt(weight) < 996) { + // 权限更新 是给用户设置权限 1~995 + qqUser = getUserDao().findByUid(Long.parseLong(qq)); + if (null == qqUser) { + qqUser = new User(); + } + } + if (null != qqUser) { + qqUser.setBotQQ(Long.parseLong(AppConfigs.BOT_QQ)); + qqUser.setUserId(Long.parseLong(qq)); + qqUser.setFunPermission(Integer.parseInt(weight)); + getUserDao().saveOrUpdate(qqUser); + CmdUtil.sendMessage("更新成功", userId, id, type); } - qqUser.setBotQQ(Long.parseLong(AppConfigs.BOT_QQ)); - qqUser.setUserId(Long.parseLong(qq)); - qqUser.setFunPermission(Integer.parseInt(weight)); - getUserDao().saveOrUpdate(qqUser); } - CmdUtil.sendMessage("更新成功", userId, id, type); } } } diff --git a/src/main/java/com/las/cmd/group/GroupJoinTip.java b/src/main/java/com/las/cmd/group/GroupJoinTip.java index 425a64d..455419d 100644 --- a/src/main/java/com/las/cmd/group/GroupJoinTip.java +++ b/src/main/java/com/las/cmd/group/GroupJoinTip.java @@ -2,6 +2,7 @@ import com.las.annotation.BotCmd; import com.las.cmd.Command; +import com.las.common.Constant; import com.las.model.GroupExt; import com.las.utils.CmdUtil; import com.las.utils.StrUtils; @@ -12,20 +13,26 @@ public class GroupJoinTip extends Command { public GroupJoinTip() { - super("群欢迎设置", "groupTip"); + super("群欢迎设置", "groupTip","gt"); } @Override public void execute(Long userId, Long id, Integer type, String command, ArrayList args) throws Exception { - if (args.size() == 2) { - // 必须只传两个参数,例如 群欢迎设置 群号 欢迎提示(带“空”字表示取消) - String gId = args.get(0); - String tip = args.get(1); - if (StrUtils.isNumeric(gId) && StrUtils.isNotEmpty(tip)) { - if ("空".equals(tip) || "None".equalsIgnoreCase(tip)) { + if (Constant.MESSAGE_TYPE_GROUP == type) { + // 必须是在群里使用,例如 群欢迎设置 欢迎提示(带“空”字表示取消) + String tip = null; + if (args.size() > 0) { + StringBuilder sb = new StringBuilder(); + for (String s : args) { + sb.append(s).append(" "); + } + tip = sb.toString(); + } + if (StrUtils.isNotEmpty(tip)) { + if ("空".equals(tip.trim()) || "None".equalsIgnoreCase(tip.trim())) { tip = ""; } - GroupExt groupExt = getGroupExtDao().findByGid(Long.parseLong(gId)); + GroupExt groupExt = getGroupExtDao().findByGid(id); if (null != groupExt) { groupExt.setAttribute1(tip); getGroupExtDao().saveOrUpdate(groupExt); diff --git a/src/main/java/com/las/dao/UserDao.java b/src/main/java/com/las/dao/UserDao.java index ca09688..58e5a4c 100644 --- a/src/main/java/com/las/dao/UserDao.java +++ b/src/main/java/com/las/dao/UserDao.java @@ -29,11 +29,32 @@ public User findSuperQQ() throws Exception { /** * 根据用户QQ查找一条记录 * + * ps:排除初次初始化权限赋能的,因为群管理员和普通用户用同一个权限字段 fun_weight, + * 只能通过remark区分,大部分企业公司都是用这招的!! + * * @param uid 用户QQ * @return User */ public User findByUid(Long uid) { - String sql = "select * from `user` where user_id = ? and bot_qq = ?"; + String sql = "select * from `user` where user_id = ? and bot_qq = ? and remark <> '初次初始化权限赋能'"; + User user = null; + try { + user = getRunner().query(sql, new BeanHandler<>(User.class, getProcessor()), uid, Long.parseLong(AppConfigs.BOT_QQ)); + } catch (SQLException e) { + e.printStackTrace(); + } + return user; + } + + /** + * + * 查找用户是否是群管理员,权限有996、997、998 + * + * @param uid 用户QQ + * @return User + */ + public User findGroupUser(Long uid) { + String sql = "select * from `user` where user_id = ? and bot_qq = ? and remark = '初次初始化权限赋能'"; User user = null; try { user = getRunner().query(sql, new BeanHandler<>(User.class, getProcessor()), uid, Long.parseLong(AppConfigs.BOT_QQ)); diff --git a/src/main/java/com/las/strategy/handle/BotMsgHandler.java b/src/main/java/com/las/strategy/handle/BotMsgHandler.java index b5e95e1..c9377be 100644 --- a/src/main/java/com/las/strategy/handle/BotMsgHandler.java +++ b/src/main/java/com/las/strategy/handle/BotMsgHandler.java @@ -352,11 +352,23 @@ private boolean checkExe(Long userId, Long id, int type, BotCmd botCmd) { if (AppConfigs.SUPER_QQ.equals(userId.toString())) { user.setFunPermission(Constant.SUPER_PERMISSION); } else { - if (user.getFunPermission() < funWeight) { - isExecute = false; - // 用户权限小于功能权限,则返回错误信息(非匹配指令不需要) - if (botCmd.isMatch()) { - logger.warn("用户:" + userId + " 权限不足,请联系管理员"); + if (funWeight < 996) { + if (user.getFunPermission() < funWeight) { + isExecute = false; + // 用户权限小于功能权限,则返回错误信息(非匹配指令不需要) + if (botCmd.isMatch()) { + logger.warn("用户:" + userId + " 权限不足,请联系管理员"); + } + } + } else { + // 管理员的功能需要查找管理员初始用户 + User groupUser = getUserDao().findGroupUser(userId); + if (null == groupUser || groupUser.getFunPermission() < funWeight) { + isExecute = false; + // 用户权限小于功能权限,则返回错误信息(非匹配指令不需要) + if (botCmd.isMatch()) { + logger.warn("群管:" + userId + " 权限不足,请联系超管"); + } } } } diff --git a/src/main/java/com/las/strategy/handle/MemberJoinMsgHandler.java b/src/main/java/com/las/strategy/handle/MemberJoinMsgHandler.java index e60d43c..8226714 100644 --- a/src/main/java/com/las/strategy/handle/MemberJoinMsgHandler.java +++ b/src/main/java/com/las/strategy/handle/MemberJoinMsgHandler.java @@ -1,5 +1,6 @@ package com.las.strategy.handle; +import com.alibaba.fastjson.JSONObject; import com.las.common.Constant; import com.las.model.GroupExt; import com.las.utils.CmdUtil; @@ -9,13 +10,14 @@ public class MemberJoinMsgHandler extends BotMsgHandler { @Override public void exec() { - if (Constant.MESSAGE_TYPE_GROUP == getMsgType()) { - GroupExt groupExt = getGroupExtDao().findByGid(getId()); - String tip = groupExt.getAttribute1(); - // 若欢迎提示不为空,则欢迎新人入群 - if (StrUtils.isNotBlank(tip)) { - CmdUtil.sendAtMessage(tip, getUserId(), getUserId(), getId(), getMsgType()); - } + JSONObject member = getMsgObject().getJSONObject("member"); + Long gId = member.getJSONObject("group").getLong("id"); + Long uId = member.getLong("id"); + GroupExt groupExt = getGroupExtDao().findByGid(gId); + String tip = groupExt.getAttribute1(); + // 若欢迎提示不为空,则欢迎新人入群 + if (StrUtils.isNotBlank(tip)) { + CmdUtil.sendAtMessage(tip, uId, uId, gId, Constant.MESSAGE_TYPE_GROUP); } }