mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
first commit.
This commit is contained in:
commit
4d233152f3
338
coolq/api.go
Normal file
338
coolq/api.go
Normal file
@ -0,0 +1,338 @@
|
|||||||
|
package coolq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
for _, f := range bot.Client.FriendList {
|
||||||
|
fs = append(fs, MSG{
|
||||||
|
"nickname": f.Nickname,
|
||||||
|
"remark": f.Remark,
|
||||||
|
"user_id": f.Uin,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return OK(fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
var gs []MSG
|
||||||
|
for _, g := range bot.Client.GroupList {
|
||||||
|
gs = append(gs, MSG{
|
||||||
|
"group_id": g.Code,
|
||||||
|
"group_name": g.Name,
|
||||||
|
"max_member_count": g.MaxMemberCount,
|
||||||
|
"member_count": g.MemberCount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return OK(gs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=get_group_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E4%BF%A1%E6%81%AF
|
||||||
|
func (bot *CQBot) CQGetGroupInfo(groupId int64) MSG {
|
||||||
|
group := bot.Client.FindGroup(groupId)
|
||||||
|
if group == nil {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
return OK(MSG{
|
||||||
|
"group_id": group.Code,
|
||||||
|
"group_name": group.Name,
|
||||||
|
"max_member_count": group.MaxMemberCount,
|
||||||
|
"member_count": group.MemberCount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=get_group_member_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E5%88%97%E8%A1%A8
|
||||||
|
func (bot *CQBot) CQGetGroupMemberList(groupId int64) MSG {
|
||||||
|
group := bot.Client.FindGroup(groupId)
|
||||||
|
if group == nil {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
var members []MSG
|
||||||
|
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 {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
return OK(convertGroupMemberInfo(groupId, member))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, msg string) MSG {
|
||||||
|
if msg == "" {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
elem := bot.ConvertStringMessage(msg, true)
|
||||||
|
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
|
||||||
|
if mid == -1 {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
return OK(MSG{"message_id": mid})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, msg string) MSG {
|
||||||
|
if msg == "" {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
elem := bot.ConvertStringMessage(msg, false)
|
||||||
|
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
|
||||||
|
if mid == -1 {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
return OK(MSG{"message_id": mid})
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%EF%BC%88%E7%BE%A4%E5%A4%87%E6%B3%A8%EF%BC%89
|
||||||
|
func (bot *CQBot) CQSetGroupCard(groupId, userId int64, card string) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
if m := g.FindMember(userId); m != nil {
|
||||||
|
m.EditCard(card)
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_special_title-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E7%BB%84%E4%B8%93%E5%B1%9E%E5%A4%B4%E8%A1%94
|
||||||
|
func (bot *CQBot) CQSetGroupSpecialTitle(groupId, userId int64, title string) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
if m := g.FindMember(userId); m != nil {
|
||||||
|
m.EditSpecialTitle(title)
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQSetGroupName(groupId int64, name string) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
g.UpdateName(name)
|
||||||
|
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) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
if m := g.FindMember(userId); m != nil {
|
||||||
|
m.Kick(msg)
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_ban-%E7%BE%A4%E7%BB%84%E5%8D%95%E4%BA%BA%E7%A6%81%E8%A8%80
|
||||||
|
func (bot *CQBot) CQSetGroupBan(groupId, userId int64, duration uint32) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
if m := g.FindMember(userId); m != nil {
|
||||||
|
m.Mute(duration)
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80
|
||||||
|
func (bot *CQBot) CQSetGroupWholeBan(groupId int64, enable bool) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
g.MuteAll(enable)
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82
|
||||||
|
func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG {
|
||||||
|
req, ok := bot.friendReqCache.Load(flag)
|
||||||
|
if !ok {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
if approve {
|
||||||
|
req.(*client.NewFriendRequest).Accept()
|
||||||
|
} else {
|
||||||
|
req.(*client.NewFriendRequest).Reject()
|
||||||
|
}
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
if subType == "add" {
|
||||||
|
req, ok := bot.joinReqCache.Load(flag)
|
||||||
|
if !ok {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
bot.joinReqCache.Delete(flag)
|
||||||
|
if approve {
|
||||||
|
req.(*client.UserJoinGroupRequest).Accept()
|
||||||
|
} else {
|
||||||
|
req.(*client.UserJoinGroupRequest).Reject()
|
||||||
|
}
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
req, ok := bot.invitedReqCache.Load(flag)
|
||||||
|
if ok {
|
||||||
|
bot.invitedReqCache.Delete(flag)
|
||||||
|
if approve {
|
||||||
|
req.(*client.GroupInvitedRequest).Accept()
|
||||||
|
} else {
|
||||||
|
req.(*client.GroupInvitedRequest).Reject()
|
||||||
|
}
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF
|
||||||
|
func (bot *CQBot) CQDeleteMessage(messageId int32) MSG {
|
||||||
|
msg := bot.GetGroupMessage(messageId)
|
||||||
|
if msg == nil {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
bot.Client.RecallGroupMessage(msg["group"].(int64), msg["message-id"].(int32), msg["internal-id"].(int32))
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQGetImage(file string) MSG {
|
||||||
|
if !global.PathExists(path.Join(global.IMAGE_PATH, file)) {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
if b, err := ioutil.ReadFile(path.Join(global.IMAGE_PATH, file)); err == nil {
|
||||||
|
r := binary.NewReader(b)
|
||||||
|
r.ReadBytes(16)
|
||||||
|
return OK(MSG{
|
||||||
|
"size": r.ReadInt32(),
|
||||||
|
"filenames": r.ReadString(),
|
||||||
|
"url": r.ReadString(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQGetGroupMessage(messageId int32) MSG {
|
||||||
|
msg := bot.GetGroupMessage(messageId)
|
||||||
|
if msg == nil {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
sender := msg["sender"].(message.Sender)
|
||||||
|
return OK(MSG{
|
||||||
|
"message_id": messageId,
|
||||||
|
"real_id": msg["message-id"],
|
||||||
|
"sender": gin.H{
|
||||||
|
"user_id": sender.Uin,
|
||||||
|
"nickname": sender.Nickname,
|
||||||
|
},
|
||||||
|
"time": msg["time"],
|
||||||
|
"content": msg["message"],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQCanSendImage() MSG {
|
||||||
|
return OK(MSG{"yes": true})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQCanSendRecord() MSG {
|
||||||
|
return OK(MSG{"yes": false})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQGetStatus() MSG {
|
||||||
|
return OK(MSG{
|
||||||
|
"app_initialized": true,
|
||||||
|
"app_enabled": true,
|
||||||
|
"plugins_good": nil,
|
||||||
|
"app_good": true,
|
||||||
|
"online": bot.Client.Online,
|
||||||
|
"good": true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQGetVersionInfo() MSG {
|
||||||
|
wd, _ := os.Getwd()
|
||||||
|
return OK(MSG{
|
||||||
|
"coolq_directory": wd,
|
||||||
|
"coolq_edition": "pro",
|
||||||
|
"go-cqhttp": true,
|
||||||
|
"plugin_version": "4.15.0",
|
||||||
|
"plugin_build_number": 99,
|
||||||
|
"plugin_build_configuration": "release",
|
||||||
|
"runtime_version": runtime.Version(),
|
||||||
|
"runtime_os": runtime.GOOS,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func OK(data interface{}) MSG {
|
||||||
|
return MSG{"data": data, "retcode": 0, "status": "ok"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Failed(code int) MSG {
|
||||||
|
return MSG{"data": nil, "retcode": code, "status": "failed"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertGroupMemberInfo(groupId int64, m *client.GroupMemberInfo) MSG {
|
||||||
|
return MSG{
|
||||||
|
"group_id": groupId,
|
||||||
|
"user_id": m.Uin,
|
||||||
|
"nickname": m.Nickname,
|
||||||
|
"card": m.CardName,
|
||||||
|
"sex": "unknown",
|
||||||
|
"age": 0,
|
||||||
|
"area": "",
|
||||||
|
"join_time": m.JoinTime,
|
||||||
|
"last_sent_time": m.LastSpeakTime,
|
||||||
|
"level": strconv.FormatInt(int64(m.Level), 10),
|
||||||
|
"role": func() string {
|
||||||
|
switch m.Permission {
|
||||||
|
case client.Owner:
|
||||||
|
return "owner"
|
||||||
|
case client.Administrator:
|
||||||
|
return "admin"
|
||||||
|
default:
|
||||||
|
return "member"
|
||||||
|
}
|
||||||
|
}(),
|
||||||
|
"unfriendly": false,
|
||||||
|
"title": m.SpecialTitle,
|
||||||
|
"title_expire_time": m.SpecialTitleExpireTime,
|
||||||
|
"card_changeable": false,
|
||||||
|
}
|
||||||
|
}
|
177
coolq/bot.go
Normal file
177
coolq/bot.go
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
package coolq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"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/xujiajun/nutsdb"
|
||||||
|
"hash/crc32"
|
||||||
|
"path"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CQBot struct {
|
||||||
|
Client *client.QQClient
|
||||||
|
|
||||||
|
events []func(string)
|
||||||
|
db *nutsdb.DB
|
||||||
|
friendReqCache sync.Map
|
||||||
|
invitedReqCache sync.Map
|
||||||
|
joinReqCache sync.Map
|
||||||
|
}
|
||||||
|
|
||||||
|
type MSG map[string]interface{}
|
||||||
|
|
||||||
|
func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
|
||||||
|
bot := &CQBot{
|
||||||
|
Client: cli,
|
||||||
|
}
|
||||||
|
if conf.EnableDB {
|
||||||
|
opt := nutsdb.DefaultOptions
|
||||||
|
opt.Dir = path.Join("data", "db")
|
||||||
|
opt.EntryIdxMode = nutsdb.HintBPTSparseIdxMode
|
||||||
|
db, err := nutsdb.Open(opt)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("打开数据库失败, 如果频繁遇到此问题请关闭数据库功能。")
|
||||||
|
}
|
||||||
|
bot.db = db
|
||||||
|
gob.Register(message.Sender{})
|
||||||
|
log.Info("信息数据库初始化完成.")
|
||||||
|
} else {
|
||||||
|
log.Warn("警告: 信息数据库已关闭,将无法使用 [回复/撤回] 等功能。")
|
||||||
|
}
|
||||||
|
bot.Client.OnPrivateMessage(bot.privateMessageEvent)
|
||||||
|
bot.Client.OnGroupMessage(bot.groupMessageEvent)
|
||||||
|
bot.Client.OnTempMessage(bot.tempMessageEvent)
|
||||||
|
bot.Client.OnGroupMuted(bot.groupMutedEvent)
|
||||||
|
bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent)
|
||||||
|
bot.Client.OnJoinGroup(bot.joinGroupEvent)
|
||||||
|
bot.Client.OnLeaveGroup(bot.leaveGroupEvent)
|
||||||
|
bot.Client.OnGroupMemberJoined(bot.memberJoinEvent)
|
||||||
|
bot.Client.OnGroupMemberLeaved(bot.memberLeaveEvent)
|
||||||
|
bot.Client.OnGroupMemberPermissionChanged(bot.memberPermissionChangedEvent)
|
||||||
|
bot.Client.OnNewFriendRequest(bot.friendRequestEvent)
|
||||||
|
bot.Client.OnGroupInvited(bot.groupInvitedEvent)
|
||||||
|
bot.Client.OnUserWantJoinGroup(bot.groupJoinReqEvent)
|
||||||
|
return bot
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) OnEventPush(f func(m string)) {
|
||||||
|
bot.events = append(bot.events, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
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(e.Value)
|
||||||
|
return gob.NewDecoder(buff).Decode(&m)
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
log.Warnf("获取信息时出现错误: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) SendGroupMessage(groupId int64, m *message.SendingMessage) int32 {
|
||||||
|
var newElem []message.IMessageElement
|
||||||
|
for _, elem := range m.Elements {
|
||||||
|
if i, ok := elem.(*message.ImageElement); ok {
|
||||||
|
gm, err := bot.Client.UploadGroupImage(groupId, i.Data)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("警告: 群 %v 消息图片上传失败.", groupId)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newElem = append(newElem, gm)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newElem = append(newElem, elem)
|
||||||
|
}
|
||||||
|
m.Elements = newElem
|
||||||
|
ret := bot.Client.SendGroupMessage(groupId, m)
|
||||||
|
return bot.InsertGroupMessage(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) SendPrivateMessage(target int64, m *message.SendingMessage) int32 {
|
||||||
|
var newElem []message.IMessageElement
|
||||||
|
for _, elem := range m.Elements {
|
||||||
|
if i, ok := elem.(*message.ImageElement); ok {
|
||||||
|
fm, err := bot.Client.UploadPrivateImage(target, i.Data)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("警告: 好友 %v 消息图片上传失败.", target)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newElem = append(newElem, fm)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newElem = append(newElem, elem)
|
||||||
|
}
|
||||||
|
m.Elements = newElem
|
||||||
|
ret := bot.Client.SendPrivateMessage(target, m)
|
||||||
|
return ToGlobalId(target, ret.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
|
||||||
|
val := MSG{
|
||||||
|
"message-id": m.Id,
|
||||||
|
"internal-id": m.InternalId,
|
||||||
|
"group": m.GroupCode,
|
||||||
|
"group-name": m.GroupName,
|
||||||
|
"sender": m.Sender,
|
||||||
|
"time": m.Time,
|
||||||
|
"message": ToStringMessage(m.Elements, m.GroupCode, true),
|
||||||
|
}
|
||||||
|
id := ToGlobalId(m.GroupCode, m.Id)
|
||||||
|
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), buf.Bytes(), 0)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("记录聊天数据时出现错误: %v", err)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToGlobalId(code int64, msgId int32) int32 {
|
||||||
|
return int32(crc32.ChecksumIEEE([]byte(fmt.Sprintf("%d-%d", code, msgId))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) Release() {
|
||||||
|
_ = bot.db.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) dispatchEventMessage(m string) {
|
||||||
|
for _, f := range bot.events {
|
||||||
|
f(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatGroupName(group *client.GroupInfo) string {
|
||||||
|
return fmt.Sprintf("%s(%d)", group.Name, group.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatMemberName(mem *client.GroupMemberInfo) string {
|
||||||
|
return fmt.Sprintf("%s(%d)", mem.DisplayName(), mem.Uin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MSG) ToJson() string {
|
||||||
|
b, _ := json.Marshal(m)
|
||||||
|
return string(b)
|
||||||
|
}
|
164
coolq/cqcode.go
Normal file
164
coolq/cqcode.go
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
package coolq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"io/ioutil"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var matchReg = regexp.MustCompile(`\[CQ:\w+?.*?]`)
|
||||||
|
var typeReg = regexp.MustCompile(`\[CQ:(\w+)`)
|
||||||
|
var paramReg = regexp.MustCompile(`,([\w\-.]+?)=([^,\]]+)`)
|
||||||
|
|
||||||
|
func ToStringMessage(e []message.IMessageElement, code int64, raw ...bool) (r string) {
|
||||||
|
ur := false
|
||||||
|
if len(raw) != 0 {
|
||||||
|
ur = raw[0]
|
||||||
|
}
|
||||||
|
for _, elem := range e {
|
||||||
|
switch o := elem.(type) {
|
||||||
|
case *message.TextElement:
|
||||||
|
r += o.Content
|
||||||
|
case *message.AtElement:
|
||||||
|
if o.Target == 0 {
|
||||||
|
r += "[CQ:at,qq=all]"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r += fmt.Sprintf("[CQ:at,qq=%d]", o.Target)
|
||||||
|
case *message.ReplyElement:
|
||||||
|
r += fmt.Sprintf("[CQ:reply,id=%d]", ToGlobalId(code, o.ReplySeq))
|
||||||
|
case *message.FaceElement:
|
||||||
|
r += fmt.Sprintf(`[CQ:face,id=%d]`, o.Index)
|
||||||
|
case *message.ImageElement:
|
||||||
|
if ur {
|
||||||
|
r += fmt.Sprintf(`[CQ:image,file=%s]`, o.Filename)
|
||||||
|
} else {
|
||||||
|
r += fmt.Sprintf(`[CQ:image,file=%s,url=%s]`, o.Filename, o.Url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessageElement) {
|
||||||
|
i := matchReg.FindAllStringSubmatchIndex(m, -1)
|
||||||
|
si := 0
|
||||||
|
for _, idx := range i {
|
||||||
|
if idx[0] > si {
|
||||||
|
text := m[si:idx[0]]
|
||||||
|
r = append(r, message.NewText(text))
|
||||||
|
}
|
||||||
|
code := m[idx[0]:idx[1]]
|
||||||
|
si = idx[1]
|
||||||
|
t := typeReg.FindAllStringSubmatch(code, -1)[0][1]
|
||||||
|
ps := paramReg.FindAllStringSubmatch(code, -1)
|
||||||
|
d := make(map[string]string)
|
||||||
|
for _, p := range ps {
|
||||||
|
d[p[1]] = p[2]
|
||||||
|
}
|
||||||
|
if t == "reply" && group {
|
||||||
|
if len(r) > 0 {
|
||||||
|
if _, ok := r[0].(*message.ReplyElement); ok {
|
||||||
|
log.Warnf("警告: 一条信息只能包含一个 Reply 元素.")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mid, err := strconv.Atoi(d["id"])
|
||||||
|
if err == nil {
|
||||||
|
org := bot.GetGroupMessage(int32(mid))
|
||||||
|
if org != nil {
|
||||||
|
r = append([]message.IMessageElement{
|
||||||
|
&message.ReplyElement{
|
||||||
|
ReplySeq: org["message-id"].(int32),
|
||||||
|
Sender: org["sender"].(message.Sender).Uin,
|
||||||
|
Time: org["time"].(int32),
|
||||||
|
Elements: bot.ConvertStringMessage(org["message"].(string), group),
|
||||||
|
},
|
||||||
|
}, r...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elem, err := bot.ToElement(t, d, group)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("转换CQ码到MiraiGo Element时出现错误: %v 将忽略本段CQ码.", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r = append(r, elem)
|
||||||
|
}
|
||||||
|
if si != len(m) {
|
||||||
|
r = append(r, message.NewText(m[si:]))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.IMessageElement, error) {
|
||||||
|
switch t {
|
||||||
|
case "text":
|
||||||
|
return message.NewText(d["text"]), nil
|
||||||
|
case "image":
|
||||||
|
f := d["file"]
|
||||||
|
if strings.HasPrefix(f, "http") || strings.HasPrefix(f, "https") {
|
||||||
|
b, err := global.GetBytes(f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return message.NewImage(b), nil
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(f, "base64") {
|
||||||
|
b, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", ""))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return message.NewImage(b), nil
|
||||||
|
}
|
||||||
|
if global.PathExists(path.Join(global.IMAGE_PATH, f)) {
|
||||||
|
b, err := ioutil.ReadFile(path.Join(global.IMAGE_PATH, f))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(b) < 20 {
|
||||||
|
return nil, errors.New("invalid local file")
|
||||||
|
}
|
||||||
|
r := binary.NewReader(b)
|
||||||
|
hash := r.ReadBytes(16)
|
||||||
|
if group {
|
||||||
|
rsp, err := bot.Client.QueryGroupImage(1, hash, r.ReadInt32())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return rsp, nil
|
||||||
|
}
|
||||||
|
rsp, err := bot.Client.QueryFriendImage(1, hash, r.ReadInt32())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return rsp, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("invalid image")
|
||||||
|
case "face":
|
||||||
|
id, err := strconv.Atoi(d["id"])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return message.NewFace(int32(id)), nil
|
||||||
|
case "at":
|
||||||
|
qq := d["qq"]
|
||||||
|
if qq == "all" {
|
||||||
|
return message.AtAll(), nil
|
||||||
|
}
|
||||||
|
t, _ := strconv.ParseInt(qq, 10, 64)
|
||||||
|
return message.NewAt(t), nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("unsupported cq code: " + t)
|
||||||
|
}
|
||||||
|
}
|
316
coolq/event.go
Normal file
316
coolq/event.go
Normal file
@ -0,0 +1,316 @@
|
|||||||
|
package coolq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"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"
|
||||||
|
"io/ioutil"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMessage) {
|
||||||
|
checkImage(m.Elements)
|
||||||
|
cqm := ToStringMessage(m.Elements, 0, true)
|
||||||
|
log.Infof("收到好友 %v(%v) 的消息: %v", m.Sender.DisplayName(), m.Sender.Uin, cqm)
|
||||||
|
fm := MSG{
|
||||||
|
"post_type": "message",
|
||||||
|
"message_type": "private",
|
||||||
|
"sub_type": "friend",
|
||||||
|
"message_id": m.Id,
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
"message": ToStringMessage(m.Elements, 0, false),
|
||||||
|
"raw_message": cqm,
|
||||||
|
"font": 0,
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"sender": MSG{
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
"nickname": m.Sender.Nickname,
|
||||||
|
"sex": "unknown",
|
||||||
|
"age": 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
bot.dispatchEventMessage(fm.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) {
|
||||||
|
checkImage(m.Elements)
|
||||||
|
cqm := ToStringMessage(m.Elements, m.GroupCode, true)
|
||||||
|
id := m.Id
|
||||||
|
if bot.db != nil {
|
||||||
|
id = bot.InsertGroupMessage(m)
|
||||||
|
}
|
||||||
|
log.Infof("收到群 %v(%v) 内 %v(%v) 的消息: %v (%v)", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
|
||||||
|
gm := MSG{
|
||||||
|
"anonymous": nil,
|
||||||
|
"font": 0,
|
||||||
|
"group_id": m.GroupCode,
|
||||||
|
"message": ToStringMessage(m.Elements, m.GroupCode, false),
|
||||||
|
"message_id": id,
|
||||||
|
"message_type": "group",
|
||||||
|
"post_type": "message",
|
||||||
|
"raw_message": cqm,
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"sender": MSG{
|
||||||
|
"age": 0,
|
||||||
|
"area": "",
|
||||||
|
"level": "",
|
||||||
|
"sex": "unknown",
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
},
|
||||||
|
"sub_type": "normal",
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
}
|
||||||
|
if m.Sender.IsAnonymous() {
|
||||||
|
gm["anonymous"] = MSG{
|
||||||
|
"flag": "",
|
||||||
|
"id": 0,
|
||||||
|
"name": m.Sender.Nickname,
|
||||||
|
}
|
||||||
|
gm["sender"].(MSG)["nickname"] = "匿名消息"
|
||||||
|
gm["sub_type"] = "anonymous"
|
||||||
|
} else {
|
||||||
|
mem := c.FindGroup(m.GroupCode).FindMember(m.Sender.Uin)
|
||||||
|
ms := gm["sender"].(MSG)
|
||||||
|
ms["role"] = func() string {
|
||||||
|
switch mem.Permission {
|
||||||
|
case client.Owner:
|
||||||
|
return "owner"
|
||||||
|
case client.Administrator:
|
||||||
|
return "admin"
|
||||||
|
default:
|
||||||
|
return "member"
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
ms["nickname"] = mem.Nickname
|
||||||
|
ms["card"] = mem.CardName
|
||||||
|
ms["title"] = mem.SpecialTitle
|
||||||
|
}
|
||||||
|
bot.dispatchEventMessage(gm.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) tempMessageEvent(c *client.QQClient, m *message.TempMessage) {
|
||||||
|
checkImage(m.Elements)
|
||||||
|
cqm := ToStringMessage(m.Elements, 0, true)
|
||||||
|
log.Infof("收到来自群 %v(%v) 内 %v(%v) 的临时会话消息: %v", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm)
|
||||||
|
tm := MSG{
|
||||||
|
"post_type": "message",
|
||||||
|
"message_type": "private",
|
||||||
|
"sub_type": "group",
|
||||||
|
"message_id": m.Id,
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
"message": ToStringMessage(m.Elements, 0, false),
|
||||||
|
"raw_message": cqm,
|
||||||
|
"font": 0,
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"sender": MSG{
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
"nickname": m.Sender.Nickname,
|
||||||
|
"sex": "unknown",
|
||||||
|
"age": 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
bot.dispatchEventMessage(tm.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupMutedEvent(c *client.QQClient, e *client.GroupMuteEvent) {
|
||||||
|
g := c.FindGroup(e.GroupCode)
|
||||||
|
if e.Time > 0 {
|
||||||
|
log.Infof("群 %v 内 %v 被 %v 禁言了 %v秒.",
|
||||||
|
formatGroupName(g), formatMemberName(g.FindMember(e.TargetUin)), formatMemberName(g.FindMember(e.OperatorUin)), e.Time)
|
||||||
|
} else {
|
||||||
|
log.Infof("群 %v 内 %v 被 %v 解除禁言.",
|
||||||
|
formatGroupName(g), formatMemberName(g.FindMember(e.TargetUin)), formatMemberName(g.FindMember(e.OperatorUin)))
|
||||||
|
}
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"duration": e.Time,
|
||||||
|
"group_id": e.GroupCode,
|
||||||
|
"notice_type": "group_ban",
|
||||||
|
"operator_id": e.OperatorUin,
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"user_id": e.TargetUin,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"sub_type": func() string {
|
||||||
|
if e.Time > 0 {
|
||||||
|
return "ban"
|
||||||
|
}
|
||||||
|
return "lift_ban"
|
||||||
|
}(),
|
||||||
|
}.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupRecallEvent(c *client.QQClient, e *client.GroupMessageRecalledEvent) {
|
||||||
|
g := c.FindGroup(e.GroupCode)
|
||||||
|
gid := ToGlobalId(e.GroupCode, e.MessageId)
|
||||||
|
log.Infof("群 %v 内 %v 撤回了 %v 的消息: %v.",
|
||||||
|
formatGroupName(g), formatMemberName(g.FindMember(e.OperatorUin)), formatMemberName(g.FindMember(e.AuthorUin)), gid)
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"group_id": e.GroupCode,
|
||||||
|
"notice_type": "group_recall",
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"user_id": e.AuthorUin,
|
||||||
|
"operator_id": e.OperatorUin,
|
||||||
|
"time": e.Time,
|
||||||
|
"message_id": gid,
|
||||||
|
}.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) joinGroupEvent(c *client.QQClient, group *client.GroupInfo) {
|
||||||
|
log.Infof("Bot进入了群 %v.", formatGroupName(group))
|
||||||
|
bot.dispatchEventMessage(bot.groupIncrease(group.Code, 0, c.Uin))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) leaveGroupEvent(c *client.QQClient, e *client.GroupLeaveEvent) {
|
||||||
|
if e.Operator != nil {
|
||||||
|
log.Infof("Bot被 %v T出了群 %v.", formatMemberName(e.Operator), formatGroupName(e.Group))
|
||||||
|
} else {
|
||||||
|
log.Infof("Bot退出了群 %v.", formatGroupName(e.Group))
|
||||||
|
}
|
||||||
|
bot.dispatchEventMessage(bot.groupDecrease(e.Group.Code, c.Uin, e.Operator))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) memberPermissionChangedEvent(c *client.QQClient, e *client.MemberPermissionChangedEvent) {
|
||||||
|
st := func() string {
|
||||||
|
if e.NewPermission == client.Administrator {
|
||||||
|
return "set"
|
||||||
|
}
|
||||||
|
return "unset"
|
||||||
|
}()
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"notice_type": "group_admin",
|
||||||
|
"sub_type": st,
|
||||||
|
"group_id": e.Group.Code,
|
||||||
|
"user_id": e.Member.Uin,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"self_id": c.Uin,
|
||||||
|
}.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) memberJoinEvent(c *client.QQClient, e *client.MemberJoinGroupEvent) {
|
||||||
|
log.Infof("新成员 %v 进入了群 %v.", formatMemberName(e.Member), formatGroupName(e.Group))
|
||||||
|
bot.dispatchEventMessage(bot.groupIncrease(e.Group.Code, 0, e.Member.Uin))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) memberLeaveEvent(c *client.QQClient, e *client.MemberLeaveGroupEvent) {
|
||||||
|
if e.Operator != nil {
|
||||||
|
log.Infof("成员 %v 被 %v T出了群 %v.", formatMemberName(e.Member), formatMemberName(e.Operator), formatGroupName(e.Group))
|
||||||
|
} else {
|
||||||
|
log.Infof("成员 %v 离开了群 %v.", formatMemberName(e.Member), formatGroupName(e.Group))
|
||||||
|
}
|
||||||
|
bot.dispatchEventMessage(bot.groupDecrease(e.Group.Code, e.Member.Uin, e.Operator))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) friendRequestEvent(c *client.QQClient, e *client.NewFriendRequest) {
|
||||||
|
log.Infof("收到来自 %v(%v) 的好友请求: %v", e.RequesterNick, e.RequesterUin, e.Message)
|
||||||
|
flag := strconv.FormatInt(e.RequestId, 10)
|
||||||
|
bot.friendReqCache.Store(flag, e)
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "request",
|
||||||
|
"request_type": "friend",
|
||||||
|
"user_id": e.RequesterUin,
|
||||||
|
"comment": e.Message,
|
||||||
|
"flag": flag,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"self_id": c.Uin,
|
||||||
|
}.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupInvitedEvent(c *client.QQClient, e *client.GroupInvitedRequest) {
|
||||||
|
log.Infof("收到来自群 %v(%v) 内用户 %v(%v) 的加群邀请.", e.GroupName, e.GroupCode, e.InvitorNick, e.InvitorUin)
|
||||||
|
flag := strconv.FormatInt(e.RequestId, 10)
|
||||||
|
bot.invitedReqCache.Store(flag, e)
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "request",
|
||||||
|
"request_type": "group",
|
||||||
|
"sub_type": "invite",
|
||||||
|
"group_id": e.GroupCode,
|
||||||
|
"user_id": e.InvitorUin,
|
||||||
|
"comment": "",
|
||||||
|
"flag": flag,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"self_id": c.Uin,
|
||||||
|
}.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupJoinReqEvent(c *client.QQClient, e *client.UserJoinGroupRequest) {
|
||||||
|
log.Infof("群 %v(%v) 收到来自用户 %v(%v) 的加群请求.", e.GroupName, e.GroupName, e.RequesterNick, e.RequesterUin)
|
||||||
|
flag := strconv.FormatInt(e.RequestId, 10)
|
||||||
|
bot.joinReqCache.Store(flag, e)
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "request",
|
||||||
|
"request_type": "group",
|
||||||
|
"sub_type": "add",
|
||||||
|
"group_id": e.GroupCode,
|
||||||
|
"user_id": e.RequesterUin,
|
||||||
|
"comment": "",
|
||||||
|
"flag": flag,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"self_id": c.Uin,
|
||||||
|
}.ToJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupIncrease(groupCode, operatorUin, userUin int64) string {
|
||||||
|
return MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"notice_type": "group_increase",
|
||||||
|
"group_id": groupCode,
|
||||||
|
"operator_id": operatorUin,
|
||||||
|
"self_id": bot.Client.Uin,
|
||||||
|
"sub_type": "approve",
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"user_id": userUin,
|
||||||
|
}.ToJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) groupDecrease(groupCode, userUin int64, operator *client.GroupMemberInfo) string {
|
||||||
|
return MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"notice_type": "group_decrease",
|
||||||
|
"group_id": groupCode,
|
||||||
|
"operator_id": func() int64 {
|
||||||
|
if operator != nil {
|
||||||
|
return operator.Uin
|
||||||
|
}
|
||||||
|
return userUin
|
||||||
|
}(),
|
||||||
|
"self_id": bot.Client.Uin,
|
||||||
|
"sub_type": func() string {
|
||||||
|
if operator != nil {
|
||||||
|
if userUin == bot.Client.Uin {
|
||||||
|
return "kick_me"
|
||||||
|
}
|
||||||
|
return "kick"
|
||||||
|
}
|
||||||
|
return "leave"
|
||||||
|
}(),
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
"user_id": userUin,
|
||||||
|
}.ToJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkImage(e []message.IMessageElement) {
|
||||||
|
for _, elem := range e {
|
||||||
|
if i, ok := elem.(*message.ImageElement); ok {
|
||||||
|
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||||
|
if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) {
|
||||||
|
_ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||||
|
w.Write(i.Md5)
|
||||||
|
w.WriteUInt32(uint32(i.Size))
|
||||||
|
w.WriteString(i.Filename)
|
||||||
|
w.WriteString(i.Url)
|
||||||
|
}), 0777)
|
||||||
|
}
|
||||||
|
i.Filename = filename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
101
global/art.go
Normal file
101
global/art.go
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"golang.org/x/image/font"
|
||||||
|
"golang.org/x/image/font/basicfont"
|
||||||
|
"golang.org/x/image/math/fixed"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
_ "image/jpeg"
|
||||||
|
_ "image/png"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://github.com/xrlin/AsciiArt
|
||||||
|
|
||||||
|
func Convert(f io.Reader, chars []string, subWidth, subHeight int, imageSwitch bool, bgColor, penColor color.RGBA) (string, *image.NRGBA, error) {
|
||||||
|
var charsLength = len(chars)
|
||||||
|
if charsLength == 0 {
|
||||||
|
return "", nil, fmt.Errorf("no chars provided")
|
||||||
|
}
|
||||||
|
if subWidth == 0 || subHeight == 0 {
|
||||||
|
return "", nil, fmt.Errorf("subWidth and subHeight params is required")
|
||||||
|
}
|
||||||
|
m, _, err := image.Decode(f)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
imageWidth, imageHeight := m.Bounds().Max.X, m.Bounds().Max.Y
|
||||||
|
var img *image.NRGBA
|
||||||
|
if imageSwitch {
|
||||||
|
img = initImage(imageWidth, imageHeight, bgColor)
|
||||||
|
}
|
||||||
|
piecesX, piecesY := imageWidth/subWidth, imageHeight/subHeight
|
||||||
|
var buff bytes.Buffer
|
||||||
|
for y := 0; y < piecesY; y++ {
|
||||||
|
offsetY := y * subHeight
|
||||||
|
for x := 0; x < piecesX; x++ {
|
||||||
|
offsetX := x * subWidth
|
||||||
|
averageBrightness := calculateAverageBrightness(m, image.Rect(offsetX, offsetY, offsetX+subWidth, offsetY+subHeight))
|
||||||
|
char := getCharByBrightness(chars, averageBrightness)
|
||||||
|
buff.WriteString(char)
|
||||||
|
if img != nil {
|
||||||
|
addCharToImage(img, char, x*subWidth, y*subHeight, penColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buff.WriteString("\n")
|
||||||
|
}
|
||||||
|
return buff.String(), img, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initImage(width, height int, bgColor color.RGBA) *image.NRGBA {
|
||||||
|
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||||
|
for x := 0; x < width; x++ {
|
||||||
|
for y := 0; y < height; y++ {
|
||||||
|
img.Set(x, y, bgColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
func calculateAverageBrightness(img image.Image, rect image.Rectangle) float64 {
|
||||||
|
var averageBrightness float64
|
||||||
|
width, height := rect.Max.X-rect.Min.X, rect.Max.Y-rect.Min.Y
|
||||||
|
var brightness float64
|
||||||
|
for x := rect.Min.X; x < rect.Max.X; x++ {
|
||||||
|
for y := rect.Min.Y; y < rect.Max.Y; y++ {
|
||||||
|
r, g, b, _ := img.At(x, y).RGBA()
|
||||||
|
brightness = float64(r>>8+g>>8+b>>8) / 3
|
||||||
|
averageBrightness += brightness
|
||||||
|
}
|
||||||
|
}
|
||||||
|
averageBrightness /= float64(width * height)
|
||||||
|
return averageBrightness
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCharByBrightness(chars []string, brightness float64) string {
|
||||||
|
index := int(brightness*float64(len(chars))) >> 8
|
||||||
|
if index == len(chars) {
|
||||||
|
index--
|
||||||
|
}
|
||||||
|
return chars[len(chars)-index-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func addCharToImage(img *image.NRGBA, char string, x, y int, penColor color.RGBA) {
|
||||||
|
face := basicfont.Face7x13
|
||||||
|
point := fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}
|
||||||
|
d := &font.Drawer{
|
||||||
|
Dst: img,
|
||||||
|
Src: image.NewUniform(penColor),
|
||||||
|
Face: face,
|
||||||
|
Dot: point,
|
||||||
|
}
|
||||||
|
d.DrawString(char)
|
||||||
|
}
|
||||||
|
|
||||||
|
var Colors = map[string]color.RGBA{"black": {0, 0, 0, 255},
|
||||||
|
"gray": {140, 140, 140, 255},
|
||||||
|
"red": {255, 0, 0, 255},
|
||||||
|
"green": {0, 128, 0, 255},
|
||||||
|
"blue": {0, 0, 255, 255}}
|
87
global/config.go
Normal file
87
global/config.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JsonConfig struct {
|
||||||
|
Uin int64 `json:"uin"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
EnableDB bool `json:"enable_db"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
//Reconnect bool `json:"reconnect"`
|
||||||
|
//ReconnectDelay int `json:"reconnect_delay"`
|
||||||
|
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
||||||
|
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CQHttpApiConfig struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
UseHttp bool `json:"use_http"`
|
||||||
|
WSHost string `json:"ws_host"`
|
||||||
|
WSPort uint16 `json:"ws_port"`
|
||||||
|
UseWS bool `json:"use_ws"`
|
||||||
|
WSReverseUrl string `json:"ws_reverse_url"`
|
||||||
|
WSReverseApiUrl string `json:"ws_reverse_api_url"`
|
||||||
|
WSReverseEventUrl string `json:"ws_reverse_event_url"`
|
||||||
|
WSReverseReconnectInterval uint16 `json:"ws_reverse_reconnect_interval"`
|
||||||
|
WSReverseReconnectOnCode1000 bool `json:"ws_reverse_reconnect_on_code_1000"`
|
||||||
|
UseWsReverse bool `json:"use_ws_reverse"`
|
||||||
|
PostUrl string `json:"post_url"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
Secret string `json:"secret"`
|
||||||
|
PostMessageFormat string `json:"post_message_format"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GoCQHttpConfig struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GoCQWebsocketConfig struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultConfig() *JsonConfig {
|
||||||
|
return &JsonConfig{
|
||||||
|
EnableDB: true,
|
||||||
|
HttpConfig: &GoCQHttpConfig{
|
||||||
|
Enabled: true,
|
||||||
|
Host: "0.0.0.0",
|
||||||
|
Port: 5700,
|
||||||
|
},
|
||||||
|
WSConfig: &GoCQWebsocketConfig{
|
||||||
|
Enabled: true,
|
||||||
|
Host: "0.0.0.0",
|
||||||
|
Port: 6700,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(p string) *JsonConfig {
|
||||||
|
if !PathExists(p) {
|
||||||
|
log.Warnf("尝试加载配置文件 %v 失败: 文件不存在", p)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
c := JsonConfig{}
|
||||||
|
err := json.Unmarshal([]byte(ReadAllText(p)), &c)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("尝试加载配置文件 %v 时出现错误: %v", p, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *JsonConfig) Save(p string) error {
|
||||||
|
data, err := json.Marshal(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
WriteAllText(p, string(data))
|
||||||
|
return nil
|
||||||
|
}
|
33
global/fs.go
Normal file
33
global/fs.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
var IMAGE_PATH = path.Join("data", "images")
|
||||||
|
|
||||||
|
func PathExists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil || os.IsExist(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadAllText(path string) string {
|
||||||
|
b, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteAllText(path, text string) {
|
||||||
|
_ = ioutil.WriteFile(path, []byte(text), 0777)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("遇到错误: %v", err)
|
||||||
|
}
|
||||||
|
}
|
34
global/net.go
Normal file
34
global/net.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetBytes(url string) ([]byte, error) {
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header["User-Agent"] = []string{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.61"}
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
|
||||||
|
buffer := bytes.NewBuffer(body)
|
||||||
|
r, _ := gzip.NewReader(buffer)
|
||||||
|
defer r.Close()
|
||||||
|
unCom, err := ioutil.ReadAll(r)
|
||||||
|
return unCom, err
|
||||||
|
}
|
||||||
|
return body, nil
|
||||||
|
}
|
18
go.mod
Normal file
18
go.mod
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
module github.com/Mrs4s/go-cqhttp
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200721195252-2accd73f8b8e
|
||||||
|
github.com/gin-gonic/gin v1.6.3
|
||||||
|
github.com/gorilla/websocket v1.4.2
|
||||||
|
github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible
|
||||||
|
github.com/lestrrat-go/strftime v1.0.1 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.6.0
|
||||||
|
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
|
||||||
|
github.com/tidwall/gjson v1.6.0
|
||||||
|
github.com/xujiajun/nutsdb v0.5.0
|
||||||
|
golang.org/x/image v0.0.0-20200618115811-c13761719519
|
||||||
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
|
||||||
|
)
|
164
go.sum
Normal file
164
go.sum
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200717203209-5ead51215a01 h1:kG9Oj5/jI8PurVDu3M5DjytBgLaPpDcScIY4oo9YjYE=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200717203209-5ead51215a01/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200718181224-a45ad2e14770 h1:sbEcdUqvUFQ5dGaXzJVlwc+Q5tsMORSDGs6vwEmHYDg=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200718181224-a45ad2e14770/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200719130800-9d81397b5d91 h1:0es7eD8Mn2CzPX7c4Ig67zt8Izz/eNyZu2wa4p0aRfY=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200719130800-9d81397b5d91/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200720175644-0a8fa220ea50 h1:phnnq/0GZXsLeoviernp6qD57M2XxBzAuWpHG8B9ESI=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200720175644-0a8fa220ea50/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200720230213-9a7a28f9dcc7 h1:nzGG3nm4gJA7wyvvyxMEvmY7RAJA7HtMTPcCUbrh/v0=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200720230213-9a7a28f9dcc7/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200720231612-a7e460246fbc h1:elEjdwOy2u+Gfz+1UvoverA/x3RKTenbbAuBMwizTGk=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200720231612-a7e460246fbc/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200721195252-2accd73f8b8e h1:68ol9TpLBwbFQU+S6VQ0CY6nQqN2xIPxHWD/rvBZxVI=
|
||||||
|
github.com/Mrs4s/MiraiGo v0.0.0-20200721195252-2accd73f8b8e/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
|
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
|
||||||
|
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
|
||||||
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
|
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
|
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
|
||||||
|
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
|
||||||
|
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||||
|
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
|
||||||
|
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||||
|
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
|
||||||
|
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||||
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
|
github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0=
|
||||||
|
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
||||||
|
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
|
||||||
|
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||||
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||||
|
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
|
||||||
|
github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible h1:4mNlp+/SvALIPFpbXV3kxNJJno9iKFWGxSDE13Kl66Q=
|
||||||
|
github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
|
||||||
|
github.com/lestrrat-go/strftime v1.0.1 h1:o7qz5pmLzPDLyGW4lG6JvTKPUfTFXwe+vOamIYWtnVU=
|
||||||
|
github.com/lestrrat-go/strftime v1.0.1/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
|
||||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
|
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||||
|
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
|
||||||
|
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
|
||||||
|
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
|
||||||
|
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
|
||||||
|
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||||
|
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||||
|
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
|
||||||
|
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||||
|
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||||
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||||
|
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||||
|
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||||
|
github.com/xujiajun/gorouter v1.2.0/go.mod h1:yJrIta+bTNpBM/2UT8hLOaEAFckO+m/qmR3luMIQygM=
|
||||||
|
github.com/xujiajun/mmap-go v1.0.1 h1:7Se7ss1fLPPRW+ePgqGpCkfGIZzJV6JPq9Wq9iv/WHc=
|
||||||
|
github.com/xujiajun/mmap-go v1.0.1/go.mod h1:CNN6Sw4SL69Sui00p0zEzcZKbt+5HtEnYUsc6BKKRMg=
|
||||||
|
github.com/xujiajun/nutsdb v0.5.0 h1:j/jM3Zw7Chg8WK7bAcKR0Xr7Mal47U1oJAMgySfDn9E=
|
||||||
|
github.com/xujiajun/nutsdb v0.5.0/go.mod h1:owdwN0tW084RxEodABLbO7h4Z2s9WiAjZGZFhRh0/1Q=
|
||||||
|
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b h1:jKG9OiL4T4xQN3IUrhUpc1tG+HfDXppkgVcrAiiaI/0=
|
||||||
|
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b/go.mod h1:AZd87GYJlUzl82Yab2kTjx1EyXSQCAfZDhpTo1SQC4k=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34=
|
||||||
|
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
|
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||||
|
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
||||||
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
|
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||||
|
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
||||||
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
|
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||||
|
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||||
|
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||||
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
|
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
||||||
|
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
143
main.go
Normal file
143
main.go
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/server"
|
||||||
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
easy "github.com/t-tomalak/logrus-easy-formatter"
|
||||||
|
"image/color"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFormatter(&easy.Formatter{
|
||||||
|
TimestampFormat: "2006-01-02 15:04:05",
|
||||||
|
LogFormat: "[%time%] [%lvl%]: %msg% \n",
|
||||||
|
})
|
||||||
|
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotatelogs.WithRotationTime(time.Hour*24))
|
||||||
|
if err == nil {
|
||||||
|
log.SetOutput(io.MultiWriter(os.Stderr, w))
|
||||||
|
}
|
||||||
|
if !global.PathExists("data") {
|
||||||
|
if err := os.Mkdir("data", 0777); err != nil {
|
||||||
|
log.Fatalf("创建数据文件夹失败: %v", err)
|
||||||
|
}
|
||||||
|
if err := os.Mkdir(path.Join("data", "images"), 0777); err != nil {
|
||||||
|
log.Fatalf("创建图片缓存文件夹失败: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if global.PathExists("cqhttp.json") {
|
||||||
|
log.Info("发现 cqhttp.json 将在五秒后尝试导入配置,按 Ctrl+C 取消.")
|
||||||
|
log.Warn("警告: 该操作会删除 cqhttp.json 并覆盖 config.json 文件.")
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
conf := global.CQHttpApiConfig{}
|
||||||
|
if err := json.Unmarshal([]byte(global.ReadAllText("cqhttp.json")), &conf); err != nil {
|
||||||
|
log.Fatalf("读取文件 cqhttp.json 失败: %v", err)
|
||||||
|
}
|
||||||
|
goConf := global.DefaultConfig()
|
||||||
|
goConf.AccessToken = conf.AccessToken
|
||||||
|
goConf.HttpConfig.Host = conf.Host
|
||||||
|
goConf.HttpConfig.Port = conf.Port
|
||||||
|
goConf.WSConfig.Host = conf.WSHost
|
||||||
|
goConf.WSConfig.Port = conf.WSPort
|
||||||
|
if err := goConf.Save("config.json"); err != nil {
|
||||||
|
log.Fatalf("保存 config.json 时出现错误: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
console := bufio.NewReader(os.Stdin)
|
||||||
|
conf := global.Load("config.json")
|
||||||
|
if conf == nil {
|
||||||
|
err := global.DefaultConfig().Save("config.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("创建默认配置文件时出现错误: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Infof("默认配置文件已生成, 请编辑 config.json 后重启程序.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if conf.Uin == 0 || conf.Password == "" {
|
||||||
|
log.Fatal("请修改 config.json 以添加账号密码.")
|
||||||
|
}
|
||||||
|
if !global.PathExists("device.json") {
|
||||||
|
log.Warn("虚拟设备信息不存在, 将自动生成随机设备,按 Enter 继续.")
|
||||||
|
_, _ = console.ReadString('\n')
|
||||||
|
client.GenRandomDevice()
|
||||||
|
_ = ioutil.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0777)
|
||||||
|
log.Info("已生成设备信息并保存到 device.json 文件.")
|
||||||
|
} else {
|
||||||
|
log.Info("将使用 device.json 内的设备信息运行Bot.")
|
||||||
|
if err := client.SystemDeviceInfo.ReadJson([]byte(global.ReadAllText("device.json"))); err != nil {
|
||||||
|
log.Fatalf("加载设备信息失败: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.")
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
log.Info("开始尝试登录并同步消息...")
|
||||||
|
cli := client.NewClient(conf.Uin, conf.Password)
|
||||||
|
rsp, err := cli.Login()
|
||||||
|
for {
|
||||||
|
global.Check(err)
|
||||||
|
if !rsp.Success {
|
||||||
|
switch rsp.Error {
|
||||||
|
case client.NeedCaptcha:
|
||||||
|
art, _, err := global.Convert(bytes.NewReader(rsp.CaptchaImage), []string{" ", "1", "i", ":", "*", "|", "."}, 1, 1, false, global.Colors["gray"], color.RGBA{})
|
||||||
|
global.Check(err)
|
||||||
|
fmt.Println(art)
|
||||||
|
log.Warn("请输入验证码.")
|
||||||
|
text, _ := console.ReadString('\n')
|
||||||
|
rsp, err = cli.SubmitCaptcha(strings.ReplaceAll(text, "\n", ""), rsp.CaptchaSign)
|
||||||
|
continue
|
||||||
|
case client.UnsafeDeviceError:
|
||||||
|
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证.", rsp.VerifyUrl)
|
||||||
|
log.Info("按 Enter 继续")
|
||||||
|
_, _ = console.ReadString('\n')
|
||||||
|
rsp, err = cli.Login()
|
||||||
|
continue
|
||||||
|
case client.OtherLoginError, client.UnknownLoginError:
|
||||||
|
log.Fatalf("登录失败: %v", rsp.ErrorMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Infof("登录成功 欢迎使用: %v", cli.Nickname)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
log.Info("开始加载好友列表...")
|
||||||
|
global.Check(cli.ReloadFriendList())
|
||||||
|
log.Infof("共加载 %v 个好友.", len(cli.FriendList))
|
||||||
|
log.Infof("开始加载群列表...")
|
||||||
|
global.Check(cli.ReloadGroupList())
|
||||||
|
log.Infof("共加载 %v 个群.", len(cli.GroupList))
|
||||||
|
b := coolq.NewQQBot(cli, conf)
|
||||||
|
if conf.HttpConfig != nil && conf.HttpConfig.Enabled {
|
||||||
|
server.HttpServer.Run(fmt.Sprintf("%s:%d", conf.HttpConfig.Host, conf.HttpConfig.Port), conf.AccessToken, b)
|
||||||
|
}
|
||||||
|
if conf.WSConfig != nil && conf.WSConfig.Enabled {
|
||||||
|
server.WebsocketServer.Run(fmt.Sprintf("%s:%d", conf.WSConfig.Host, conf.WSConfig.Port), conf.AccessToken, b)
|
||||||
|
}
|
||||||
|
log.Info("资源初始化完成, 开始处理信息.")
|
||||||
|
log.Info("アトリは、高性能ですから!")
|
||||||
|
cli.OnDisconnected(func(bot *client.QQClient, e *client.ClientDisconnectedEvent) {
|
||||||
|
b.Release()
|
||||||
|
log.Fatalf("Bot已断线:", e.Message)
|
||||||
|
})
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt, os.Kill)
|
||||||
|
<-c
|
||||||
|
b.Release()
|
||||||
|
}
|
313
server/http.go
Normal file
313
server/http.go
Normal file
@ -0,0 +1,313 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type httpServer struct {
|
||||||
|
engine *gin.Engine
|
||||||
|
bot *coolq.CQBot
|
||||||
|
}
|
||||||
|
|
||||||
|
var HttpServer = &httpServer{}
|
||||||
|
|
||||||
|
func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) {
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
s.engine = gin.New()
|
||||||
|
s.bot = bot
|
||||||
|
s.engine.Use(func(c *gin.Context) {
|
||||||
|
if c.Request.Method != "GET" && c.Request.Method != "POST" {
|
||||||
|
log.Warnf("已拒绝客户端 %v 的请求: 方法错误", c.Request.RemoteAddr)
|
||||||
|
c.Status(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if c.Request.Method == "POST" && c.Request.Header.Get("Content-Type") == "application/json" {
|
||||||
|
d, err := c.GetRawData()
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("获取请求 %v 的Body时出现错误: %v", c.Request.RequestURI, err)
|
||||||
|
c.Status(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !gjson.ValidBytes(d) {
|
||||||
|
log.Warnf("已拒绝客户端 %v 的请求: 非法Json", c.Request.RemoteAddr)
|
||||||
|
c.Status(400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Set("json_body", gjson.ParseBytes(d))
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
})
|
||||||
|
|
||||||
|
if authToken != "" {
|
||||||
|
s.engine.Use(func(c *gin.Context) {
|
||||||
|
if auth := c.Request.Header.Get("Authorization"); auth != "" {
|
||||||
|
if strings.SplitN(auth, " ", 2)[1] != authToken {
|
||||||
|
c.AbortWithStatus(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c.Query("access_token") != authToken {
|
||||||
|
c.AbortWithStatus(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
s.engine.Any("/get_login_info", s.GetLoginInfo)
|
||||||
|
s.engine.Any("/get_login_info_async", s.GetLoginInfo)
|
||||||
|
|
||||||
|
s.engine.Any("/get_friend_list", s.GetFriendList)
|
||||||
|
s.engine.Any("/get_friend_list_async", s.GetFriendList)
|
||||||
|
|
||||||
|
s.engine.Any("/get_group_list", s.GetGroupList)
|
||||||
|
s.engine.Any("/get_group_list_async", s.GetGroupList)
|
||||||
|
|
||||||
|
s.engine.Any("/get_group_info", s.GetGroupInfo)
|
||||||
|
s.engine.Any("/get_group_info_async", s.GetGroupInfo)
|
||||||
|
|
||||||
|
s.engine.Any("/get_group_member_list", s.GetGroupMemberList)
|
||||||
|
s.engine.Any("/get_group_member_list_async", s.GetGroupMemberList)
|
||||||
|
|
||||||
|
s.engine.Any("/get_group_member_info", s.GetGroupMemberInfo)
|
||||||
|
s.engine.Any("/get_group_member_info_async", s.GetGroupMemberInfo)
|
||||||
|
|
||||||
|
s.engine.Any("/send_msg", s.SendMessage)
|
||||||
|
s.engine.Any("/send_msg_async", s.SendMessage)
|
||||||
|
|
||||||
|
s.engine.Any("/send_private_msg", s.SendPrivateMessage)
|
||||||
|
s.engine.Any("/send_private_msg_async", s.SendPrivateMessage)
|
||||||
|
|
||||||
|
s.engine.Any("/send_group_msg", s.SendGroupMessage)
|
||||||
|
s.engine.Any("/send_group_msg_async", s.SendGroupMessage)
|
||||||
|
|
||||||
|
s.engine.Any("/delete_msg", s.DeleteMessage)
|
||||||
|
s.engine.Any("/delete_msg_async", s.DeleteMessage)
|
||||||
|
|
||||||
|
s.engine.Any("/set_friend_add_request", s.ProcessFriendRequest)
|
||||||
|
s.engine.Any("/set_friend_add_request_async", s.ProcessFriendRequest)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_add_request", s.ProcessGroupRequest)
|
||||||
|
s.engine.Any("/set_group_add_request_async", s.ProcessGroupRequest)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_card", s.SetGroupCard)
|
||||||
|
s.engine.Any("/set_group_card_async", s.SetGroupCard)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_special_title", s.SetSpecialTitle)
|
||||||
|
s.engine.Any("/set_group_special_title_async", s.SetSpecialTitle)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_kick", s.SetGroupKick)
|
||||||
|
s.engine.Any("/set_group_kick_async", s.SetGroupKick)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_ban", s.SetGroupBan)
|
||||||
|
s.engine.Any("/set_group_ban_async", s.SetGroupBan)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_whole_ban", s.SetWholeBan)
|
||||||
|
s.engine.Any("/set_group_whole_ban_async", s.SetWholeBan)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_name", s.SetGroupName)
|
||||||
|
s.engine.Any("/set_group_name_async", s.SetGroupName)
|
||||||
|
|
||||||
|
s.engine.Any("/get_image", s.GetImage)
|
||||||
|
s.engine.Any("/get_image_async", s.GetImage)
|
||||||
|
|
||||||
|
s.engine.Any("/get_group_msg", s.GetGroupMessage)
|
||||||
|
s.engine.Any("/get_group_msg_async", s.GetGroupMessage)
|
||||||
|
|
||||||
|
s.engine.Any("/can_send_image", s.CanSendImage)
|
||||||
|
s.engine.Any("/can_send_image_async", s.CanSendImage)
|
||||||
|
|
||||||
|
s.engine.Any("/can_send_record", s.CanSendRecord)
|
||||||
|
s.engine.Any("/can_send_record_async", s.CanSendRecord)
|
||||||
|
|
||||||
|
s.engine.Any("/get_status", s.GetStatus)
|
||||||
|
s.engine.Any("/get_status_async", s.GetStatus)
|
||||||
|
|
||||||
|
s.engine.Any("/get_version_info", s.GetVersionInfo)
|
||||||
|
s.engine.Any("/get_version_info_async", s.GetVersionInfo)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Infof("CQ HTTP 服务器已启动: %v", addr)
|
||||||
|
log.Fatal(s.engine.Run(addr))
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetLoginInfo(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQGetLoginInfo())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetFriendList(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQGetFriendList())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetGroupList(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQGetGroupList())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetGroupInfo(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQGetGroupInfo(gid))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetGroupMemberList(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQGetGroupMemberList(gid))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetGroupMemberInfo(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
|
nc := getParamOrDefault(c, "no_cache", "false")
|
||||||
|
c.JSON(200, s.bot.CQGetGroupMemberInfo(gid, uid, nc == "true"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SendMessage(c *gin.Context) {
|
||||||
|
if getParam(c, "group_id") != "" {
|
||||||
|
s.SendGroupMessage(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if getParam(c, "user_id") != "" {
|
||||||
|
s.SendPrivateMessage(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SendPrivateMessage(c *gin.Context) {
|
||||||
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
|
msg := getParam(c, "message")
|
||||||
|
c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SendGroupMessage(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
msg := getParam(c, "message")
|
||||||
|
c.JSON(200, s.bot.CQSendGroupMessage(gid, msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetImage(c *gin.Context) {
|
||||||
|
file := getParam(c, "file")
|
||||||
|
c.JSON(200, s.bot.CQGetImage(file))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetGroupMessage(c *gin.Context) {
|
||||||
|
mid, _ := strconv.ParseInt(getParam(c, "message_id"), 10, 32)
|
||||||
|
c.JSON(200, s.bot.CQGetGroupMessage(int32(mid)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) ProcessFriendRequest(c *gin.Context) {
|
||||||
|
flag := getParam(c, "flag")
|
||||||
|
approve := getParamOrDefault(c, "approve", "true")
|
||||||
|
c.JSON(200, s.bot.CQProcessFriendRequest(flag, approve == "true"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) ProcessGroupRequest(c *gin.Context) {
|
||||||
|
flag := getParam(c, "flag")
|
||||||
|
subType := getParam(c, "sub_type")
|
||||||
|
if subType == "" {
|
||||||
|
subType = getParam(c, "type")
|
||||||
|
}
|
||||||
|
approve := getParamOrDefault(c, "approve", "true")
|
||||||
|
c.JSON(200, s.bot.CQProcessGroupRequest(flag, subType, approve == "true"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetGroupCard(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQSetGroupCard(gid, uid, getParam(c, "card")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetSpecialTitle(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQSetGroupSpecialTitle(gid, uid, getParam(c, "special_title")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetGroupKick(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
|
msg := getParam(c, "message")
|
||||||
|
c.JSON(200, s.bot.CQSetGroupKick(gid, uid, msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetGroupBan(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
|
time, _ := strconv.ParseInt(getParam(c, "duration"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQSetGroupBan(gid, uid, uint32(time)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetWholeBan(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQSetGroupWholeBan(gid, getParamOrDefault(c, "enable", "true") == "true"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetGroupName(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQSetGroupName(gid, getParam(c, "name")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) DeleteMessage(c *gin.Context) {
|
||||||
|
mid, _ := strconv.ParseInt(getParam(c, "message_id"), 10, 32)
|
||||||
|
c.JSON(200, s.bot.CQDeleteMessage(int32(mid)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) CanSendImage(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQCanSendImage())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) CanSendRecord(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQCanSendRecord())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetStatus(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQGetStatus())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) GetVersionInfo(c *gin.Context) {
|
||||||
|
c.JSON(200, s.bot.CQGetVersionInfo())
|
||||||
|
}
|
||||||
|
|
||||||
|
func getParamOrDefault(c *gin.Context, k, def string) string {
|
||||||
|
r := getParam(c, k)
|
||||||
|
if r != "" {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
func getParam(c *gin.Context, k string) string {
|
||||||
|
if q := c.Query(k); q != "" {
|
||||||
|
return q
|
||||||
|
}
|
||||||
|
if c.Request.Method == "POST" {
|
||||||
|
if h := c.Request.Header.Get("Content-Type"); h != "" {
|
||||||
|
if h == "application/x-www-form-urlencoded" {
|
||||||
|
if p, ok := c.GetPostForm(k); ok {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h == "application/json" {
|
||||||
|
if obj, ok := c.Get("json_body"); ok {
|
||||||
|
res := obj.(gjson.Result).Get(k)
|
||||||
|
if res.Exists() {
|
||||||
|
switch res.Type {
|
||||||
|
case gjson.String:
|
||||||
|
return res.Str
|
||||||
|
case gjson.Number:
|
||||||
|
return strconv.FormatInt(res.Int(), 10) // 似乎没有需要接受 float 类型的api
|
||||||
|
case gjson.True:
|
||||||
|
return "true"
|
||||||
|
case gjson.False:
|
||||||
|
return "false"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
238
server/websocket.go
Normal file
238
server/websocket.go
Normal file
@ -0,0 +1,238 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type websocketServer struct {
|
||||||
|
bot *coolq.CQBot
|
||||||
|
token string
|
||||||
|
eventConn []*websocket.Conn
|
||||||
|
pushLock *sync.Mutex
|
||||||
|
handshake string
|
||||||
|
}
|
||||||
|
|
||||||
|
var WebsocketServer = &websocketServer{}
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *websocketServer) Run(addr, authToken string, b *coolq.CQBot) {
|
||||||
|
s.token = authToken
|
||||||
|
s.pushLock = new(sync.Mutex)
|
||||||
|
s.bot = b
|
||||||
|
s.handshake = fmt.Sprintf(`{"_post_method":2,"meta_event_type":"lifecycle","post_type":"meta_event","self_id":%d,"sub_type":"connect","time":%d}`,
|
||||||
|
s.bot.Client.Uin, time.Now().Unix())
|
||||||
|
b.OnEventPush(s.onBotPushEvent)
|
||||||
|
http.HandleFunc("/event", s.event)
|
||||||
|
http.HandleFunc("/api", s.api)
|
||||||
|
http.HandleFunc("/", s.any)
|
||||||
|
go func() {
|
||||||
|
log.Infof("CQ Websocket 服务器已启动: %v", addr)
|
||||||
|
log.Fatal(http.ListenAndServe(addr, nil))
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *websocketServer) event(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if s.token != "" {
|
||||||
|
if r.URL.Query().Get("access_token") != s.token && strings.SplitN(r.Header.Get("Authorization"), " ", 2)[1] != s.token {
|
||||||
|
log.Warnf("已拒绝 %v 的 Websocket 请求: Token错误", r.RemoteAddr)
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("处理 Websocket 请求时出现错误: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = c.WriteMessage(websocket.TextMessage, []byte(s.handshake))
|
||||||
|
if err == nil {
|
||||||
|
log.Infof("接受 Websocket 连接: %v (/event)", r.RemoteAddr)
|
||||||
|
s.eventConn = append(s.eventConn, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *websocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if s.token != "" {
|
||||||
|
if r.URL.Query().Get("access_token") != s.token && strings.SplitN(r.Header.Get("Authorization"), " ", 2)[1] != s.token {
|
||||||
|
log.Warnf("已拒绝 %v 的 Websocket 请求: Token错误", r.RemoteAddr)
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("处理 Websocket 请求时出现错误: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Infof("接受 Websocket 连接: %v (/api)", r.RemoteAddr)
|
||||||
|
go s.listenApi(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *websocketServer) any(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if s.token != "" {
|
||||||
|
if r.URL.Query().Get("access_token") != s.token && strings.SplitN(r.Header.Get("Authorization"), " ", 2)[1] != s.token {
|
||||||
|
log.Warnf("已拒绝 %v 的 Websocket 请求: Token错误", r.RemoteAddr)
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("处理 Websocket 请求时出现错误: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = c.WriteMessage(websocket.TextMessage, []byte(s.handshake))
|
||||||
|
if err == nil {
|
||||||
|
log.Infof("接受 Websocket 连接: %v (/)", r.RemoteAddr)
|
||||||
|
s.eventConn = append(s.eventConn, c)
|
||||||
|
s.listenApi(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *websocketServer) listenApi(c *websocket.Conn) {
|
||||||
|
defer c.Close()
|
||||||
|
for {
|
||||||
|
t, payload, err := c.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if t == websocket.TextMessage {
|
||||||
|
j := gjson.ParseBytes(payload)
|
||||||
|
t := strings.ReplaceAll(j.Get("action").Str, "_async", "") //TODO: async support
|
||||||
|
log.Infof("API调用: %v", j.Get("action").Str)
|
||||||
|
if f, ok := wsApi[t]; ok {
|
||||||
|
_ = c.WriteMessage(websocket.TextMessage, []byte(f(s.bot, j.Get("params"))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *websocketServer) onBotPushEvent(m string) {
|
||||||
|
s.pushLock.Lock()
|
||||||
|
defer s.pushLock.Unlock()
|
||||||
|
pos := 0
|
||||||
|
for _, conn := range s.eventConn {
|
||||||
|
err := conn.WriteMessage(websocket.TextMessage, []byte(m))
|
||||||
|
if err != nil {
|
||||||
|
_ = conn.Close()
|
||||||
|
s.eventConn = append(s.eventConn[:pos], s.eventConn[pos+1:]...)
|
||||||
|
if pos > 0 {
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wsApi = map[string]func(*coolq.CQBot, gjson.Result) string{
|
||||||
|
"get_login_info": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetLoginInfo().ToJson()
|
||||||
|
},
|
||||||
|
"get_friend_list": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetFriendList().ToJson()
|
||||||
|
},
|
||||||
|
"get_group_list": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetGroupList().ToJson()
|
||||||
|
},
|
||||||
|
"get_group_info": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetGroupInfo(p.Get("group_id").Int()).ToJson()
|
||||||
|
},
|
||||||
|
"get_group_member_list": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetGroupMemberList(p.Get("group_id").Int()).ToJson()
|
||||||
|
},
|
||||||
|
"get_group_member_info": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetGroupMemberInfo(
|
||||||
|
p.Get("group_id").Int(), p.Get("user_id").Int(),
|
||||||
|
p.Get("no_cache").Bool(),
|
||||||
|
).ToJson()
|
||||||
|
},
|
||||||
|
"send_msg": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
if p.Get("group_id").Int() != 0 {
|
||||||
|
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message").Str).ToJson()
|
||||||
|
}
|
||||||
|
if p.Get("user_id").Int() != 0 {
|
||||||
|
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message").Str).ToJson()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
},
|
||||||
|
"send_group_msg": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message").Str).ToJson()
|
||||||
|
},
|
||||||
|
"send_private_msg": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message").Str).ToJson()
|
||||||
|
},
|
||||||
|
"delete_msg": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQDeleteMessage(int32(p.Get("message_id").Int())).ToJson()
|
||||||
|
},
|
||||||
|
"set_friend_add_request": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
apr := true
|
||||||
|
if p.Get("approve").Exists() {
|
||||||
|
apr = p.Get("approve").Bool()
|
||||||
|
}
|
||||||
|
return bot.CQProcessFriendRequest(p.Get("flag").Str, apr).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_add_request": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
subType := p.Get("sub_type").Str
|
||||||
|
apr := true
|
||||||
|
if subType == "" {
|
||||||
|
subType = p.Get("type").Str
|
||||||
|
}
|
||||||
|
if p.Get("approve").Exists() {
|
||||||
|
apr = p.Get("approve").Bool()
|
||||||
|
}
|
||||||
|
return bot.CQProcessGroupRequest(p.Get("flag").Str, subType, apr).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_card": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSetGroupCard(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("card").Str).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_special_title": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSetGroupSpecialTitle(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("special_title").Str).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_kick": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSetGroupKick(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("message").Str).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_ban": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSetGroupBan(p.Get("group_id").Int(), p.Get("user_id").Int(), uint32(p.Get("duration").Int())).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_whole_ban": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSetGroupWholeBan(p.Get("group_id").Int(), func() bool {
|
||||||
|
if p.Get("enable").Exists() {
|
||||||
|
return p.Get("enable").Bool()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}()).ToJson()
|
||||||
|
},
|
||||||
|
"set_group_name": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQSetGroupName(p.Get("group_id").Int(), p.Get("name").Str).ToJson()
|
||||||
|
},
|
||||||
|
"get_image": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetImage(p.Get("file").Str).ToJson()
|
||||||
|
},
|
||||||
|
"get_group_msg": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetGroupMessage(int32(p.Get("message_id").Int())).ToJson()
|
||||||
|
},
|
||||||
|
"can_send_image": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQCanSendImage().ToJson()
|
||||||
|
},
|
||||||
|
"can_send_record": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQCanSendRecord().ToJson()
|
||||||
|
},
|
||||||
|
"get_status": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetStatus().ToJson()
|
||||||
|
},
|
||||||
|
"get_version_info": func(bot *coolq.CQBot, p gjson.Result) string {
|
||||||
|
return bot.CQGetVersionInfo().ToJson()
|
||||||
|
},
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user