44 "bytes"
55 "crypto/rand"
66 "encoding/base64"
7+ "encoding/json"
78 "errors"
8- "github.com/sirupsen/logrus"
99 "io/ioutil"
1010 "math/big"
1111 "os"
@@ -18,6 +18,8 @@ import (
1818 "sync"
1919 "time"
2020
21+ "github.com/sirupsen/logrus"
22+
2123 nested "github.com/antonfisher/nested-logrus-formatter"
2224 "github.com/goinggo/mapstructure"
2325 gosocketio "github.com/mcoo/OPQBot/golang-socketio-edit"
@@ -56,6 +58,67 @@ func (b *BotManager) SetMaxRetryCount(maxRetryCount int) {
5658
5759var interrupt chan os.Signal
5860
61+ func ParserGroupAtMsg (pack GroupMsgPack ) (a AtMsg , e error ) {
62+ if pack .MsgType != "AtMsg" {
63+ e = errors .New ("Not AtMsg. " )
64+ return
65+ }
66+ e = json .Unmarshal ([]byte (pack .Content ), & a )
67+ if e != nil {
68+ return
69+ }
70+ return
71+ }
72+ func (a AtMsg ) Clean () AtMsg {
73+ for _ , v := range a .UserExt {
74+ a .Content = strings .TrimSpace (strings .ReplaceAll (a .Content , "@" + v .QQNick , "" ))
75+ }
76+ return a
77+ }
78+ func ParserGroupReplyMsg (pack GroupMsgPack ) (a Reply , e error ) {
79+ if pack .MsgType != "AtMsg" {
80+ e = errors .New ("Not ReplyMsg. " )
81+ return
82+ }
83+ e = json .Unmarshal ([]byte (pack .Content ), & a )
84+ if e != nil {
85+ return
86+ }
87+ return
88+ }
89+ func ParserGroupPicMsg (pack GroupMsgPack ) (a PicMsg , e error ) {
90+ if pack .MsgType != "PicMsg" {
91+ e = errors .New ("Not PicMsg. " )
92+ return
93+ }
94+ e = json .Unmarshal ([]byte (pack .Content ), & a )
95+ if e != nil {
96+ return
97+ }
98+ return
99+ }
100+ func ParserGroupFileMsg (pack GroupMsgPack ) (a GroupFileMsg , e error ) {
101+ if pack .MsgType != "GroupFileMsg" {
102+ e = errors .New ("Not GroupFileMsg. " )
103+ return
104+ }
105+ e = json .Unmarshal ([]byte (pack .Content ), & a )
106+ if e != nil {
107+ return
108+ }
109+ return
110+ }
111+ func ParserVideoMsg (pack GroupMsgPack ) (a VideoMsg , e error ) {
112+ if pack .MsgType != "VideoMsg" {
113+ e = errors .New ("Not VideoMsg. " )
114+ return
115+ }
116+ e = json .Unmarshal ([]byte (pack .Content ), & a )
117+ if e != nil {
118+ return
119+ }
120+ return
121+ }
59122func (b * BotManager ) Wait () {
60123home:
61124 b .wg .Wait ()
@@ -162,14 +225,14 @@ func init() {
162225func SetLog (l * logrus.Entry ) {
163226 log = l
164227}
165- func NewBotManager (QQ int64 , OPQUrl string ) BotManager {
228+ func NewBotManager (QQ int64 , OPQUrl string ) * BotManager {
166229
167230 s , err := session .NewManager ("qq" , 3600 )
168231 if err != nil {
169232 panic (err )
170233 }
171234 go s .GC ()
172- 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 }
235+ 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 }
173236 go func () {
174237 for {
175238 select {
@@ -952,12 +1015,15 @@ func MacroAtAll() string {
9521015 return "[ATALL()]"
9531016}
9541017
955- func (b * BotManager ) AddEvent (EventName string , f ... interface {}) error {
1018+ func (b * BotManager ) AddEvent (EventName string , f ... interface {}) ( func (), error ) {
9561019 var events []reflect.Value
1020+ if len (f ) == 0 {
1021+ return nil , errors .New ("调用错误" )
1022+ }
9571023 for _ , v := range f {
9581024 fVal := reflect .ValueOf (v )
9591025 if fVal .Kind () != reflect .Func {
960- return errors .New ("NotFuncError" )
1026+ return nil , errors .New ("NotFuncError" )
9611027 }
9621028 var okStruck string
9631029 switch EventName {
@@ -988,23 +1054,35 @@ func (b *BotManager) AddEvent(EventName string, f ...interface{}) error {
9881054 case EventNameOnOther :
9891055 okStruck = "interface {}"
9901056 default :
991- return errors .New ("Unknown EventName " )
1057+ return nil , errors .New ("Unknown EventName " )
9921058 }
9931059
9941060 if fVal .Type ().NumIn () == 0 && okStruck == "ok" {
9951061 events = append (events , fVal )
9961062 continue
9971063 }
9981064 if fVal .Type ().NumIn () != 2 || fVal .Type ().In (1 ).String () != okStruck {
999- return errors .New (EventName + ": FuncError, Your Function Should Have " + okStruck + " Your Struct is " + fVal .Type ().In (1 ).String ())
1065+ return nil , errors .New (EventName + ": FuncError, Your Function Should Have " + okStruck + " Your Struct is " + fVal .Type ().In (1 ).String ())
10001066 }
10011067
10021068 events = append (events , fVal )
10031069 }
10041070 b .locker .Lock ()
10051071 defer b .locker .Unlock ()
10061072 b .onEvent [EventName ] = append (b .onEvent [EventName ], events )
1007- return nil
1073+ return func () {
1074+ b .locker .Lock ()
1075+ defer b .locker .Unlock ()
1076+ for i , v := range b .onEvent [EventName ] {
1077+ if len (v ) > 0 && v [0 ] == reflect .ValueOf (f [0 ]) {
1078+ if len (b .onEvent [EventName ]) == 1 {
1079+ delete (b .onEvent , EventName )
1080+ break
1081+ }
1082+ b .onEvent [EventName ] = append (b .onEvent [EventName ][:i ], b .onEvent [EventName ][i + 1 :]... )
1083+ }
1084+ }
1085+ }, nil
10081086
10091087}
10101088
@@ -1029,8 +1107,12 @@ func (b *BotManager) RegSendMiddleware(priority int, f func(m map[string]interfa
10291107 b .middleware = append (b .middleware , middle )
10301108 return nil
10311109}
1032- func (b * BotManager ) CallFunc (FuncName string , funcstruct interface {}) {
1033-
1110+ func (b * BotManager ) CallFunc (FuncName string , funcStruct string ) ([]byte , error ) {
1111+ res , err := requests .PostJson (b .OPQUrl + "/v1/LuaApiCaller?funcname=" + FuncName + "&qq=" + strconv .FormatInt (b .QQ , 10 ), funcStruct )
1112+ if err != nil {
1113+ return nil , err
1114+ }
1115+ return res .Content (), nil
10341116}
10351117func (b * BotManager ) receiveSendPack () {
10361118 log .Info ("QQ发送信息通道开启" )
@@ -1160,20 +1242,26 @@ OuterLoop:
11601242 sendJsonPack ["SendToType" ] = sendMsgPack .SendToType
11611243 sendJsonPack ["ForwordBuf" ] = content .ForwordBuf
11621244 sendJsonPack ["ForwordField" ] = content .ForwordField
1245+ sendJsonPack ["Content" ] = content .Content
11631246 record .MsgType = "ForwordMsg"
11641247 case SendTypeForwordContentPrivateChat :
11651248 sendJsonPack ["SendMsgType" ] = "ForwordMsg"
11661249 sendJsonPack ["SendToType" ] = sendMsgPack .SendToType
11671250 sendJsonPack ["ForwordBuf" ] = content .ForwordBuf
11681251 sendJsonPack ["ForwordField" ] = content .ForwordField
11691252 sendJsonPack ["GroupID" ] = content .Group
1253+ sendJsonPack ["Content" ] = content .Content
11701254 record .MsgType = "ForwordMsg"
11711255
1172- case SendTypeRelayContent :
1256+ case SendTypeReplyContent :
1257+ sendJsonPack ["SendMsgType" ] = "ReplayMsg"
11731258 sendJsonPack ["ReplayInfo" ] = content .ReplayInfo
1259+ sendJsonPack ["SendToType" ] = sendMsgPack .SendToType
1260+ sendJsonPack ["Content" ] = content .Content
11741261 record .MsgType = "ReplayMsg"
1175- case SendTypeRelayContentPrivateChat :
1262+ case SendTypeReplyContentPrivateChat :
11761263 sendJsonPack ["SendMsgType" ] = "ReplayMsg"
1264+ sendJsonPack ["Content" ] = content .Content
11771265 sendJsonPack ["SendToType" ] = sendMsgPack .SendToType
11781266 sendJsonPack ["ReplayInfo" ] = content .ReplayInfo
11791267 sendJsonPack ["GroupID" ] = content .Group
@@ -1266,7 +1354,7 @@ OuterLoop:
12661354 sendMsgPack .CallbackFunc (result .Ret , result .Msg , myRecordPack )
12671355 stop <- true
12681356
1269- case <- time .After (2 * time .Second ):
1357+ case <- time .After (10 * time .Second ):
12701358 sendMsgPack .CallbackFunc (result .Ret , result .Msg , MyRecord {})
12711359 stop <- true
12721360 }
0 commit comments