Skip to content

Commit e0875e2

Browse files
committed
fix: 全局白名单 群聊 黑名单 help显示不正确
fix: 切换白名单后所有插件被禁用
1 parent c8266f4 commit e0875e2

File tree

7 files changed

+138
-77
lines changed

7 files changed

+138
-77
lines changed

basic/ban/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func init() {
4848
proxy.OnCommands([]string{"封禁", "ban", "Ban"}, zero.OnlyToMe).SetBlock(true).FirstPriority().Handle(banUser)
4949
proxy.OnCommands([]string{"解封", "unban", "Unban"}, zero.OnlyToMe).SetBlock(true).FirstPriority().Handle(unbanUser)
5050
proxy.OnCommands([]string{"黑名单"}, zero.OnlyToMe).SetBlock(true).FirstPriority().Handle(showBlack)
51+
// 仅超级用户可以使用白名单查看某个白名单模式群的启用功能
52+
proxy.OnCommands([]string{"白名单"}, zero.OnlyToMe, zero.SuperUserPermission).SetBlock(true).FirstPriority().Handle(showWhite)
5153
proxy.AddConfig("tip", false)
5254
manager.AddPreHook(checkPluginStatus)
5355
}

basic/ban/public.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func GetGroupRunningMode(groupID int64) bool {
141141
return preGroup.WhiteMode
142142
}
143143

144+
// GetUserRunningMode 获取用户运行模式(白名单模式为true,黑名单为false)
144145
func GetUserRunningMode(userID int64) bool {
145146
var preUser dao.UserSetting
146147
proxy.GetDB().Take(&preUser, userID)

basic/ban/show.go

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,49 @@ import (
1515
zero "github.com/wdvxdr1123/ZeroBot"
1616
)
1717

18-
// userID -> 被封插件列表
19-
func getUsersBlack() map[int64][]string {
18+
// userID -> 被封插件列表(白名单为启用的列表)
19+
func getUsersBlackOrWhite(filterWhite bool) map[int64][]string {
2020
var users []dao.UserSetting
21-
proxy.GetDB().Select("id", "black_plugins").Where("LENGTH(black_plugins) > 1").Find(&users)
21+
22+
proxy.GetDB().Select("id", "black_plugins", "white_plugins", "white_mode").Where("LENGTH(black_plugins) > 1 or LENGTH(white_plugins) > 1").Find(&users)
2223
res := make(map[int64][]string)
2324
for _, user := range users {
24-
blacks := utils.MergeStringSlices(strings.Split(user.BlackPlugins, "|")) // 去重去空
25-
if len(blacks) == 0 {
25+
var plugins []string
26+
if user.WhiteMode != filterWhite {
2627
continue
2728
}
28-
res[user.ID] = blacks
29+
if filterWhite {
30+
plugins = utils.MergeStringSlices(strings.Split(user.WhitePlugins, "|")) // 去重去空
31+
} else {
32+
plugins = utils.MergeStringSlices(strings.Split(user.BlackPlugins, "|")) // 去重去空
33+
}
34+
if len(plugins) == 0 {
35+
continue
36+
}
37+
res[user.ID] = plugins
2938
}
3039
return res
3140
}
3241

33-
// groupID -> 被封插件列表
34-
func getGroupsBlack() map[int64][]string {
42+
// groupID -> 被封插件列表(白名单为启用的列表)
43+
func getGroupsBlackOrWhite(filterWhite bool) map[int64][]string {
3544
var groups []dao.GroupSetting
36-
proxy.GetDB().Select("id", "black_plugins").Where("LENGTH(black_plugins) > 1").Find(&groups)
45+
proxy.GetDB().Select("id", "black_plugins", "white_plugins", "white_mode").Where("LENGTH(black_plugins) > 1").Find(&groups)
3746
res := make(map[int64][]string)
3847
for _, group := range groups {
39-
blacks := utils.MergeStringSlices(strings.Split(group.BlackPlugins, "|")) // 去重去空
40-
if len(blacks) == 0 {
48+
var plugins []string
49+
if group.WhiteMode != filterWhite {
50+
continue
51+
}
52+
if filterWhite {
53+
plugins = utils.MergeStringSlices(strings.Split(group.WhitePlugins, "|")) // 去重去空
54+
} else {
55+
plugins = utils.MergeStringSlices(strings.Split(group.BlackPlugins, "|")) // 去重去空
56+
}
57+
if len(plugins) == 0 {
4158
continue
4259
}
43-
res[group.ID] = blacks
60+
res[group.ID] = plugins
4461
}
4562
return res
4663
}
@@ -57,7 +74,7 @@ func showBlack(ctx *zero.Ctx) {
5774
}
5875
// 群管理员 in 群聊
5976
var str string
60-
userM := getUsersBlack()
77+
userM := getUsersBlackOrWhite(false)
6178
rsp := ctx.GetGroupMemberList(ctx.Event.GroupID) // 过滤掉非本群成员
6279
users := rsp.Array()
6380
for _, user := range users {
@@ -88,9 +105,60 @@ func showBlack(ctx *zero.Ctx) {
88105
ctx.SendChain(msg)
89106
}
90107

108+
func showWhite(ctx *zero.Ctx) {
109+
userM := getUsersBlackOrWhite(true)
110+
groupM := getGroupsBlackOrWhite(true)
111+
if len(userM) == 0 && len(groupM) == 0 {
112+
ctx.Send("没有群/用户运行在白名单模式")
113+
return
114+
}
115+
// 用户
116+
if len(userM) > 0 {
117+
str := "用户:"
118+
userDesM := make(map[int64]string)
119+
for id, blacks := range userM {
120+
var des string
121+
if id == 0 { // 全体用户(全局)
122+
des = fmt.Sprintf("全体: %v\n", formBlackDescription(blacks))
123+
} else { // 正常用户
124+
user := ctx.GetStrangerInfo(id, false)
125+
des = fmt.Sprintf("%v(%v): %v\n", user.Get("nickname"), id, formBlackDescription(blacks))
126+
}
127+
userDesM[id] = des
128+
str += des + "\n"
129+
}
130+
w, _ := images.MeasureStringDefault(str, 24, 1.3)
131+
msg, err := images.GenQQListMsgWithAva(userDesM, w, true)
132+
if err != nil {
133+
ctx.Send(str)
134+
} else {
135+
ctx.SendChain(message.Text("用户:\n"), msg)
136+
}
137+
}
138+
// 群聊
139+
if len(groupM) > 0 {
140+
str := "群:"
141+
groupDesM := make(map[int64]string)
142+
for id, blacks := range groupM {
143+
group := ctx.GetGroupInfo(id, false)
144+
des := fmt.Sprintf("%v(%v): %v\n",
145+
group.Name, id, formBlackDescription(blacks))
146+
groupDesM[id] = des
147+
str += des + "\n"
148+
}
149+
w, _ := images.MeasureStringDefault(str, 24, 1.3)
150+
msg, err := images.GenQQListMsgWithAva(groupDesM, w, false)
151+
if err != nil {
152+
ctx.Send(str)
153+
} else {
154+
ctx.SendChain(message.Text("群:\n"), msg)
155+
}
156+
}
157+
}
158+
91159
func showBlackInPrimarySuper(ctx *zero.Ctx) {
92-
userM := getUsersBlack()
93-
groupM := getGroupsBlack()
160+
userM := getUsersBlackOrWhite(false)
161+
groupM := getGroupsBlackOrWhite(false)
94162
if len(userM) == 0 && len(groupM) == 0 {
95163
ctx.Send("大家都是好人,黑名单暂时是空哒")
96164
return

basic/ban/switch.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func setModeWhite(ctx *zero.Ctx) {
9898
return
9999
}
100100
preGroup.WhiteMode = true
101+
if preGroup.WhitePlugins == "" {
102+
preGroup.WhitePlugins = "auth|ban|event|help|invite|limiter|nickname|sc"
103+
}
101104
if err := proxy.GetDB().Clauses(clause.OnConflict{
102105
Columns: []clause.Column{{Name: "id"}},
103106
DoUpdates: clause.AssignmentColumns([]string{"white_mode"}), // Upsert
@@ -111,8 +114,15 @@ func setModeWhite(ctx *zero.Ctx) {
111114
//私聊如果是超级管理员设置全局
112115
if utils.IsSuperUser(ctx.Event.UserID) {
113116
var preUser dao.UserSetting
114-
if err := proxy.GetDB().Model(&preUser).Where("id = ?", 0).Update("white_mode", false).Error; err != nil {
115-
log.Errorf("setModeBlack err: %v", err)
117+
preUser.WhiteMode = true
118+
if preUser.WhitePlugins == "" {
119+
preUser.WhitePlugins = "auth|ban|event|help|invite|limiter|nickname|sc"
120+
}
121+
if err := proxy.GetDB().Clauses(clause.OnConflict{
122+
Columns: []clause.Column{{Name: "id"}},
123+
DoUpdates: clause.AssignmentColumns([]string{"white_mode", "white_plugins"}), // Upsert
124+
}).Create(&preUser).Error; err != nil {
125+
log.Errorf("setModeWhite err: %v", err)
116126
ctx.Send("失败了...")
117127
return
118128
}
@@ -135,9 +145,6 @@ func setModeBlack(ctx *zero.Ctx) {
135145
return
136146
}
137147
preGroup.WhiteMode = false
138-
if preGroup.WhitePlugins == "" {
139-
preGroup.WhitePlugins = "auth|ban|event|help|invite|limiter|nickname|sc"
140-
}
141148
if err := proxy.GetDB().Clauses(clause.OnConflict{
142149
Columns: []clause.Column{{Name: "id"}},
143150
DoUpdates: clause.AssignmentColumns([]string{"white_mode"}), // Upsert

basic/help/init.go

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package help
22

33
import (
4-
"github.com/RicheyJang/PaimengBot/basic/ban"
54
"math"
65
"strings"
76

87
"github.com/RicheyJang/PaimengBot/basic/auth"
9-
"github.com/RicheyJang/PaimengBot/basic/dao"
108
"github.com/RicheyJang/PaimengBot/manager"
119
"github.com/RicheyJang/PaimengBot/utils"
1210

@@ -42,24 +40,14 @@ func helpHandle(ctx *zero.Ctx) {
4240
isSuper := utils.IsSuperUser(ctx.Event.UserID)
4341
arg := strings.TrimSpace(utils.GetArgs(ctx))
4442
level := auth.GetGroupUserPriority(ctx.Event.GroupID, ctx.Event.UserID)
45-
whiteMode := ban.GetGroupRunningMode(ctx.Event.GroupID)
46-
if !utils.IsMessageGroup(ctx) {
47-
whiteMode = ban.GetUserRunningMode(ctx.Event.UserID)
48-
}
49-
var keys map[string]struct{}
50-
if whiteMode {
51-
keys = getWhiteKeys(ctx.Event.UserID, ctx.Event.GroupID)
52-
} else {
53-
keys = getBlackKeys(ctx.Event.UserID, ctx.Event.GroupID)
54-
}
5543
if utils.IsGroupAnonymous(ctx) { // 匿名用户单独处理
5644
isSuper = false
5745
level = math.MaxInt
5846
}
5947
if len(arg) == 0 {
60-
ctx.SendChain(formSummaryHelpMsg(isSuper, utils.IsMessagePrimary(ctx), level, whiteMode, keys))
48+
ctx.SendChain(formSummaryHelpMsg(isSuper, utils.IsMessagePrimary(ctx), level, ctx.Event.UserID, ctx.Event.GroupID))
6149
} else {
62-
ctx.SendChain(formSingleHelpMsg(arg, isSuper, utils.IsMessagePrimary(ctx), level, whiteMode, keys))
50+
ctx.SendChain(formSingleHelpMsg(arg, isSuper, utils.IsMessagePrimary(ctx), level, ctx.Event.UserID, ctx.Event.GroupID))
6351
}
6452
}
6553

@@ -80,32 +68,3 @@ func checkPluginCouldShow(plugin *manager.PluginCondition, isSuper, isPrimary bo
8068
}
8169
return true
8270
}
83-
84-
func getBlackKeys(userID, groupID int64) map[string]struct{} {
85-
var users []dao.UserSetting
86-
var groupS dao.GroupSetting
87-
proxy.GetDB().Find(&users, []int64{0, userID})
88-
if groupID != 0 {
89-
proxy.GetDB().Find(&groupS, groupID)
90-
}
91-
var usersKey string
92-
for _, user := range users {
93-
usersKey += user.BlackPlugins
94-
}
95-
return utils.FormSetByStrings(strings.Split(groupS.BlackPlugins, "|"),
96-
strings.Split(usersKey, "|"))
97-
}
98-
func getWhiteKeys(userID, groupID int64) map[string]struct{} {
99-
var users []dao.UserSetting
100-
var groupS dao.GroupSetting
101-
proxy.GetDB().Find(&users, []int64{0, userID})
102-
if groupID != 0 {
103-
proxy.GetDB().Find(&groupS, groupID)
104-
}
105-
var usersKey string
106-
for _, user := range users {
107-
usersKey += user.BlackPlugins
108-
}
109-
return utils.FormSetByStrings(strings.Split(groupS.WhitePlugins, "|"),
110-
strings.Split(usersKey, "|"))
111-
}

basic/help/single.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,38 @@ package help
33
import (
44
"fmt"
55

6+
"github.com/RicheyJang/PaimengBot/basic/ban"
67
"github.com/RicheyJang/PaimengBot/manager"
78
"github.com/RicheyJang/PaimengBot/utils/images"
89
log "github.com/sirupsen/logrus"
910
"github.com/wdvxdr1123/ZeroBot/message"
1011
)
1112

12-
func formSingleHelpMsg(cmd string, isSuper, isPrimary bool, priority int, white bool, blackKeys map[string]struct{}) message.MessageSegment {
13+
func formSingleHelpMsg(cmd string, isSuper, isPrimary bool, priority int, userID int64, groupID int64) message.MessageSegment {
1314
plugins := manager.GetAllPluginConditions()
1415
// 寻找插件
16+
var keys = make(map[string]struct{})
17+
for _, plugin := range plugins {
18+
if !ban.GetUserPluginStatus(0, plugin) {
19+
keys[plugin.Key] = struct{}{}
20+
}
21+
if !ban.GetUserPluginStatus(userID, plugin) {
22+
keys[plugin.Key] = struct{}{}
23+
}
24+
if groupID > 0 && !ban.GetGroupPluginStatus(groupID, plugin) {
25+
keys[plugin.Key] = struct{}{}
26+
}
27+
}
1528
var selected *manager.PluginCondition
1629
for _, plugin := range plugins { // 优先找插件名
17-
if plugin.Name == cmd && checkPluginCouldShow(plugin, isSuper, isPrimary, priority, blackKeys) {
30+
if plugin.Name == cmd && checkPluginCouldShow(plugin, isSuper, isPrimary, priority, keys) {
1831
selected = plugin
1932
break
2033
}
2134
}
2235
if selected == nil { // 尝试通过命令
2336
for _, plugin := range plugins {
24-
if isCmdContains(plugin, cmd, isSuper) && checkPluginCouldShow(plugin, isSuper, isPrimary, priority, blackKeys) {
37+
if isCmdContains(plugin, cmd, isSuper) && checkPluginCouldShow(plugin, isSuper, isPrimary, priority, keys) {
2538
selected = plugin
2639
break
2740
}
@@ -31,10 +44,7 @@ func formSingleHelpMsg(cmd string, isSuper, isPrimary bool, priority int, white
3144
return message.Text("没有找到这个功能哦,或在群聊中无法查看功能详情")
3245
}
3346
// 插件状态检查
34-
if _, ok := blackKeys[selected.Key]; !white && ok {
35-
return message.Text("功能被禁用中")
36-
}
37-
if _, ok := blackKeys[selected.Key]; white && !ok {
47+
if _, ok := keys[selected.Key]; ok {
3848
return message.Text("功能被禁用中")
3949
}
4050
// 生成图片 名称|普通用法|超级用户用法

basic/help/summary.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88
"strconv"
99

10+
"github.com/RicheyJang/PaimengBot/basic/ban"
1011
"github.com/RicheyJang/PaimengBot/manager"
1112
"github.com/RicheyJang/PaimengBot/utils"
1213
"github.com/RicheyJang/PaimengBot/utils/images"
@@ -18,24 +19,37 @@ import (
1819
const defaultClassify = "一般功能"
1920
const passiveClassify = "被动"
2021

21-
func formSummaryHelpMsg(isSuper, isPrimary bool, priority int, white bool, keys map[string]struct{}) message.MessageSegment {
22+
func formSummaryHelpMsg(isSuper, isPrimary bool, priority int, userID int64, groupID int64) message.MessageSegment {
2223
plugins := manager.GetAllPluginConditions()
2324
// 获取所有插件信息
2425
var helps helpSummaryMap = make(map[string]*blockInfo)
26+
var globalBan = make(map[string]struct{})
27+
var groupBan = make(map[string]struct{})
28+
for _, plugin := range plugins {
29+
if !ban.GetUserPluginStatus(0, plugin) {
30+
globalBan[plugin.Key] = struct{}{}
31+
}
32+
if !ban.GetUserPluginStatus(userID, plugin) {
33+
groupBan[plugin.Key] = struct{}{}
34+
}
35+
if groupID > 0 && !ban.GetGroupPluginStatus(groupID, plugin) {
36+
groupBan[plugin.Key] = struct{}{}
37+
}
38+
}
2539
for _, plugin := range plugins {
2640
// 过滤
27-
if !checkPluginCouldShow(plugin, isSuper, isPrimary, priority, keys) {
41+
if _, ok := globalBan[plugin.Key]; !isSuper && ok {
42+
continue
43+
}
44+
if !checkPluginCouldShow(plugin, isSuper, isPrimary, priority, globalBan) {
2845
continue
2946
}
3047
// 生成项目(一个插件)
3148
var item blockItem
3249
item.name = plugin.Name
3350
item.color = "black"
34-
if _, ok := keys[plugin.Key]; white && !ok {
35-
item.disabled = true // 插件对该用户或群被禁用
36-
}
37-
if _, ok := keys[plugin.Key]; !white && ok {
38-
item.disabled = true // 插件对该用户或群被禁用
51+
if _, ok := groupBan[plugin.Key]; ok {
52+
item.disabled = true
3953
}
4054
if plugin.IsPassive && len(plugin.Classify) != 0 && plugin.Classify != passiveClassify {
4155
item.name += "(被动)" // 被动且已有其它分类

0 commit comments

Comments
 (0)