Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit 2fb0817

Browse files
authored
Merge pull request #16 from opq-osc/beta
合并到主分支
2 parents e16f6e1 + f0e0457 commit 2fb0817

File tree

4 files changed

+208
-33
lines changed

4 files changed

+208
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ go.sum
1515

1616
# Dependency directories (remove the comment below to include it)
1717
# vendor/
18+
.vscode/*

example/main.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package main
33
import (
44
"encoding/base64"
55
"fmt"
6-
"github.com/mcoo/OPQBot"
7-
"github.com/mcoo/requests"
86
"io/ioutil"
97
"log"
108
"os"
119
"strconv"
1210
"strings"
1311
"time"
12+
13+
"github.com/mcoo/OPQBot"
14+
"github.com/mcoo/requests"
1415
)
1516

1617
var ZanNote = map[int64]int{}
@@ -52,7 +53,8 @@ func main() {
5253
//log.Println(infoReg.FindStringSubmatch(lists.Data.Data[0]["html"].(string))[1])
5354

5455
//log.Println(ck.PSkey.Qzone)
55-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupMessage, VerifyBlackList, func(botQQ int64, packet *OPQBot.GroupMsgPack) {
56+
var cancel func()
57+
cancel, err = opqBot.AddEvent(OPQBot.EventNameOnGroupMessage, VerifyBlackList, func(botQQ int64, packet *OPQBot.GroupMsgPack) {
5658
if packet.FromUserID != opqBot.QQ {
5759
s := opqBot.Session.SessionStart(packet.FromUserID)
5860
//last, _ := s.GetString("last")
@@ -68,6 +70,9 @@ func main() {
6870
//b,_ := ioutil.ReadFile("./1.mp3")base64.StdEncoding.EncodeToString(b)
6971
log.Println(opqBot.UploadFileWithBase64("1.mp3", "MTIzMTIzMTIzMjEz", packet.FromGroupID, true))
7072
}
73+
if packet.Content == "取消事件监听" {
74+
cancel()
75+
}
7176
if packet.Content == "#silk" {
7277
b, err := OPQBot.VoiceMp3ToSilk("./secret base ~君がくれたもの~ (10 years after Ver.).mp3")
7378
if err != nil {
@@ -105,6 +110,24 @@ func main() {
105110
_ = opqBot.ReCallMsg(record.FromGroupID, record.MsgRandom, record.MsgSeq)
106111
},
107112
})
113+
opqBot.Send(OPQBot.SendMsgPack{
114+
SendToType: OPQBot.SendToTypeGroup,
115+
ToUserUid: packet.FromGroupID,
116+
Content: OPQBot.SendTypeTextMsgContent{Content: OPQBot.MacroAt([]int64{packet.FromUserID}) + "20s撤回测试!\n" + OPQBot.MacroId()},
117+
CallbackFunc: func(Code int, Info string, record OPQBot.MyRecord) {
118+
time.Sleep(20 * time.Second)
119+
_ = opqBot.ReCallMsg(record.FromGroupID, record.MsgRandom, record.MsgSeq)
120+
},
121+
})
122+
opqBot.Send(OPQBot.SendMsgPack{
123+
SendToType: OPQBot.SendToTypeGroup,
124+
ToUserUid: packet.FromGroupID,
125+
Content: OPQBot.SendTypeTextMsgContent{Content: OPQBot.MacroAt([]int64{packet.FromUserID}) + "20s撤回测试!\n" + OPQBot.MacroId()},
126+
CallbackFunc: func(Code int, Info string, record OPQBot.MyRecord) {
127+
time.Sleep(20 * time.Second)
128+
_ = opqBot.ReCallMsg(record.FromGroupID, record.MsgRandom, record.MsgSeq)
129+
},
130+
})
108131
}
109132
if packet.Content == "#赞我" {
110133
i, ok := ZanNote[packet.FromUserID]
@@ -189,7 +212,7 @@ func main() {
189212
if err != nil {
190213
log.Println(err.Error())
191214
}
192-
err = opqBot.AddEvent(OPQBot.EventNameOnFriendMessage, func(botQQ int64, packet *OPQBot.FriendMsgPack) {
215+
_, err = opqBot.AddEvent(OPQBot.EventNameOnFriendMessage, func(botQQ int64, packet *OPQBot.FriendMsgPack) {
193216
if packet.Content == "赞我" {
194217
i, ok := ZanNote[packet.FromUin]
195218
if ok {
@@ -259,46 +282,46 @@ func main() {
259282
if err != nil {
260283
log.Println(err.Error())
261284
}
262-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupShut, func(botQQ int64, packet *OPQBot.GroupShutPack) {
285+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupShut, func(botQQ int64, packet *OPQBot.GroupShutPack) {
263286
log.Println(botQQ, packet)
264287
})
265288
if err != nil {
266289
log.Println(err.Error())
267290
}
268-
err = opqBot.AddEvent(OPQBot.EventNameOnConnected, func() {
291+
_, err = opqBot.AddEvent(OPQBot.EventNameOnConnected, func() {
269292
log.Println("连接成功!!!")
270293
})
271294
if err != nil {
272295
log.Println(err.Error())
273296
}
274-
err = opqBot.AddEvent(OPQBot.EventNameOnDisconnected, func() {
297+
_, err = opqBot.AddEvent(OPQBot.EventNameOnDisconnected, func() {
275298
log.Println("连接断开!!")
276299
})
277300
if err != nil {
278301
log.Println(err.Error())
279302
}
280-
err = opqBot.AddEvent(OPQBot.EventNameOnOther, func(botQQ int64, e interface{}) {
303+
_, err = opqBot.AddEvent(OPQBot.EventNameOnOther, func(botQQ int64, e interface{}) {
281304
log.Println(e)
282305
})
283-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupSystemNotify, func(botQQ int64, e *OPQBot.GroupSystemNotifyPack) {
306+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupSystemNotify, func(botQQ int64, e *OPQBot.GroupSystemNotifyPack) {
284307
log.Println(e)
285308
})
286-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupRevoke, func(botQQ int64, e *OPQBot.GroupRevokePack) {
309+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupRevoke, func(botQQ int64, e *OPQBot.GroupRevokePack) {
287310
log.Println(e)
288311
})
289-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupJoin, func(botQQ int64, e *OPQBot.GroupJoinPack) {
312+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupJoin, func(botQQ int64, e *OPQBot.GroupJoinPack) {
290313
log.Println(e)
291314
})
292-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupAdmin, func(botQQ int64, e *OPQBot.GroupAdminPack) {
315+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupAdmin, func(botQQ int64, e *OPQBot.GroupAdminPack) {
293316
log.Println(e)
294317
})
295-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupExit, func(botQQ int64, e *OPQBot.GroupExitPack) {
318+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupExit, func(botQQ int64, e *OPQBot.GroupExitPack) {
296319
log.Println(e)
297320
})
298-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupExitSuccess, func(botQQ int64, e *OPQBot.GroupExitSuccessPack) {
321+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupExitSuccess, func(botQQ int64, e *OPQBot.GroupExitSuccessPack) {
299322
log.Println(e)
300323
})
301-
err = opqBot.AddEvent(OPQBot.EventNameOnGroupAdminSysNotify, func(botQQ int64, e *OPQBot.GroupAdminSysNotifyPack) {
324+
_, err = opqBot.AddEvent(OPQBot.EventNameOnGroupAdminSysNotify, func(botQQ int64, e *OPQBot.GroupAdminSysNotifyPack) {
302325
log.Println(e)
303326
})
304327
if err != nil {

main.go

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
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

5759
var 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+
}
59122
func (b *BotManager) Wait() {
60123
home:
61124
b.wg.Wait()
@@ -162,14 +225,14 @@ func init() {
162225
func 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
}
10351117
func (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

Comments
 (0)