Skip to content

Commit

Permalink
style: move coolq.MSG to global package
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrs4s committed Sep 17, 2021
1 parent 6d19f07 commit efdd6bd
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 253 deletions.
234 changes: 117 additions & 117 deletions coolq/api.go

Large diffs are not rendered by default.

31 changes: 14 additions & 17 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ type CQBot struct {
tempSessionCache sync.Map
}

// MSG 消息Map
type MSG map[string]interface{}

// Event 事件
type Event struct {
RawMsg MSG
RawMsg global.MSG

once sync.Once
buffer *bytes.Buffer
Expand Down Expand Up @@ -165,7 +162,7 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
t := time.NewTicker(time.Second * time.Duration(i))
for {
<-t.C
bot.dispatchEventMessage(MSG{
bot.dispatchEventMessage(global.MSG{
"time": time.Now().Unix(),
"self_id": bot.Client.Uin,
"post_type": "meta_event",
Expand All @@ -186,9 +183,9 @@ func (bot *CQBot) OnEventPush(f func(e *Event)) {
}

// GetMessage 获取给定消息id对应的消息
func (bot *CQBot) GetMessage(mid int32) MSG {
func (bot *CQBot) GetMessage(mid int32) global.MSG {
if bot.db != nil {
m := MSG{}
m := global.MSG{}
data, err := bot.db.Get(binary.ToBytes(mid), nil)
if err == nil {
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&m)
Expand Down Expand Up @@ -400,7 +397,7 @@ func (bot *CQBot) SendPrivateMessage(target int64, groupID int64, m *message.Sen

// InsertGroupMessage 群聊消息入数据库
func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
val := MSG{
val := global.MSG{
"message-id": m.Id,
"internal-id": m.InternalId,
"group": m.GroupCode,
Expand All @@ -427,7 +424,7 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {

// InsertPrivateMessage 私聊消息入数据库
func (bot *CQBot) InsertPrivateMessage(m *message.PrivateMessage) int32 {
val := MSG{
val := global.MSG{
"message-id": m.Id,
"internal-id": m.InternalId,
"target": m.Target,
Expand All @@ -453,7 +450,7 @@ func (bot *CQBot) InsertPrivateMessage(m *message.PrivateMessage) int32 {

// InsertTempMessage 临时消息入数据库
func (bot *CQBot) InsertTempMessage(target int64, m *message.TempMessage) int32 {
val := MSG{
val := global.MSG{
"message-id": m.Id,
// FIXME(InsertTempMessage) InternalId missing
"from-group": m.GroupCode,
Expand Down Expand Up @@ -491,7 +488,7 @@ func (bot *CQBot) Release() {
}
}

func (bot *CQBot) dispatchEventMessage(m MSG) {
func (bot *CQBot) dispatchEventMessage(m global.MSG) {
bot.lock.RLock()
defer bot.lock.RUnlock()

Expand Down Expand Up @@ -519,9 +516,9 @@ func (bot *CQBot) dispatchEventMessage(m MSG) {
global.PutBuffer(event.buffer)
}

func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) global.MSG {
cqm := ToStringMessage(m.Elements, m.GroupCode, true)
gm := MSG{
gm := global.MSG{
"anonymous": nil,
"font": 0,
"group_id": m.GroupCode,
Expand All @@ -536,7 +533,7 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
}(),
"raw_message": cqm,
"self_id": bot.Client.Uin,
"sender": MSG{
"sender": global.MSG{
"age": 0,
"area": "",
"level": "",
Expand All @@ -548,12 +545,12 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
"user_id": m.Sender.Uin,
}
if m.Sender.IsAnonymous() {
gm["anonymous"] = MSG{
gm["anonymous"] = global.MSG{
"flag": m.Sender.AnonymousInfo.AnonymousId + "|" + m.Sender.AnonymousInfo.AnonymousNick,
"id": m.Sender.Uin,
"name": m.Sender.AnonymousInfo.AnonymousNick,
}
gm["sender"].(MSG)["nickname"] = "匿名消息"
gm["sender"].(global.MSG)["nickname"] = "匿名消息"
gm["sub_type"] = "anonymous"
} else {
group := bot.Client.FindGroup(m.GroupCode)
Expand All @@ -571,7 +568,7 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
return nil
}
}
ms := gm["sender"].(MSG)
ms := gm["sender"].(global.MSG)
switch mem.Permission {
case client.Owner:
ms["role"] = "owner"
Expand Down
40 changes: 20 additions & 20 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (e *PokeElement) Type() message.ElementType {
}

// ToArrayMessage 将消息元素数组转为MSG数组以用于消息上报
func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
r = make([]MSG, 0, len(e))
func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []global.MSG) {
r = make([]global.MSG, 0, len(e))
m := &message.SendingMessage{Elements: e}
reply := m.FirstOrNil(func(e message.IMessageElement) bool {
_, ok := e.(*message.ReplyElement)
Expand All @@ -138,7 +138,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
rid = replyElem.Sender
}
if ExtraReplyData {
r = append(r, MSG{
r = append(r, global.MSG{
"type": "reply",
"data": map[string]string{
"id": strconv.FormatInt(int64(toGlobalID(rid, replyElem.ReplySeq)), 10),
Expand All @@ -149,14 +149,14 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
},
})
} else {
r = append(r, MSG{
r = append(r, global.MSG{
"type": "reply",
"data": map[string]string{"id": strconv.FormatInt(int64(toGlobalID(rid, replyElem.ReplySeq)), 10)},
})
}
}
for i, elem := range e {
var m MSG
var m global.MSG
switch o := elem.(type) {
case *message.ReplyElement:
if RemoveReplyAt && i+1 < len(e) {
Expand All @@ -166,49 +166,49 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
}
}
case *message.TextElement:
m = MSG{
m = global.MSG{
"type": "text",
"data": map[string]string{"text": o.Content},
}
case *message.LightAppElement:
m = MSG{
m = global.MSG{
"type": "json",
"data": map[string]string{"data": o.Content},
}
case *message.AtElement:
if o.Target == 0 {
m = MSG{
m = global.MSG{
"type": "at",
"data": map[string]string{"qq": "all"},
}
} else {
m = MSG{
m = global.MSG{
"type": "at",
"data": map[string]string{"qq": strconv.FormatInt(o.Target, 10)},
}
}
case *message.RedBagElement:
m = MSG{
m = global.MSG{
"type": "redbag",
"data": map[string]string{"title": o.Title},
}
case *message.ForwardElement:
m = MSG{
m = global.MSG{
"type": "forward",
"data": map[string]string{"id": o.ResId},
}
case *message.FaceElement:
m = MSG{
m = global.MSG{
"type": "face",
"data": map[string]string{"id": strconv.FormatInt(int64(o.Index), 10)},
}
case *message.VoiceElement:
m = MSG{
m = global.MSG{
"type": "record",
"data": map[string]string{"file": o.Name, "url": o.Url},
}
case *message.ShortVideoElement:
m = MSG{
m = global.MSG{
"type": "video",
"data": map[string]string{"file": o.Name, "url": o.Url},
}
Expand All @@ -221,7 +221,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
data["type"] = "show"
data["id"] = strconv.FormatInt(int64(o.EffectID), 10)
}
m = MSG{
m = global.MSG{
"type": "image",
"data": data,
}
Expand All @@ -230,18 +230,18 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
if o.Flash {
data["type"] = "flash"
}
m = MSG{
m = global.MSG{
"type": "image",
"data": data,
}
case *message.ServiceElement:
if isOk := strings.Contains(o.Content, "<?xml"); isOk {
m = MSG{
m = global.MSG{
"type": "xml",
"data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
}
} else {
m = MSG{
m = global.MSG{
"type": "json",
"data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
}
Expand Down Expand Up @@ -379,7 +379,7 @@ func (bot *CQBot) ConvertStringMessage(raw string, isGroup bool) (r []message.IM
switch {
case customText != "":
var elem *message.ReplyElement
var org MSG
var org global.MSG
sender, senderErr := strconv.ParseInt(d["qq"], 10, 64)
if senderErr != nil && err != nil {
log.Warnf("警告: 自定义 Reply 元素中必须包含 Uin 或 id")
Expand Down Expand Up @@ -551,7 +551,7 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag
switch {
case customText != "":
var elem *message.ReplyElement
var org MSG
var org global.MSG
sender, senderErr := strconv.ParseInt(e.Get("data.[user_id,qq]").String(), 10, 64)
if senderErr != nil && err != nil {
log.Warnf("警告: 自定义 Reply 元素中必须包含 user_id 或 id")
Expand Down
Loading

0 comments on commit efdd6bd

Please sign in to comment.