Skip to content

Commit

Permalink
add mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora233 committed Aug 18, 2021
1 parent e9679cb commit e07e951
Show file tree
Hide file tree
Showing 10 changed files with 784 additions and 69 deletions.
12 changes: 12 additions & 0 deletions lsp/buntdb/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

type KeyPatternFunc func(...interface{}) string

func Key(keys ...interface{}) string {
var _keys []string
for _, ikey := range keys {
Expand Down Expand Up @@ -161,6 +163,16 @@ func ImageCacheKey(keys ...interface{}) string {
return NamedKey("ImageCache", keys)
}

func ModeKey() string {
return NamedKey("Mode", nil)
}
func NewFriendRequestKey(keys ...interface{}) string {
return NamedKey("NewFriendRequest", keys)
}
func GroupInvitedKey(keys ...interface{}) string {
return NamedKey("GroupInvited", keys)
}

func ParseConcernStateKeyWithInt64(key string) (groupCode int64, id int64, err error) {
keys := strings.Split(key, ":")
if len(keys) != 3 {
Expand Down
4 changes: 4 additions & 0 deletions lsp/buntdb/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ func TestParseConcernStateKeyWithString2(t *testing.T) {
assert.NotNil(t, err)
}
}

func TestModeKey(t *testing.T) {
assert.Equal(t, "Mode", ModeKey())
}
35 changes: 35 additions & 0 deletions lsp/buntdb/shortcut.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buntdb

import (
"encoding/json"
"github.com/modern-go/gls"
"github.com/tidwall/buntdb"
"strings"
Expand Down Expand Up @@ -70,6 +71,32 @@ func (*ShortCut) RCover(f func() error) error {
})
}

func (s *ShortCut) JsonSave(key string, obj interface{}, opt ...*buntdb.SetOptions) error {
return s.RWCoverTx(func(tx *buntdb.Tx) error {
b, err := json.Marshal(obj)
if err != nil {
return err
}
if len(opt) == 0 {
_, _, err = tx.Set(key, string(b), nil)
} else {
_, _, err = tx.Set(key, string(b), opt[0])
}
return err
})
}

func (s *ShortCut) JsonGet(key string, obj interface{}) error {
return s.RWCoverTx(func(tx *buntdb.Tx) error {
val, err := tx.Get(key)
if err != nil {
return err
}
err = json.Unmarshal([]byte(val), obj)
return err
})
}

func RWCoverTx(f func(tx *buntdb.Tx) error) error {
return shortCut.RWCoverTx(f)
}
Expand All @@ -82,6 +109,14 @@ func RCover(f func() error) error {
return shortCut.RCover(f)
}

func JsonGet(key string, obj interface{}) error {
return shortCut.JsonGet(key, obj)
}

func JsonSave(key string, obj interface{}, opt ...*buntdb.SetOptions) error {
return shortCut.JsonSave(key, obj, opt...)
}

func ExpireOption(duration time.Duration) *buntdb.SetOptions {
if duration == 0 {
return nil
Expand Down
10 changes: 8 additions & 2 deletions lsp/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
SysinfoCommand = "sysinfo"
WhosyourdaddyCommand = "whosyourdaddy"
QuitCommand = "quit"
ModeCommand = "mode"
GroupRequestCommand = "群请求"
FriendRequestCommand = "好友请求"
)

var allGroupCommand = [...]string{
Expand All @@ -47,12 +50,15 @@ var allPrivateOperate = [...]string{
BlockCommand, SysinfoCommand, ListCommand,
WatchCommand, UnwatchCommand, DisableCommand,
EnableCommand, GrantCommand, ConfigCommand,
WhosyourdaddyCommand, QuitCommand,
WhosyourdaddyCommand, QuitCommand, ModeCommand,
GroupRequestCommand, FriendRequestCommand,
}

var nonOprateable = [...]string{
EnableCommand, DisableCommand, GrantCommand,
BlockCommand, LogCommand, PingCommand, WhosyourdaddyCommand, QuitCommand,
BlockCommand, LogCommand, PingCommand,
WhosyourdaddyCommand, QuitCommand, ModeCommand,
GroupRequestCommand, FriendRequestCommand,
}

func CheckValidCommand(command string) bool {
Expand Down
16 changes: 12 additions & 4 deletions lsp/groupCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,18 @@ func (lgc *LspGroupCommand) HelpCommand() {
return
}

text := "一个多功能单推专用推送机器人,支持b站、斗鱼、油管、虎牙推送\n" +
"只需添加bot好友,阁下也可为自己的群添加自动推送功能\n" +
"详细介绍请添加好友后私聊发送/help"
lgc.textReply(text)
var sb strings.Builder
sb.WriteString("DDBOT是一个多功能单推专用推送机器人,支持b站、斗鱼、油管、虎牙推送\n")

switch lgc.l.LspStateManager.GetCurrentMode() {
case PublicMode:
sb.WriteString("当前BOT处于公开状态,添加BOT好友后即可让BOT为阁下的群服务\n详细介绍请添加好友后私聊发送/help")
case PrivateMode:
sb.WriteString("当前BOT处于私有状态,BOT会拒绝好友与加群邀请")
case ProtectMode:
sb.WriteString("当前BOT处于审核状态,添加BOT好友并由管理员审核后即可让BOT为阁下的群服务\n详细介绍请添加好友后私聊发送/help")
}
lgc.textReply(sb.String())
}

func (lgc *LspGroupCommand) ImageContent() {
Expand Down
110 changes: 67 additions & 43 deletions lsp/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,68 +190,79 @@ func (l *Lsp) PostInit() {

func (l *Lsp) Serve(bot *bot.Bot) {
bot.OnGroupInvited(func(qqClient *client.QQClient, request *client.GroupInvitedRequest) {
log := logger.WithField("group_code", request.GroupCode).
WithField("group_name", request.GroupName).
WithField("invitor_uin", request.InvitorUin).
WithField("invitor_nick", request.InvitorNick)
log := logger.WithField("GroupCode", request.GroupCode).
WithField("GroupName", request.GroupName).
WithField("InvitorUin", request.InvitorUin).
WithField("InvitorNick", request.InvitorNick)

if l.PermissionStateManager.CheckBlockList(request.InvitorUin) {
log.Debug("blocked invited")
log.Debug("收到加群邀请,该用户在block列表中,将拒绝加群邀请")
request.Reject(false, "")
return
}

log.Info("new group invited")
fi := bot.FindFriend(request.InvitorUin)
if fi == nil {
request.Reject(false, "未找到阁下的好友信息,请添加好友进行操作")
log.Errorf("can not find friend info")
log.Errorf("收到加群邀请,无法找到好友信息,将拒绝加群邀请")
return
}
sendingMsg := message.NewSendingMessage()
sendingMsg.Append(message.NewText(fmt.Sprintf("阁下的群邀请已通过,基于对阁下的信任,阁下已获得本bot在群【%s】的控制权限,相信阁下不会滥用本bot。", request.GroupName)))
bot.SendPrivateMessage(request.InvitorUin, sendingMsg)
if err := l.PermissionStateManager.GrantGroupRole(request.GroupCode, request.InvitorUin, permission.GroupAdmin); err != nil {
log.WithField("target", request.InvitorUin).
WithField("group_code", request.GroupCode).
Errorf("grant group admin failed %v", err)

switch l.LspStateManager.GetCurrentMode() {
case PrivateMode:
log.Info("收到加群邀请,当前BOT处于私有模式,将拒绝加群邀请")
request.Reject(false, "当前BOT处于私有模式")
case ProtectMode:
log.Info("收到加群邀请,当前BOT处于审核模式,将保留加群邀请")
case PublicMode:
request.Accept()
log.Info("收到加群邀请,当前BOT处于公开模式,将接受加群邀请")
sendingMsg := message.NewSendingMessage()
sendingMsg.Append(message.NewText(fmt.Sprintf("阁下的群邀请已通过,基于对阁下的信任,阁下已获得本bot在群【%s】的控制权限,相信阁下不会滥用本bot。", request.GroupName)))
bot.SendPrivateMessage(request.InvitorUin, sendingMsg)
if err := l.PermissionStateManager.GrantGroupRole(request.GroupCode, request.InvitorUin, permission.GroupAdmin); err != nil {
log.Errorf("设置群管理员权限失败 - %v", err)
}
default:
// impossible
log.Errorf("收到加群邀请,当前BOT处于未知模式,将拒绝加群邀请,请将该问题反馈给开发者")
request.Reject(false, "内部错误")
}
request.Accept()

//err := l.LspStateManager.SaveGroupInvitor(request.GroupCode, request.InvitorUin)
//if err == localdb.ErrKeyExist {
// request.Reject(false, "已有其他群友邀请加群,请通知管理员审核")
// log.Errorf("invited duplicate")
// return
//} else if err != nil {
// request.Reject(false, "未知问题,加群失败")
// log.Errorf("invited process failed %v", err)
// return
//} else {
// request.Accept()
// sendingMsg := message.NewSendingMessage()
// sendingMsg.Append(message.NewText(fmt.Sprintf("已接受阁下的群邀请。在成功入群后,基于对阁下的信任,阁下将获得bot在群【%s】的控制权限。", request.GroupName)))
// bot.SendPrivateMessage(request.InvitorUin, sendingMsg)
//}
})

bot.OnNewFriendRequest(func(qqClient *client.QQClient, request *client.NewFriendRequest) {
log := logger.WithField("uin", request.RequesterUin).
WithField("nickname", request.RequesterNick).
WithField("message", request.Message)
log.Info("收到好友申请")
log := logger.WithField("RequesterUin", request.RequesterUin).
WithField("RequesterNick", request.RequesterNick).
WithField("Message", request.Message)
if l.PermissionStateManager.CheckBlockList(request.RequesterUin) {
log.Info("该用户在block列表中,将拒绝好友申请")
log.Info("收到好友申请,该用户在block列表中,将拒绝好友申请")
request.Reject()
return
}
log.Info("friend request")
request.Accept()
switch l.LspStateManager.GetCurrentMode() {
case PrivateMode:
log.Info("收到好友申请,当前BOT处于私有模式,将拒绝好友申请")
request.Reject()
case ProtectMode:
if err := l.LspStateManager.SaveNewFriendRequest(request); err != nil {
log.Errorf("收到好友申请,但记录申请失败,将拒绝该申请,请将该问题反馈给开发者 - error %v", err)
request.Reject()
} else {
log.Info("收到好友申请,当前BOT处于审核模式,将保留好友申请")
}
case PublicMode:
log.Info("收到好友申请,当前BOT处于公开模式,将通过好友申请")
request.Accept()
default:
// impossible
log.Errorf("收到好友申请,当前BOT处于未知模式,将拒绝好友申请,请将该问题反馈给开发者")
request.Reject()
}
})

bot.OnNewFriendAdded(func(qqClient *client.QQClient, event *client.NewFriendEvent) {
logger.WithField("uin", event.Friend.Uin).
WithField("nickname", event.Friend.Nickname).
logger.WithField("Uin", event.Friend.Uin).
WithField("Nickname", event.Friend.Nickname).
Info("添加新好友")
sendingMsg := message.NewSendingMessage()
sendingMsg.Append(message.NewText("阁下的好友请求已通过,请使用/help查看帮助,然后在群成员页面邀请bot加群(bot不会主动加群)。"))
Expand All @@ -276,17 +287,17 @@ func (l *Lsp) Serve(bot *bot.Bot) {
douyuIds, _, _ := l.douyuConcern.ListByGroup(event.Group.Code, nil)
huyaIds, _, _ := l.huyaConcern.ListByGroup(event.Group.Code, nil)
ytbIds, _, _ := l.youtubeConcern.ListByGroup(event.Group.Code, nil)
logger := logger.WithField("GroupCode", event.Group.Code).
log := logger.WithField("GroupCode", event.Group.Code).
WithField("GroupName", event.Group.Name).
WithField("MemberCount", event.Group.MemberCount).
WithField("bilibili订阅数", len(bilibiliIds)).
WithField("douyu订阅数", len(douyuIds)).
WithField("huya订阅数", len(huyaIds)).
WithField("ytb订阅数", len(ytbIds))
if event.Operator == nil {
logger.Info("退出群聊")
log.Info("退出群聊")
} else {
logger.Infof("被%v踢出群聊", event.Operator.DisplayName())
log.Infof("被 %v 踢出群聊", event.Operator.DisplayName())
}
l.RemoveAllByGroup(event.Group.Code)
})
Expand Down Expand Up @@ -597,6 +608,19 @@ func (l *Lsp) getConcernConfigNotifyManager(ctype concern.Type, concernConfig *c
}
}

//
//func (l *Lsp) IsPublicMode() bool {
// return l.LspStateManager.IsPublicMode()
//}
//
//func (l *Lsp) IsProtectMode() bool {
// return l.LspStateManager.IsProtectMode()
//}
//
//func (l *Lsp) IsPrivateMode() bool {
// return l.LspStateManager.IsPrivateMode()
//}

var Instance *Lsp

func init() {
Expand Down

0 comments on commit e07e951

Please sign in to comment.