Skip to content

Commit

Permalink
add: pull black
Browse files Browse the repository at this point in the history
  • Loading branch information
RicheyJang committed Mar 16, 2022
1 parent 7235000 commit 5caf702
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 5 deletions.
3 changes: 2 additions & 1 deletion basic/ban/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/RicheyJang/PaimengBot/manager"
"github.com/RicheyJang/PaimengBot/utils"

zero "github.com/wdvxdr1123/ZeroBot"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func init() {
const AllPluginKey = "all"

func checkPluginStatus(condition *manager.PluginCondition, ctx *zero.Ctx) error {
if ctx.Event == nil {
if !utils.IsMessage(ctx) { //仅处理消息类型事件
return nil
}
// 群ban
Expand Down
1 change: 1 addition & 0 deletions basic/dao/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type UserSetting struct {
Nickname string // 昵称
Likeability float64 // 好感度(无用,抱歉...)
Flag string // 非空时代表该用户尚未成为好友,是他的好友请求flag
IsPullBlack bool // 是否被拉黑:拒绝好友请求、拒绝加群请求
}

type GroupSetting struct {
Expand Down
27 changes: 25 additions & 2 deletions basic/event/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,29 @@ func handleGroupAdmin(ctx *zero.Ctx) {
}
}

// 收到邀请入群、加好友请求时
// 收到加群、邀请入群、加好友请求时
func handleInvite(ctx *zero.Ctx) {
switch ctx.Event.RequestType {
case "friend":
handleFriendRequest(ctx)
case "group":
if ctx.Event.SubType == "invite" {
handleGroupInvite(ctx)
} else if ctx.Event.SubType == "add" {
handleGroupAdd(ctx)
}
}
}

func handleFriendRequest(ctx *zero.Ctx) {
// 处理拉黑用户的加好友请求:自动拒绝
var pbUser dao.UserSetting
if rows := proxy.GetDB().Take(&pbUser, ctx.Event.UserID).RowsAffected; rows > 0 && pbUser.IsPullBlack {
ctx.SetFriendAddRequest(ctx.Event.Flag, false, "")
log.Infof("已自动拒绝被拉黑用户%d的加好友请求", ctx.Event.UserID)
return
}
// 正常用户
userS := dao.UserSetting{
ID: ctx.Event.UserID,
Flag: ctx.Event.Flag,
Expand Down Expand Up @@ -149,8 +159,21 @@ func handleGroupInvite(ctx *zero.Ctx) {
log.Errorf("set group(id=%v) flag error(sql): %v", ctx.Event.GroupID, err)
utils.SendToSuper(message.Text("处理群邀请请求时SQL出错,请尽快查看日志处理"))
} else {
str := fmt.Sprintf("收到一条群邀请:\n群ID: %v\n邀请者ID:%v", ctx.Event.GroupID, ctx.Event.UserID)
str := fmt.Sprintf("收到一条群邀请:\n群ID: %v", ctx.Event.GroupID)
if ctx.Event.UserID != 0 {
str += fmt.Sprintf("\n邀请者ID:%v", ctx.Event.UserID)
}
str += fmt.Sprintf("\n若同意请说:同意群邀请 %[1]v\n若拒绝请说:拒绝群邀请 %[1]v", ctx.Event.GroupID)
utils.SendToSuper(message.Text(str))
}
}

func handleGroupAdd(ctx *zero.Ctx) {
// 处理拉黑用户的加群请求:自动拒绝
var user dao.UserSetting
if rows := proxy.GetDB().Take(&user, ctx.Event.UserID).RowsAffected; rows > 0 && user.IsPullBlack {
ctx.SetGroupAddRequest(ctx.Event.Flag, "add", false, "你已被拉黑")
log.Infof("已自动拒绝被拉黑用户%d的加群(%d)请求", ctx.Event.UserID, ctx.Event.GroupID)
return
}
}
136 changes: 136 additions & 0 deletions plugins/admin/black.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package admin

import (
"fmt"
"strconv"
"strings"

"github.com/RicheyJang/PaimengBot/basic/ban"
"github.com/RicheyJang/PaimengBot/basic/dao"
"github.com/RicheyJang/PaimengBot/utils"
"github.com/RicheyJang/PaimengBot/utils/images"

log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"gorm.io/gorm/clause"
)

func blackSomeone(ctx *zero.Ctx) {
if !utils.IsMessageGroup(ctx) && !utils.IsSuperUser(ctx.Event.UserID) {
ctx.Send("请在群聊中调用此功能")
return
}
id, _, err := analysisArgs(ctx, false)
if err != nil {
log.Errorf("解析参数错误:%v", err)
return
}
if id <= 0 {
ctx.Send("请指定拉黑的QQ号或@")
return
}
// 禁止拉黑机器人自身和超级用户
if id == ctx.Event.SelfID || utils.IsSuperUser(id) {
ctx.Send("?")
return
}
// 获取确认
ctx.Send(fmt.Sprintf("确认拉黑用户%v?这将踢出并自动拒绝该用户加入任何%[2]v作为管理员的群聊,删除并禁止其加%[2]v为好友,封禁该用户的所有功能使用权", id, utils.GetBotNickname()))
event := utils.WaitNextMessage(ctx)
if event == nil { // 无回应
return
}
confirm := strings.TrimSpace(event.Message.ExtractPlainText())
if !(confirm == "是" || confirm == "确定" || confirm == "确认") {
ctx.Send("已取消")
return
}
// 修改IsPullBlack
preUser := dao.UserSetting{
ID: id,
IsPullBlack: true,
}
if err = proxy.GetDB().Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"is_pull_black"}), // Upsert
}).Create(&preUser).Error; err != nil {
log.Errorf("set user(%v) is_pull_black error(sql): %v", id, err)
ctx.Send("失败了...")
return
}
// 踢出所有群
groups := ctx.GetGroupList().Array()
for _, group := range groups {
groupID := group.Get("group_id").Int()
if groupID == 0 {
continue
}
black := ctx.GetGroupMemberInfo(groupID, id, false)
if black.Get("role").String() != "member" { // 拉黑成员不在该群或无法踢出
continue
}
self := ctx.GetGroupMemberInfo(groupID, ctx.Event.SelfID, false)
if self.Get("role").String() == "member" { // 机器人并非管理员
log.Warnf("该用户在群%d中,但%v没有管理员权限,在群%[1]d中无法踢出该用户", groupID, utils.GetBotNickname())
continue
}
ctx.SetGroupKick(groupID, id, false)
}
// 删好友
ctx.CallAction("delete_friend", zero.Params{
"friend_id": id,
})
// 封禁
_ = ban.SetUserPluginStatus(false, id, nil, 0)
ctx.Send("走你")
}

func unBlackSomeone(ctx *zero.Ctx) {
if !utils.IsMessageGroup(ctx) && !utils.IsSuperUser(ctx.Event.UserID) {
ctx.Send("请在群聊中调用此功能")
return
}
id, _, err := analysisArgs(ctx, false)
if err != nil {
log.Errorf("解析参数错误:%v", err)
return
}
if id <= 0 {
ctx.Send("请指定取消拉黑的QQ号或@")
return
}
// 修改IsPullBlack
if err = proxy.GetDB().Model(&dao.UserSetting{ID: id}).Update("is_pull_black", false).Error; err != nil {
log.Errorf("set user(%v) is_pull_black error(sql): %v", id, err)
ctx.Send("失败了...")
return
}
// 解封
_ = ban.SetUserPluginStatus(true, id, nil, 0)
ctx.Send("好哒")
}

func blackList(ctx *zero.Ctx) {
var users []dao.UserSetting
proxy.GetDB().Where(&dao.UserSetting{IsPullBlack: true}).Find(&users)
if len(users) == 0 {
ctx.Send("暂时没有用户被拉黑")
return
}
// 生成消息
userMap := make(map[int64]string)
least := "被拉黑用户清单:"
for _, user := range users {
id := "QQ: " + strconv.FormatInt(user.ID, 10)
userMap[user.ID] = id
least += "\n" + id
}
w, _ := images.MeasureStringDefault(least, 24, 1.3)
msg, err := images.GenQQListMsgWithAva(userMap, w, true)
if err != nil {
log.Warnf("GenQQListMsgWithAva err: %v", err)
ctx.Send(least)
} else {
ctx.Send(msg)
}
}
36 changes: 34 additions & 2 deletions plugins/admin/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,39 @@ import (
var info = manager.PluginInfo{
Name: "群管理",
Classify: "群功能",
Usage: `用法:(需要设为管理员
Usage: `用法:(需要将{bot}设为群管理员
踢了 [QQ号或@]:并在后续询问中答复"是",则将指定QQ号或@的人踢出本群
禁言 [QQ号或@] [时长]:将指定QQ号或@的人指定时长(时长为0则解除禁言; QQ号为0则全体禁言)
拉黑 [QQ号或@]:并在后续询问中答复"是",则将指定QQ号或@的人拉黑
取消拉黑 [QQ号或@]:将指定QQ号或@的人取消拉黑
另外,回复某条消息"禁言 [时长]",则可以将原消息发送者禁言指定时长
拉黑指:踢出并自动拒绝该用户加入任何{bot}作为管理员的群聊,删除并禁止其加{bot}为好友,封禁该用户的所有功能使用权
此拉黑与"功能开关"插件中的封禁、黑名单等功能没有任何联系,请注意区别!
示例:
禁言 123456 30m:将123456禁言30分钟
禁言 @XXX 1d12h:将XXX禁言1天12小时
禁言 123456 0:将123456解除禁言`,
SuperUsage: `
当前拉黑:显示当前被拉黑的用户列表
PS: 超级用户可以在私聊中调用"拉黑"和"取消拉黑"功能
PPS: 踢了、禁言、拉黑不会对机器人本身及超级用户生效`,
AdminLevel: 5,
}
var proxy *manager.PluginProxy

func init() {
info.Usage = strings.ReplaceAll(info.Usage, "{bot}", utils.GetBotNickname())
proxy = manager.RegisterPlugin(info)
if proxy == nil {
return
}
proxy.OnCommands([]string{"踢了"}, zero.OnlyGroup).SetBlock(true).ThirdPriority().Handle(kickSomeone)
proxy.OnCommands([]string{"禁言"}, zero.OnlyGroup).SetBlock(true).ThirdPriority().Handle(muteSomeone)
proxy.OnMessage(zero.OnlyGroup, rules.ReplyAndCommands("禁言")).SetBlock(true).ThirdPriority().Handle(muteReply)
// 拉黑相关
proxy.OnCommands([]string{"拉黑"}).SetBlock(true).ThirdPriority().Handle(blackSomeone)
proxy.OnCommands([]string{"取消拉黑"}).SetBlock(true).ThirdPriority().Handle(unBlackSomeone)
proxy.OnFullMatch([]string{"当前拉黑"}, zero.SuperUserPermission).SetBlock(true).SetPriority(3).Handle(blackList)
}

func kickSomeone(ctx *zero.Ctx) {
Expand All @@ -51,6 +64,11 @@ func kickSomeone(ctx *zero.Ctx) {
ctx.Send("请指定踢出的QQ号或@")
return
}
// 禁止踢出机器人自身和超级用户
if id == ctx.Event.SelfID || utils.IsSuperUser(id) {
ctx.Send("?")
return
}
// 获取确认
ctx.Send(fmt.Sprintf("确认踢出用户%v?", id))
event := utils.WaitNextMessage(ctx)
Expand Down Expand Up @@ -80,6 +98,11 @@ func muteSomeone(ctx *zero.Ctx) {
if duration != 0 && duration <= time.Minute { // 至少禁言1分钟
duration = time.Minute
}
// 禁止禁言机器人自身和超级用户
if duration != 0 && (id == ctx.Event.SelfID || utils.IsSuperUser(id)) {
ctx.Send("?")
return
}
// 禁言
if id == 0 {
ctx.SetGroupWholeBan(ctx.Event.GroupID, duration != 0)
Expand Down Expand Up @@ -118,6 +141,11 @@ func muteReply(ctx *zero.Ctx) {
if duration != 0 && duration <= time.Minute { // 至少禁言1分钟
duration = time.Minute
}
// 禁止禁言机器人自身和超级用户
if duration != 0 && (msg.Sender.ID == ctx.Event.SelfID || utils.IsSuperUser(msg.Sender.ID)) {
ctx.Send("?")
return
}
// 禁言
// WARNING Go-Cqhttp并没有提供AnonymousFlag字段,因此无法支持禁言匿名用户
ctx.SetGroupBan(ctx.Event.GroupID, msg.Sender.ID, int64(duration/time.Second))
Expand All @@ -129,8 +157,12 @@ func analysisArgs(ctx *zero.Ctx, parseTime bool) (ID int64, seconds time.Duratio
// @
for _, msg := range ctx.Event.Message {
if msg.Type == "at" {
ID, err = strconv.ParseInt(msg.Data["qq"], 10, 64)
if err != nil || ID == ctx.Event.SelfID {
ID = 0
continue
}
parseID = false
ID, _ = strconv.ParseInt(msg.Data["qq"], 10, 64)
if !parseTime {
return
}
Expand Down

0 comments on commit 5caf702

Please sign in to comment.