Skip to content

Commit

Permalink
Merge pull request #2 from Mrs4s/master
Browse files Browse the repository at this point in the history
up to author
  • Loading branch information
Akegarasu committed Aug 26, 2020
2 parents 464122a + 174ebfa commit 8b14409
Show file tree
Hide file tree
Showing 19 changed files with 1,142 additions and 255 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set RELEASE_VERSION env
run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF:10}
- uses: wangyoucao577/go-release-action@master
env:
CGO_ENABLED: 0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
ldflags: "-w -s"
ldflags: -w -s -X "github.com/Mrs4s/go-cqhttp/coolq.version=${{ env.RELEASE_VERSION }}"

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
.idea
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

- [CQ:image]
- [CQ:record]
- [CQ:video]
- [CQ:face]
- [CQ:at]
- [CQ:share]
Expand Down Expand Up @@ -91,6 +92,16 @@

</details>

# 关于ISSUE

以下ISSUE会被直接关闭
- 提交BUG不使用Template
- 询问已知问题
- 提问找不到重点
- 重复提问

> 请注意, 开发者并没有义务回复您的问题. 您应该具备基本的提问技巧。
# 性能

在关闭数据库的情况下, 加载25个好友128个群运行24小时后内存使用为10MB左右. 开启数据库后内存使用将根据消息量增加10-20MB, 如果系统内存小于128M建议关闭数据库使用.
153 changes: 121 additions & 32 deletions coolq/api.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package coolq

import (
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"io/ioutil"
"os"
"path"
"runtime"
"strconv"
"time"

"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)

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 {
return OK(MSG{"user_id": bot.Client.Uin, "nickname": bot.Client.Nickname})
Expand All @@ -34,8 +37,11 @@ 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() MSG {
func (bot *CQBot) CQGetGroupList(noCache bool) MSG {
var gs []MSG
if noCache {
_ = bot.Client.ReloadGroupList()
}
for _, g := range bot.Client.GroupList {
gs = append(gs, MSG{
"group_id": g.Code,
Expand Down Expand Up @@ -96,11 +102,25 @@ func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64, noCache bool) MSG
}

// https://cqhttp.cc/docs/4.15/#/API?id=send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF
func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}, autoEscape bool) MSG {
var str string
fixAt := func(elem []message.IMessageElement) {
for _, e := range elem {
if at, ok := e.(*message.AtElement); ok && at.Target != 0 {
at.Display = "@" + func() string {
mem := bot.Client.FindGroup(groupId).FindMember(at.Target)
if mem != nil {
return mem.DisplayName()
}
return strconv.FormatInt(at.Target, 10)
}()
}
}
}
if m, ok := i.(gjson.Result); ok {
if m.Type == gjson.JSON {
elem := bot.ConvertObjectMessage(m, true)
fixAt(elem)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
Expand All @@ -113,20 +133,20 @@ func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
}
return m.Raw
}()
}
if s, ok := i.(string); ok {
} else if s, ok := i.(string); ok {
str = s
}
if str == "" {
log.Warnf("群消息发送失败: 信息为空. MSG: %v", i)
return Failed(100)
}
elem := bot.ConvertStringMessage(str, true)
// fix at display
for _, e := range elem {
if at, ok := e.(*message.AtElement); ok && at.Target != 0 {
at.Display = "@" + bot.Client.FindGroup(groupId).FindMember(at.Target).DisplayName()
}
var elem []message.IMessageElement
if autoEscape {
elem = append(elem, message.NewText(str))
} else {
elem = bot.ConvertStringMessage(str, true)
}
fixAt(elem)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
Expand Down Expand Up @@ -205,7 +225,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
}

// https://cqhttp.cc/docs/4.15/#/API?id=send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF
func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) MSG {
func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape bool) MSG {
var str string
if m, ok := i.(gjson.Result); ok {
if m.Type == gjson.JSON {
Expand All @@ -222,14 +242,18 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) MSG {
}
return m.Raw
}()
}
if s, ok := i.(string); ok {
} else if s, ok := i.(string); ok {
str = s
}
if str == "" {
return Failed(100)
}
elem := bot.ConvertStringMessage(str, false)
var elem []message.IMessageElement
if autoEscape {
elem = append(elem, message.NewText(str))
} else {
elem = bot.ConvertStringMessage(str, false)
}
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
Expand Down Expand Up @@ -322,7 +346,7 @@ func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG {
}

// https://cqhttp.cc/docs/4.15/#/API?id=set_group_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E7%BE%A4%E8%AF%B7%E6%B1%82%EF%BC%8F%E9%82%80%E8%AF%B7
func (bot *CQBot) CQProcessGroupRequest(flag, subType string, approve bool) MSG {
func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bool) MSG {
if subType == "add" {
req, ok := bot.joinReqCache.Load(flag)
if !ok {
Expand All @@ -332,7 +356,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType string, approve bool) MSG
if approve {
req.(*client.UserJoinGroupRequest).Accept()
} else {
req.(*client.UserJoinGroupRequest).Reject()
req.(*client.UserJoinGroupRequest).Reject(false, reason)
}
return OK(nil)
}
Expand All @@ -342,7 +366,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType string, approve bool) MSG
if approve {
req.(*client.GroupInvitedRequest).Accept()
} else {
req.(*client.GroupInvitedRequest).Reject()
req.(*client.GroupInvitedRequest).Reject(false, reason)
}
return OK(nil)
}
Expand All @@ -359,6 +383,61 @@ func (bot *CQBot) CQDeleteMessage(messageId int32) MSG {
return OK(nil)
}

// 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}
convertMem := func(memList []client.HonorMemberInfo) (ret []MSG) {
for _, mem := range memList {
ret = append(ret, MSG{
"user_id": mem.Uin,
"nickname": mem.Name,
"avatar": mem.Avatar,
"description": mem.Desc,
})
}
return
}
if t == "talkative" || t == "all" {
if honor, err := bot.Client.GetGroupHonorInfo(groupId, client.Talkative); err == nil {
if honor.CurrentTalkative.Uin != 0 {
msg["current_talkative"] = MSG{
"user_id": honor.CurrentTalkative.Uin,
"nickname": honor.CurrentTalkative.Name,
"avatar": honor.CurrentTalkative.Avatar,
"day_count": honor.CurrentTalkative.DayCount,
}
}
msg["talkative_list"] = convertMem(honor.TalkativeList)
}
}

if t == "performer" || t == "all" {
if honor, err := bot.Client.GetGroupHonorInfo(groupId, client.Performer); err == nil {
msg["performer_lis"] = convertMem(honor.ActorList)
}
}

if t == "legend" || t == "all" {
if honor, err := bot.Client.GetGroupHonorInfo(groupId, client.Legend); err == nil {
msg["legend_list"] = convertMem(honor.LegendList)
}
}

if t == "strong_newbie" || t == "all" {
if honor, err := bot.Client.GetGroupHonorInfo(groupId, client.StrongNewbie); err == nil {
msg["strong_newbie_list"] = convertMem(honor.StrongNewbieList)
}
}

if t == "emotion" || t == "all" {
if honor, err := bot.Client.GetGroupHonorInfo(groupId, client.Emotion); err == nil {
msg["emotion_list"] = convertMem(honor.EmotionList)
}
}

return OK(msg)
}

// 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 All @@ -368,6 +447,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
msgType := context.Get("message_type").Str
reply := operation.Get("reply")
if reply.Exists() {
autoEscape := global.EnsureBool(operation.Get("auto_escape"), false)
/*
at := true
if operation.Get("at_sender").Exists() {
Expand All @@ -376,10 +456,10 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
*/
// TODO: 处理at字段
if msgType == "group" {
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply)
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply, autoEscape)
}
if msgType == "private" {
bot.CQSendPrivateMessage(context.Get("user_id").Int(), reply)
bot.CQSendPrivateMessage(context.Get("user_id").Int(), reply, autoEscape)
}
}
if msgType == "group" {
Expand All @@ -404,12 +484,12 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
}
case "request":
reqType := context.Get("request_type").Str
if context.Get("approve").Bool() {
if operation.Get("approve").Exists() {
if reqType == "friend" {
bot.CQProcessFriendRequest(context.Get("flag").Str, true)
bot.CQProcessFriendRequest(context.Get("flag").Str, operation.Get("approve").Bool())
}
if reqType == "group" {
bot.CQProcessGroupRequest(context.Get("flag").Str, context.Get("sub_type").Str, true)
bot.CQProcessGroupRequest(context.Get("flag").Str, context.Get("sub_type").Str, operation.Get("reason").Str, operation.Get("approve").Bool())
}
}
}
Expand All @@ -423,11 +503,19 @@ func (bot *CQBot) CQGetImage(file string) MSG {
if b, err := ioutil.ReadFile(path.Join(global.IMAGE_PATH, file)); err == nil {
r := binary.NewReader(b)
r.ReadBytes(16)
return OK(MSG{
msg := MSG{
"size": r.ReadInt32(),
"filename": r.ReadString(),
"url": r.ReadString(),
})
}
local := path.Join(global.CACHE_PATH, file+"."+path.Ext(msg["filename"].(string)))
if !global.PathExists(local) {
if data, err := global.GetBytes(msg["url"].(string)); err == nil {
_ = ioutil.WriteFile(local, data, 0644)
}
}
msg["file"] = local
return OK(msg)
}
return Failed(100)
}
Expand All @@ -439,14 +527,14 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
}
var r []MSG
for _, n := range m.Nodes {
checkMedia(n.Message)
bot.checkMedia(n.Message)
r = append(r, MSG{
"sender": MSG{
"user_id": n.SenderId,
"nickname": n.SenderName,
},
"time": n.Time,
"content": ToStringMessage(n.Message, 0, false),
"content": ToFormattedMessage(n.Message, 0, false),
})
}
return OK(MSG{
Expand Down Expand Up @@ -502,6 +590,7 @@ func (bot *CQBot) CQGetVersionInfo() MSG {
"plugin_build_configuration": "release",
"runtime_version": runtime.Version(),
"runtime_os": runtime.GOOS,
"version": version,
})
}

Expand Down
Loading

0 comments on commit 8b14409

Please sign in to comment.