Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wfjsw committed Sep 20, 2020
2 parents 46642a9 + 65811d4 commit 20c1124
Show file tree
Hide file tree
Showing 21 changed files with 1,421 additions and 393 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
ldflags: -w -s -X "github.com/Mrs4s/go-cqhttp/coolq.version=${{ env.RELEASE_VERSION }}"
ldflags: -w -s -X "github.com/Mrs4s/go-cqhttp/coolq.Version=${{ env.RELEASE_VERSION }}"

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
.idea
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.2-alpine AS builder
FROM golang:1.14.7-alpine AS builder

RUN go env -w GO111MODULE=auto \
&& go env -w CGO_ENABLED=0 \
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@
| ------------------------------------------------------------ |
| [私聊信息](https://cqhttp.cc/docs/4.15/#/Post?id=私聊消息) |
| [群消息](https://cqhttp.cc/docs/4.15/#/Post?id=群消息) |
| [群消息撤回(拓展Event)](docs/cqhttp.md#群消息撤回) |
| [好友消息撤回(拓展Event)](docs/cqhttp.md#好友消息撤回) |
| [群消息撤回(拓展Event)](docs/cqhttp.md#群消息撤回) |
| [好友消息撤回(拓展Event)](docs/cqhttp.md#好友消息撤回) |
| [群内提示事件(拓展Event)(龙王等事件)](docs/cqhttp.md#群内戳一戳) |
| [群管理员变动](https://cqhttp.cc/docs/4.15/#/Post?id=群管理员变动) |
| [群成员减少](https://cqhttp.cc/docs/4.15/#/Post?id=群成员减少) |
| [群成员增加](https://cqhttp.cc/docs/4.15/#/Post?id=群成员增加) |
| [群禁言](https://cqhttp.cc/docs/4.15/#/Post?id=群禁言) |
| [群文件上传](https://cqhttp.cc/docs/4.15/#/Post?id=群文件上传)|
| [群文件上传](https://cqhttp.cc/docs/4.15/#/Post?id=群文件上传) |
| [加好友请求](https://cqhttp.cc/docs/4.15/#/Post?id=加好友请求) |
| [加群请求/邀请](https://cqhttp.cc/docs/4.15/#/Post?id=加群请求/邀请) |

Expand Down
121 changes: 105 additions & 16 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/wfjsw/go-cqhttp/global"
)

var version = "unknown"
var Version = "unknown"

// https://cqhttp.cc/docs/4.15/#/API?id=get_login_info-%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E5%8F%B7%E4%BF%A1%E6%81%AF
func (bot *CQBot) CQGetLoginInfo() MSG {
Expand All @@ -25,7 +25,7 @@ func (bot *CQBot) CQGetLoginInfo() MSG {

// https://cqhttp.cc/docs/4.15/#/API?id=get_friend_list-%E8%8E%B7%E5%8F%96%E5%A5%BD%E5%8F%8B%E5%88%97%E8%A1%A8
func (bot *CQBot) CQGetFriendList() MSG {
var fs []MSG
fs := make([]MSG, 0)
for _, f := range bot.Client.FriendList {
fs = append(fs, MSG{
"nickname": f.Nickname,
Expand All @@ -38,7 +38,7 @@ func (bot *CQBot) CQGetFriendList() MSG {

// https://cqhttp.cc/docs/4.15/#/API?id=get_group_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E5%88%97%E8%A1%A8
func (bot *CQBot) CQGetGroupList(noCache bool) MSG {
var gs []MSG
gs := make([]MSG, 0)
if noCache {
_ = bot.Client.ReloadGroupList()
}
Expand Down Expand Up @@ -81,27 +81,19 @@ func (bot *CQBot) CQGetGroupMemberList(groupId int64, noCache bool) MSG {
}
group.Members = t
}
var members []MSG
members := make([]MSG, 0)
for _, m := range group.Members {
members = append(members, convertGroupMemberInfo(groupId, m))
}
return OK(members)
}

// https://cqhttp.cc/docs/4.15/#/API?id=get_group_member_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF
func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64, noCache bool) MSG {
func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64) MSG {
group := bot.Client.FindGroup(groupId)
if group == nil {
return Failed(100)
}
if noCache {
t, err := bot.Client.GetGroupMembers(group)
if err != nil {
log.Warnf("刷新群 %v 成员列表失败: %v", groupId, err)
return Failed(100)
}
group.Members = t
}
member := group.FindMember(userId)
if member == nil {
return Failed(102)
Expand Down Expand Up @@ -206,11 +198,24 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
name := e.Get("data.name").Str
content := bot.ConvertObjectMessage(e.Get("data.content"), true)
if uin != 0 && name != "" && len(content) > 0 {
var newElem []message.IMessageElement
for _, elem := range content {
if img, ok := elem.(*message.ImageElement); ok {
gm, err := bot.Client.UploadGroupImage(groupId, img.Data)
if err != nil {
log.Warnf("警告:群 %v 图片上传失败: %v", groupId, err)
continue
}
newElem = append(newElem, gm)
continue
}
newElem = append(newElem, elem)
}
nodes = append(nodes, &message.ForwardNode{
SenderId: uin,
SenderName: name,
Time: int32(ts.Unix()),
Message: content,
Message: newElem,
})
return
}
Expand Down Expand Up @@ -299,6 +304,14 @@ func (bot *CQBot) CQSetGroupName(groupId int64, name string) MSG {
return Failed(100)
}

func (bot *CQBot) CQSetGroupMemo(groupId int64, msg string) MSG {
if g := bot.Client.FindGroup(groupId); g != nil {
g.UpdateMemo(msg)
return OK(nil)
}
return Failed(100)
}

// https://cqhttp.cc/docs/4.15/#/API?id=set_group_kick-%E7%BE%A4%E7%BB%84%E8%B8%A2%E4%BA%BA
func (bot *CQBot) CQSetGroupKick(groupId, userId int64, msg string, block bool) MSG {
if g := bot.Client.FindGroup(groupId); g != nil {
Expand Down Expand Up @@ -391,6 +404,44 @@ func (bot *CQBot) CQDeleteMessage(messageId int32) MSG {
return OK(nil)
}

// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_admin-%E7%BE%A4%E7%BB%84%E8%AE%BE%E7%BD%AE%E7%AE%A1%E7%90%86%E5%91%98
func (bot *CQBot) CQSetGroupAdmin(groupId, userId int64, enable bool) MSG {
group := bot.Client.FindGroup(groupId)
if group == nil || group.OwnerUin != bot.Client.Uin {
return Failed(100)
}
mem := group.FindMember(userId)
if mem == nil {
return Failed(100)
}
mem.SetAdmin(enable)
t, err := bot.Client.GetGroupMembers(group)
if err != nil {
log.Warnf("刷新群 %v 成员列表失败: %v", groupId, err)
return Failed(100)
}
group.Members = t
return OK(nil)
}

func (bot *CQBot) CQGetVipInfo(userId int64) MSG {
msg := MSG{}
vip, err := bot.Client.GetVipInfo(userId)
if err != nil {
return Failed(100)
}
msg = MSG{
"user_id": vip.Uin,
"nickname": vip.Name,
"level": vip.Level,
"level_speed": vip.LevelSpeed,
"vip_level": vip.VipLevel,
"vip_growth_speed": vip.VipGrowthSpeed,
"vip_growth_total": vip.VipGrowthTotal,
}
return OK(msg)
}

// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_honor_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E8%8D%A3%E8%AA%89%E4%BF%A1%E6%81%AF
func (bot *CQBot) CQGetGroupHonorInfo(groupId int64, t string) MSG {
msg := MSG{"group_id": groupId}
Expand Down Expand Up @@ -446,6 +497,27 @@ func (bot *CQBot) CQGetGroupHonorInfo(groupId int64, t string) MSG {
return OK(msg)
}

// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_stranger_info-%E8%8E%B7%E5%8F%96%E9%99%8C%E7%94%9F%E4%BA%BA%E4%BF%A1%E6%81%AF
func (bot *CQBot) CQGetStrangerInfo(userId int64) MSG {
info, err := bot.Client.GetSummaryInfo(userId)
if err != nil {
return Failed(100)
}
return OK(MSG{
"user_id": info.Uin,
"nickname": info.Nickname,
"sex": func() string {
if info.Sex == 1 {
return "female"
}
return "male"
}(),
"age": info.Age,
"level": info.Level,
"login_days": info.LoginDays,
})
}

// https://cqhttp.cc/docs/4.15/#/API?id=-handle_quick_operation-%E5%AF%B9%E4%BA%8B%E4%BB%B6%E6%89%A7%E8%A1%8C%E5%BF%AB%E9%80%9F%E6%93%8D%E4%BD%9C
// https://github.com/richardchien/coolq-http-api/blob/master/src/cqhttp/plugins/web/http.cpp#L376
func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
Expand Down Expand Up @@ -533,7 +605,7 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
if m == nil {
return Failed(100)
}
var r []MSG
r := make([]MSG, 0)
for _, n := range m.Nodes {
bot.checkMedia(n.Message)
r = append(r, MSG{
Expand Down Expand Up @@ -576,6 +648,11 @@ func (bot *CQBot) CQCanSendRecord() MSG {
return OK(MSG{"yes": true})
}

func (bot *CQBot) CQReloadEventFilter() MSG {
global.BootFilter()
return OK(nil)
}

func (bot *CQBot) CQGetStatus() MSG {
return OK(MSG{
"app_initialized": true,
Expand All @@ -598,7 +675,19 @@ func (bot *CQBot) CQGetVersionInfo() MSG {
"plugin_build_configuration": "release",
"runtime_version": runtime.Version(),
"runtime_os": runtime.GOOS,
"version": version,
"version": Version,
"protocol": func() int {
switch client.SystemDeviceInfo.Protocol {
case client.AndroidPad:
return 0
case client.AndroidPhone:
return 1
case client.AndroidWatch:
return 2
default:
return -1
}
}(),
})
}

Expand Down
48 changes: 39 additions & 9 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/wfjsw/MiraiGo/binary"
"github.com/wfjsw/MiraiGo/client"
"github.com/wfjsw/MiraiGo/message"
Expand All @@ -27,10 +28,13 @@ type CQBot struct {
invitedReqCache sync.Map
joinReqCache sync.Map
tempMsgCache sync.Map
oneWayMsgCache sync.Map
}

type MSG map[string]interface{}

var ForceFragmented = false

func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
bot := &CQBot{
Client: cli,
Expand All @@ -54,6 +58,7 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
bot.Client.OnTempMessage(bot.tempMessageEvent)
bot.Client.OnGroupMuted(bot.groupMutedEvent)
bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent)
bot.Client.OnGroupNotify(bot.groupNotifyEvent)
bot.Client.OnFriendMessageRecalled(bot.friendRecallEvent)
bot.Client.OnJoinGroup(bot.joinGroupEvent)
bot.Client.OnLeaveGroup(bot.leaveGroupEvent)
Expand All @@ -65,15 +70,23 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
bot.Client.OnGroupInvited(bot.groupInvitedEvent)
bot.Client.OnUserWantJoinGroup(bot.groupJoinReqEvent)
go func() {
i := conf.HeartbeatInterval
if i < 0 {
log.Warn("警告: 心跳功能已关闭,若非预期,请检查配置文件。")
return
}
if i == 0 {
i = 5
}
for {
time.Sleep(time.Second * 5)
time.Sleep(time.Second * i)
bot.dispatchEventMessage(MSG{
"time": time.Now().Unix(),
"self_id": bot.Client.Uin,
"post_type": "meta_event",
"meta_event_type": "heartbeat",
"status": nil,
"interval": 5000,
"interval": 1000 * i,
})
}
}()
Expand Down Expand Up @@ -125,10 +138,18 @@ func (bot *CQBot) SendGroupMessage(groupId int64, m *message.SendingMessage) int
newElem = append(newElem, gv)
continue
}
if i, ok := elem.(*PokeElement); ok {
if group := bot.Client.FindGroup(groupId); group != nil {
if mem := group.FindMember(i.Target); mem != nil {
mem.Poke()
return 0
}
}
}
newElem = append(newElem, elem)
}
m.Elements = newElem
ret := bot.Client.SendGroupMessage(groupId, m)
ret := bot.Client.SendGroupMessage(groupId, m, ForceFragmented)
if ret == nil || ret.Id == -1 {
log.Warnf("群消息发送失败: 账号可能被风控.")
return -1
Expand Down Expand Up @@ -157,12 +178,15 @@ func (bot *CQBot) SendPrivateMessage(target int64, m *message.SendingMessage) in
if msg != nil {
id = msg.Id
}
} else {
if code, ok := bot.tempMsgCache.Load(target); ok {
msg := bot.Client.SendTempMessage(code.(int64), target, m)
if msg != nil {
id = msg.Id
}
} else if code, ok := bot.tempMsgCache.Load(target); ok {
msg := bot.Client.SendTempMessage(code.(int64), target, m)
if msg != nil {
id = msg.Id
}
} else if _, ok := bot.oneWayMsgCache.Load(target); ok {
msg := bot.Client.SendPrivateMessage(target, m)
if msg != nil {
id = msg.Id
}
}
if id == -1 {
Expand Down Expand Up @@ -221,6 +245,12 @@ func (bot *CQBot) Release() {
}

func (bot *CQBot) dispatchEventMessage(m MSG) {
payload := gjson.Parse(m.ToJson())
filter := global.EventFilter
if filter != nil && (*filter).Eval(payload) == false {
log.Debug("Event filtered!")
return
}
for _, f := range bot.events {
fn := f
go func() {
Expand Down

0 comments on commit 20c1124

Please sign in to comment.