Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
G
go-cqhttp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
go-cqhttp
Commits
7df17f7f
Commit
7df17f7f
authored
Oct 17, 2020
by
Mrs4s
Browse files
Options
Browse Files
Download
Plain Diff
update MiraiGo & private ptt support.
parents
9bd41c77
18f8a12b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
42 deletions
+45
-42
coolq/bot.go
coolq/bot.go
+27
-24
coolq/cqcode.go
coolq/cqcode.go
+18
-18
No files found.
coolq/bot.go
View file @
7df17f7f
...
...
@@ -5,6 +5,7 @@ import (
"encoding/gob"
"encoding/json"
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"hash/crc32"
"path"
"sync"
...
...
@@ -17,14 +18,13 @@ import (
log
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/xujiajun/nutsdb"
)
type
CQBot
struct
{
Client
*
client
.
QQClient
events
[]
func
(
MSG
)
db
*
nuts
db
.
DB
db
*
level
db
.
DB
friendReqCache
sync
.
Map
invitedReqCache
sync
.
Map
joinReqCache
sync
.
Map
...
...
@@ -41,14 +41,22 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
Client
:
cli
,
}
if
conf
.
EnableDB
{
opt
:=
nutsdb
.
DefaultOptions
opt
.
Dir
=
path
.
Join
(
"data"
,
"db"
)
opt
.
EntryIdxMode
=
nutsdb
.
HintBPTSparseIdxMode
db
,
err
:=
nutsdb
.
Open
(
opt
)
p
:=
path
.
Join
(
"data"
,
"leveldb"
)
db
,
err
:=
leveldb
.
OpenFile
(
p
,
nil
)
if
err
!=
nil
{
log
.
Fatalf
(
"打开数据库失败, 如果频繁遇到此问题请清理 data/db 文件夹或关闭数据库功能。"
)
}
bot
.
db
=
db
/*
opt := nutsdb.DefaultOptions
opt.Dir = path.Join("data", "db")
opt.EntryIdxMode = nutsdb.HintBPTSparseIdxMode
db, err := nutsdb.Open(opt)
if err != nil {
log.Fatalf("打开数据库失败, 如果频繁遇到此问题请清理 data/db 文件夹或关闭数据库功能。")
}
bot.db = db
*/
gob
.
Register
(
message
.
Sender
{})
log
.
Info
(
"信息数据库初始化完成."
)
}
else
{
...
...
@@ -103,17 +111,14 @@ func (bot *CQBot) OnEventPush(f func(m MSG)) {
func
(
bot
*
CQBot
)
GetGroupMessage
(
mid
int32
)
MSG
{
if
bot
.
db
!=
nil
{
m
:=
MSG
{}
err
:=
bot
.
db
.
View
(
func
(
tx
*
nutsdb
.
Tx
)
error
{
e
,
err
:=
tx
.
Get
(
"group-messages"
,
binary
.
ToBytes
(
mid
))
if
err
!=
nil
{
return
err
}
buff
:=
new
(
bytes
.
Buffer
)
buff
.
Write
(
binary
.
GZipUncompress
(
e
.
Value
))
return
gob
.
NewDecoder
(
buff
)
.
Decode
(
&
m
)
})
data
,
err
:=
bot
.
db
.
Get
(
binary
.
ToBytes
(
mid
),
nil
)
if
err
==
nil
{
return
m
buff
:=
new
(
bytes
.
Buffer
)
buff
.
Write
(
binary
.
GZipUncompress
(
data
))
err
=
gob
.
NewDecoder
(
buff
)
.
Decode
(
&
m
)
if
err
==
nil
{
return
m
}
}
log
.
Warnf
(
"获取信息时出现错误: %v id: %v"
,
err
,
mid
)
}
...
...
@@ -299,14 +304,12 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
}
id
:=
ToGlobalId
(
m
.
GroupCode
,
m
.
Id
)
if
bot
.
db
!=
nil
{
err
:=
bot
.
db
.
Update
(
func
(
tx
*
nutsdb
.
Tx
)
error
{
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
gob
.
NewEncoder
(
buf
)
.
Encode
(
val
);
err
!=
nil
{
return
err
}
return
tx
.
Put
(
"group-messages"
,
binary
.
ToBytes
(
id
),
binary
.
GZipCompress
(
buf
.
Bytes
()),
0
)
})
if
err
!=
nil
{
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
gob
.
NewEncoder
(
buf
)
.
Encode
(
val
);
err
!=
nil
{
log
.
Warnf
(
"记录聊天数据时出现错误: %v"
,
err
)
return
-
1
}
if
err
:=
bot
.
db
.
Put
(
binary
.
ToBytes
(
id
),
binary
.
GZipCompress
(
buf
.
Bytes
()),
nil
);
err
!=
nil
{
log
.
Warnf
(
"记录聊天数据时出现错误: %v"
,
err
)
return
-
1
}
...
...
coolq/cqcode.go
View file @
7df17f7f
...
...
@@ -567,27 +567,27 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m messag
case
"cardimage"
:
source
:=
d
[
"source"
]
icon
:=
d
[
"icon"
]
min
w
idth
,
_
:=
strconv
.
ParseInt
(
d
[
"minwidth"
],
10
,
64
)
if
min
w
idth
==
0
{
min
w
idth
=
200
min
W
idth
,
_
:=
strconv
.
ParseInt
(
d
[
"minwidth"
],
10
,
64
)
if
min
W
idth
==
0
{
min
W
idth
=
200
}
min
h
eight
,
_
:=
strconv
.
ParseInt
(
d
[
"minheight"
],
10
,
64
)
if
min
h
eight
==
0
{
min
h
eight
=
200
min
H
eight
,
_
:=
strconv
.
ParseInt
(
d
[
"minheight"
],
10
,
64
)
if
min
H
eight
==
0
{
min
H
eight
=
200
}
max
w
idth
,
_
:=
strconv
.
ParseInt
(
d
[
"maxwidth"
],
10
,
64
)
if
max
w
idth
==
0
{
max
w
idth
=
500
max
W
idth
,
_
:=
strconv
.
ParseInt
(
d
[
"maxwidth"
],
10
,
64
)
if
max
W
idth
==
0
{
max
W
idth
=
500
}
max
h
eight
,
_
:=
strconv
.
ParseInt
(
d
[
"maxheight"
],
10
,
64
)
if
max
h
eight
==
0
{
max
h
eight
=
1000
max
H
eight
,
_
:=
strconv
.
ParseInt
(
d
[
"maxheight"
],
10
,
64
)
if
max
H
eight
==
0
{
max
H
eight
=
1000
}
img
,
err
:=
bot
.
makeImageElem
(
d
,
group
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"send cardimage faild"
)
}
return
bot
.
SendNewPic
(
img
,
source
,
icon
,
min
width
,
minheight
,
maxwidth
,
maxh
eight
,
group
)
return
bot
.
SendNewPic
(
img
,
source
,
icon
,
min
Width
,
minHeight
,
maxWidth
,
maxH
eight
,
group
)
default
:
return
nil
,
errors
.
New
(
"unsupported cq code: "
+
t
)
}
...
...
@@ -737,7 +737,7 @@ func (bot *CQBot) makeImageElem(d map[string]string, group bool) (message.IMessa
}
//SendNewPic 一种xml 方式发送的群消息图片
func
(
bot
*
CQBot
)
SendNewPic
(
elem
message
.
IMessageElement
,
source
string
,
icon
string
,
min
width
int64
,
minheigt
int64
,
maxwidth
int64
,
maxh
eight
int64
,
group
bool
)
(
*
message
.
ServiceElement
,
error
)
{
func
(
bot
*
CQBot
)
SendNewPic
(
elem
message
.
IMessageElement
,
source
string
,
icon
string
,
min
Width
int64
,
minHeight
int64
,
maxWidth
int64
,
maxH
eight
int64
,
group
bool
)
(
*
message
.
ServiceElement
,
error
)
{
var
xml
string
xml
=
""
if
i
,
ok
:=
elem
.
(
*
message
.
ImageElement
);
ok
{
...
...
@@ -747,7 +747,7 @@ func (bot *CQBot) SendNewPic(elem message.IMessageElement, source string, icon s
log
.
Warnf
(
"警告: 好友消息 %v 消息图片上传失败: %v"
,
1
,
err
)
return
nil
,
err
}
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
gm
.
Md5
,
gm
.
Md5
,
len
(
i
.
Data
),
""
,
min
width
,
minheigt
,
maxwidth
,
maxh
eight
,
source
,
icon
)
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
gm
.
Md5
,
gm
.
Md5
,
len
(
i
.
Data
),
""
,
min
Width
,
minHeight
,
maxWidth
,
maxH
eight
,
source
,
icon
)
}
else
{
gm
,
err
:=
bot
.
Client
.
UploadGroupImage
(
1
,
i
.
Data
)
...
...
@@ -755,14 +755,14 @@ func (bot *CQBot) SendNewPic(elem message.IMessageElement, source string, icon s
log
.
Warnf
(
"警告: 群 %v 消息图片上传失败: %v"
,
1
,
err
)
return
nil
,
err
}
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
gm
.
Md5
,
gm
.
Md5
,
len
(
i
.
Data
),
""
,
min
width
,
minheigt
,
maxwidth
,
maxh
eight
,
source
,
icon
)
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
gm
.
Md5
,
gm
.
Md5
,
len
(
i
.
Data
),
""
,
min
Width
,
minHeight
,
maxWidth
,
maxH
eight
,
source
,
icon
)
}
}
if
i
,
ok
:=
elem
.
(
*
message
.
GroupImageElement
);
ok
{
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
i
.
Md5
,
i
.
Md5
,
0
,
""
,
min
width
,
minheigt
,
maxwidth
,
maxh
eight
,
source
,
icon
)
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
i
.
Md5
,
i
.
Md5
,
0
,
""
,
min
Width
,
minHeight
,
maxWidth
,
maxH
eight
,
source
,
icon
)
}
if
i
,
ok
:=
elem
.
(
*
message
.
FriendImageElement
);
ok
{
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
i
.
Md5
,
i
.
Md5
,
0
,
""
,
min
width
,
minheigt
,
maxwidth
,
maxh
eight
,
source
,
icon
)
xml
=
fmt
.
Sprintf
(
`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="[分享]我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`
,
""
,
i
.
Md5
,
i
.
Md5
,
0
,
""
,
min
Width
,
minHeight
,
maxWidth
,
maxH
eight
,
source
,
icon
)
}
if
xml
!=
""
{
log
.
Warn
(
xml
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment