From 48c6fb4954aa27a1ca2a2c154758a8d8ccb2f886 Mon Sep 17 00:00:00 2001 From: mcoo <2435932516@qq.com> Date: Sun, 4 Jul 2021 19:48:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add=20=E6=A8=A1=E5=9D=97=E5=8C=96=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 135 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 79 insertions(+), 56 deletions(-) diff --git a/main.go b/main.go index c2ff897..e5ba18e 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ type BotManager struct { wg sync.WaitGroup myRecord map[string]MyRecord myRecordLocker sync.RWMutex - onEvent map[string][]reflect.Value + onEvent map[string][][]reflect.Value middleware []middleware delayed int locker sync.RWMutex @@ -155,7 +155,7 @@ func NewBotManager(QQ int64, OPQUrl string) BotManager { panic(err) } go s.GC() - b := BotManager{restart: make(chan int, 1), Session: s, Done: make(chan int, 10), MaxRetryCount: 10, wg: sync.WaitGroup{}, QQ: QQ, OPQUrl: OPQUrl, SendChan: make(chan SendMsgPack, 1024), onEvent: make(map[string][]reflect.Value), myRecord: map[string]MyRecord{}, myRecordLocker: sync.RWMutex{}, locker: sync.RWMutex{}, delayed: 1000} + b := BotManager{restart: make(chan int, 1), Session: s, Done: make(chan int, 10), MaxRetryCount: 10, wg: sync.WaitGroup{}, QQ: QQ, OPQUrl: OPQUrl, SendChan: make(chan SendMsgPack, 1024), onEvent: make(map[string][][]reflect.Value), myRecord: map[string]MyRecord{}, myRecordLocker: sync.RWMutex{}, locker: sync.RWMutex{}, delayed: 1000} go func() { for { select { @@ -216,7 +216,7 @@ func (b *BotManager) Start() error { // log.Println("连接成功!") f, ok := b.onEvent[EventNameOnConnected] if ok && len(f) >= 1 { - f[0].Call([]reflect.Value{}) + f[0][0].Call([]reflect.Value{}) } }) if err != nil { @@ -227,7 +227,7 @@ func (b *BotManager) Start() error { // log.Println("连接断开!") f, ok := b.onEvent[EventNameOnDisconnected] if ok && len(f) >= 1 { - f[0].Call([]reflect.Value{}) + f[0][0].Call([]reflect.Value{}) } b.restart <- 1 }) @@ -267,12 +267,14 @@ func (b *BotManager) Start() error { b.myRecordLocker.Unlock() }() } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) } //log.Println(args) }) @@ -293,11 +295,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } //log.Println(args) }) @@ -331,11 +335,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupAdmin: b.locker.RLock() @@ -348,11 +354,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupExit: b.locker.RLock() @@ -365,11 +373,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupExitSuccess: b.locker.RLock() @@ -382,11 +392,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupAdminSysNotify: b.locker.RLock() @@ -399,11 +411,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupRevoke: b.locker.RLock() @@ -416,11 +430,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupShut: b.locker.RLock() @@ -433,11 +449,13 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } case EventNameOnGroupSystemNotify: b.locker.RLock() @@ -450,18 +468,23 @@ func (b *BotManager) Start() error { log.Println("解析包错误") return } - result.Bot = b - result.f = f - result.NowIndex = 0 - result.MaxIndex = len(f) - 1 - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + for _, v := range f { + result.Bot = b + result.f = v + result.NowIndex = 0 + result.MaxIndex = len(v) - 1 + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)}) + } } default: b.locker.RLock() defer b.locker.RUnlock() f, ok := b.onEvent[EventNameOnOther] if ok && len(f) >= 1 { - f[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(args)}) + for _, v := range f { + v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(args)}) + } + } } }) @@ -940,7 +963,7 @@ func (b *BotManager) AddEvent(EventName string, f ...interface{}) error { } b.locker.Lock() defer b.locker.Unlock() - b.onEvent[EventName] = events + b.onEvent[EventName] = append(b.onEvent[EventName], events) return nil } From 70899e024fc4944f50c01c3dd50e450d536e6c44 Mon Sep 17 00:00:00 2001 From: mcoo <2435932516@qq.com> Date: Wed, 7 Jul 2021 19:35:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?add=20=E6=A8=A1=E5=9D=97=E5=8C=96=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 30 ++++++++++++++++++++++++++++++ model.go | 4 ++++ 2 files changed, 34 insertions(+) diff --git a/main.go b/main.go index e5ba18e..18444ba 100644 --- a/main.go +++ b/main.go @@ -268,6 +268,9 @@ func (b *BotManager) Start() error { }() } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -296,6 +299,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -336,6 +342,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -355,6 +364,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -374,6 +386,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -393,6 +408,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -412,6 +430,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -431,6 +452,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -450,6 +474,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 @@ -469,6 +496,9 @@ func (b *BotManager) Start() error { return } for _, v := range f { + if result.Ban { + return + } result.Bot = b result.f = v result.NowIndex = 0 diff --git a/model.go b/model.go index de98b55..5a8dd44 100644 --- a/model.go +++ b/model.go @@ -164,6 +164,7 @@ type MyRecord struct { Content string `json:"Content"` } type Context struct { + Ban bool NowIndex int MaxIndex int f []reflect.Value @@ -171,6 +172,9 @@ type Context struct { } func (ctx *Context) Next(currentQQ int64, result interface{}) { + if ctx.Ban { + return + } if ctx.NowIndex >= ctx.MaxIndex { return } From d74c996a48a27c3074b831a79477cc5df8a817ec Mon Sep 17 00:00:00 2001 From: mcoo <2435932516@qq.com> Date: Wed, 7 Jul 2021 19:54:01 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=20=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 18444ba..0fb3a93 100644 --- a/main.go +++ b/main.go @@ -555,7 +555,7 @@ func (b *BotManager) ReCallMsg(GroupID, MsgRandom int64, MsgSeq int) error { return err } if result.Ret != 0 { - return errors.New("Error ") + return errors.New("Error 撤回失败") } else { return nil } @@ -574,7 +574,7 @@ func (b *BotManager) RefreshKey() error { return err } if result.Ret != 0 { - return errors.New("Error ") + return errors.New("Error 刷新Key失败") } else { return nil }