mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 12:03:50 +08:00
coolq: unify converting IMessage to string&array message
This commit is contained in:
parent
810c781c25
commit
296668441f
@ -1577,7 +1577,7 @@ func (bot *CQBot) CQGetMessage(messageID int32) global.MSG {
|
|||||||
// @route(get_guild_msg)
|
// @route(get_guild_msg)
|
||||||
func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG {
|
func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG {
|
||||||
source, seq := decodeGuildMessageID(messageID)
|
source, seq := decodeGuildMessageID(messageID)
|
||||||
if source == nil {
|
if source.SourceType == 0 {
|
||||||
log.Warnf("获取消息时出现错误: 无效消息ID")
|
log.Warnf("获取消息时出现错误: 无效消息ID")
|
||||||
return Failed(100, "INVALID_MESSAGE_ID", "无效消息ID")
|
return Failed(100, "INVALID_MESSAGE_ID", "无效消息ID")
|
||||||
}
|
}
|
||||||
@ -1613,7 +1613,7 @@ func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG {
|
|||||||
"tiny_id": fU64(pull[0].Sender.TinyId),
|
"tiny_id": fU64(pull[0].Sender.TinyId),
|
||||||
"nickname": pull[0].Sender.Nickname,
|
"nickname": pull[0].Sender.Nickname,
|
||||||
}
|
}
|
||||||
m["message"] = ToFormattedMessage(pull[0].Elements, *source, false)
|
m["message"] = ToFormattedMessage(pull[0].Elements, source, false)
|
||||||
m["reactions"] = convertReactions(pull[0].Reactions)
|
m["reactions"] = convertReactions(pull[0].Reactions)
|
||||||
bot.InsertGuildChannelMessage(pull[0])
|
bot.InsertGuildChannelMessage(pull[0])
|
||||||
} else {
|
} else {
|
||||||
@ -1628,7 +1628,7 @@ func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG {
|
|||||||
"tiny_id": fU64(channelMsgByDB.Attribute.SenderTinyID),
|
"tiny_id": fU64(channelMsgByDB.Attribute.SenderTinyID),
|
||||||
"nickname": channelMsgByDB.Attribute.SenderName,
|
"nickname": channelMsgByDB.Attribute.SenderName,
|
||||||
}
|
}
|
||||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(channelMsgByDB.Content, message.SourceGuildChannel), *source)
|
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(channelMsgByDB.Content, message.SourceGuildChannel), source, false)
|
||||||
}
|
}
|
||||||
case message.SourceGuildDirect:
|
case message.SourceGuildDirect:
|
||||||
// todo(mrs4s): 支持 direct 消息
|
// todo(mrs4s): 支持 direct 消息
|
||||||
|
@ -630,13 +630,13 @@ func encodeGuildMessageID(primaryID, subID, seq uint64, source message.SourceTyp
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeGuildMessageID(id string) (source *message.Source, seq uint64) {
|
func decodeGuildMessageID(id string) (source message.Source, seq uint64) {
|
||||||
b, _ := base64.StdEncoding.DecodeString(id)
|
b, _ := base64.StdEncoding.DecodeString(id)
|
||||||
if len(b) < 25 {
|
if len(b) < 25 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r := binary.NewReader(b)
|
r := binary.NewReader(b)
|
||||||
source = &message.Source{
|
source = message.Source{
|
||||||
SourceType: message.SourceType(r.ReadByte()),
|
SourceType: message.SourceType(r.ReadByte()),
|
||||||
PrimaryID: r.ReadInt64(),
|
PrimaryID: r.ReadInt64(),
|
||||||
SecondaryID: r.ReadInt64(),
|
SecondaryID: r.ReadInt64(),
|
||||||
|
@ -2,6 +2,7 @@ package coolq
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/Mrs4s/MiraiGo/topic"
|
"github.com/Mrs4s/MiraiGo/topic"
|
||||||
|
|
||||||
@ -64,7 +65,7 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) global.MSG {
|
|||||||
SourceType: message.SourceGroup,
|
SourceType: message.SourceGroup,
|
||||||
PrimaryID: m.GroupCode,
|
PrimaryID: m.GroupCode,
|
||||||
}
|
}
|
||||||
cqm := ToStringMessage(m.Elements, source, true)
|
cqm := toStringMessage(m.Elements, source, true)
|
||||||
postType := "message"
|
postType := "message"
|
||||||
if m.Sender.Uin == bot.Client.Uin {
|
if m.Sender.Uin == bot.Client.Uin {
|
||||||
postType = "message_sent"
|
postType = "message_sent"
|
||||||
@ -211,6 +212,15 @@ func convertReactions(reactions []*message.GuildMessageEmojiReaction) (r []globa
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toStringMessage(m []message.IMessageElement, source message.Source, raw bool) string {
|
||||||
|
elems := toElements(m, source, raw)
|
||||||
|
var sb strings.Builder
|
||||||
|
for _, elem := range elems {
|
||||||
|
sb.WriteString(elem.CQCode())
|
||||||
|
}
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
func fU64(v uint64) string {
|
func fU64(v uint64) string {
|
||||||
return strconv.FormatUint(v, 10)
|
return strconv.FormatUint(v, 10)
|
||||||
}
|
}
|
||||||
|
320
coolq/cqcode.go
320
coolq/cqcode.go
@ -92,9 +92,12 @@ func replyID(r *message.ReplyElement, source message.Source) int32 {
|
|||||||
return db.ToGlobalID(id, seq)
|
return db.ToGlobalID(id, seq)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToArrayMessage 将消息元素数组转为MSG数组以用于消息上报
|
// toElements 将消息元素数组转为MSG数组以用于消息上报
|
||||||
func ToArrayMessage(e []message.IMessageElement, source message.Source) (r []global.MSG) {
|
func toElements(e []message.IMessageElement, source message.Source, raw bool) (r []cqcode.Element) {
|
||||||
r = make([]global.MSG, 0, len(e))
|
type pair = cqcode.Pair // simplify code
|
||||||
|
type pairs = []pair
|
||||||
|
|
||||||
|
r = make([]cqcode.Element, 0, len(e))
|
||||||
m := &message.SendingMessage{Elements: e}
|
m := &message.SendingMessage{Elements: e}
|
||||||
reply := m.FirstOrNil(func(e message.IMessageElement) bool {
|
reply := m.FirstOrNil(func(e message.IMessageElement) bool {
|
||||||
_, ok := e.(*message.ReplyElement)
|
_, ok := e.(*message.ReplyElement)
|
||||||
@ -103,26 +106,24 @@ func ToArrayMessage(e []message.IMessageElement, source message.Source) (r []glo
|
|||||||
if reply != nil && source.SourceType&(message.SourceGroup|message.SourcePrivate) != 0 {
|
if reply != nil && source.SourceType&(message.SourceGroup|message.SourcePrivate) != 0 {
|
||||||
replyElem := reply.(*message.ReplyElement)
|
replyElem := reply.(*message.ReplyElement)
|
||||||
id := replyID(replyElem, source)
|
id := replyID(replyElem, source)
|
||||||
if base.ExtraReplyData {
|
elem := cqcode.Element{
|
||||||
r = append(r, global.MSG{
|
Type: "reply",
|
||||||
"type": "reply",
|
Data: pairs{
|
||||||
"data": map[string]string{
|
{"id", strconv.FormatInt(int64(id), 10)},
|
||||||
"id": strconv.FormatInt(int64(id), 10),
|
|
||||||
"seq": strconv.FormatInt(int64(replyElem.ReplySeq), 10),
|
|
||||||
"qq": strconv.FormatInt(replyElem.Sender, 10),
|
|
||||||
"time": strconv.FormatInt(int64(replyElem.Time), 10),
|
|
||||||
"text": ToStringMessage(replyElem.Elements, source),
|
|
||||||
},
|
},
|
||||||
})
|
|
||||||
} else {
|
|
||||||
r = append(r, global.MSG{
|
|
||||||
"type": "reply",
|
|
||||||
"data": map[string]string{"id": strconv.FormatInt(int64(id), 10)},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
if base.ExtraReplyData {
|
||||||
|
elem.Data = append(elem.Data,
|
||||||
|
pair{K: "seq", V: strconv.FormatInt(int64(replyElem.ReplySeq), 10)},
|
||||||
|
pair{K: "qq", V: strconv.FormatInt(replyElem.Sender, 10)},
|
||||||
|
pair{K: "time", V: strconv.FormatInt(int64(replyElem.Time), 10)},
|
||||||
|
pair{K: "text", V: toStringMessage(replyElem.Elements, source, true)},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
r = append(r, elem)
|
||||||
}
|
}
|
||||||
for i, elem := range e {
|
for i, elem := range e {
|
||||||
var m global.MSG
|
var m cqcode.Element
|
||||||
switch o := elem.(type) {
|
switch o := elem.(type) {
|
||||||
case *message.ReplyElement:
|
case *message.ReplyElement:
|
||||||
if base.RemoveReplyAt && i+1 < len(e) {
|
if base.RemoveReplyAt && i+1 < len(e) {
|
||||||
@ -133,103 +134,143 @@ func ToArrayMessage(e []message.IMessageElement, source message.Source) (r []glo
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
case *message.TextElement:
|
case *message.TextElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "text",
|
Type: "text",
|
||||||
"data": map[string]string{"text": o.Content},
|
Data: pairs{
|
||||||
|
{"text", o.Content},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.LightAppElement:
|
case *message.LightAppElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "json",
|
Type: "json",
|
||||||
"data": map[string]string{"data": o.Content},
|
Data: pairs{
|
||||||
|
{"data", o.Content},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.AtElement:
|
case *message.AtElement:
|
||||||
target := "all"
|
target := "all"
|
||||||
if o.Target != 0 {
|
if o.Target != 0 {
|
||||||
target = strconv.FormatUint(uint64(o.Target), 10)
|
target = strconv.FormatUint(uint64(o.Target), 10)
|
||||||
}
|
}
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "at",
|
Type: "at",
|
||||||
"data": map[string]string{"qq": target},
|
Data: pairs{
|
||||||
|
{"qq", target},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.RedBagElement:
|
case *message.RedBagElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "redbag",
|
Type: "redbag",
|
||||||
"data": map[string]string{"title": o.Title},
|
Data: pairs{
|
||||||
|
{"title", o.Title},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.ForwardElement:
|
case *message.ForwardElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "forward",
|
Type: "forward",
|
||||||
"data": map[string]string{"id": o.ResId},
|
Data: pairs{
|
||||||
|
{"id", o.ResId},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.FaceElement:
|
case *message.FaceElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "face",
|
Type: "face",
|
||||||
"data": map[string]string{"id": strconv.FormatInt(int64(o.Index), 10)},
|
Data: pairs{
|
||||||
|
{"id", strconv.FormatInt(int64(o.Index), 10)},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.VoiceElement:
|
case *message.VoiceElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "record",
|
Type: "record",
|
||||||
"data": map[string]string{"file": o.Name, "url": o.Url},
|
Data: pairs{
|
||||||
|
{"file", o.Name},
|
||||||
|
{"url", o.Url},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.ShortVideoElement:
|
case *message.ShortVideoElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "video",
|
Type: "video",
|
||||||
"data": map[string]string{"file": o.Name, "url": o.Url},
|
Data: pairs{
|
||||||
|
{"file", o.Name},
|
||||||
|
{"url", o.Url},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.GroupImageElement:
|
case *message.GroupImageElement:
|
||||||
data := map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url, "subType": strconv.FormatInt(int64(o.ImageBizType), 10)}
|
data := pairs{
|
||||||
|
{"file", hex.EncodeToString(o.Md5) + ".image"},
|
||||||
|
{"subType", strconv.FormatInt(int64(o.ImageBizType), 10)},
|
||||||
|
}
|
||||||
|
if raw {
|
||||||
|
data = append(data, pair{K: "url", V: o.Url})
|
||||||
|
}
|
||||||
switch {
|
switch {
|
||||||
case o.Flash:
|
case o.Flash:
|
||||||
data["type"] = "flash"
|
data = append(data, pair{K: "type", V: "flash"})
|
||||||
case o.EffectID != 0:
|
case o.EffectID != 0:
|
||||||
data["type"] = "show"
|
data = append(data, pair{K: "type", V: "show"})
|
||||||
data["id"] = strconv.FormatInt(int64(o.EffectID), 10)
|
data = append(data, pair{K: "id", V: strconv.FormatInt(int64(o.EffectID), 10)})
|
||||||
}
|
}
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "image",
|
Type: "image",
|
||||||
"data": data,
|
Data: data,
|
||||||
}
|
}
|
||||||
case *message.GuildImageElement:
|
case *message.GuildImageElement:
|
||||||
data := map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}
|
data := pairs{
|
||||||
m = global.MSG{
|
{"file", hex.EncodeToString(o.Md5) + ".image"},
|
||||||
"type": "image",
|
}
|
||||||
"data": data,
|
if raw {
|
||||||
|
data = append(data, pair{K: "url", V: o.Url})
|
||||||
|
}
|
||||||
|
m = cqcode.Element{
|
||||||
|
Type: "image",
|
||||||
|
Data: data,
|
||||||
}
|
}
|
||||||
case *message.FriendImageElement:
|
case *message.FriendImageElement:
|
||||||
data := map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}
|
data := pairs{
|
||||||
if o.Flash {
|
{"file", hex.EncodeToString(o.Md5) + ".image"},
|
||||||
data["type"] = "flash"
|
|
||||||
}
|
}
|
||||||
m = global.MSG{
|
if raw {
|
||||||
"type": "image",
|
data = append(data, pair{K: "url", V: o.Url})
|
||||||
"data": data,
|
}
|
||||||
|
if o.Flash {
|
||||||
|
data = append(data, pair{K: "type", V: "flash"})
|
||||||
|
}
|
||||||
|
m = cqcode.Element{
|
||||||
|
Type: "image",
|
||||||
|
Data: data,
|
||||||
}
|
}
|
||||||
case *message.DiceElement:
|
case *message.DiceElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "dice",
|
Type: "dice",
|
||||||
"data": map[string]string{"value": strconv.FormatInt(int64(o.Value), 10)},
|
Data: pairs{
|
||||||
|
{"value", strconv.FormatInt(int64(o.Value), 10)},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.MarketFaceElement:
|
case *message.MarketFaceElement:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "text",
|
Type: "text",
|
||||||
"data": map[string]string{"text": o.Name},
|
Data: pairs{
|
||||||
|
{"text", o.Name},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
case *message.ServiceElement:
|
case *message.ServiceElement:
|
||||||
if isOk := strings.Contains(o.Content, "<?xml"); isOk {
|
m = cqcode.Element{
|
||||||
m = global.MSG{
|
Type: "xml",
|
||||||
"type": "xml",
|
Data: pairs{
|
||||||
"data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
|
{"data", o.Content},
|
||||||
}
|
{"resid", o.ResId},
|
||||||
} else {
|
},
|
||||||
m = global.MSG{
|
|
||||||
"type": "json",
|
|
||||||
"data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
|
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(o.Content, "<?xml") {
|
||||||
|
m.Type = "json"
|
||||||
}
|
}
|
||||||
case *message.AnimatedSticker:
|
case *message.AnimatedSticker:
|
||||||
m = global.MSG{
|
m = cqcode.Element{
|
||||||
"type": "face",
|
Type: "face",
|
||||||
"data": map[string]string{"id": strconv.FormatInt(int64(o.ID), 10), "type": "sticker"},
|
Data: pairs{
|
||||||
|
{"id", strconv.FormatInt(int64(o.ID), 10)},
|
||||||
|
{"type", "sticker"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
continue
|
continue
|
||||||
@ -239,129 +280,6 @@ func ToArrayMessage(e []message.IMessageElement, source message.Source) (r []glo
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToStringMessage 将消息元素数组转为字符串以用于消息上报
|
|
||||||
func ToStringMessage(e []message.IMessageElement, source message.Source, isRaw ...bool) (r string) {
|
|
||||||
sb := global.NewBuffer()
|
|
||||||
sb.Reset()
|
|
||||||
write := func(format string, a ...interface{}) {
|
|
||||||
_, _ = fmt.Fprintf(sb, format, a...)
|
|
||||||
}
|
|
||||||
ur := false
|
|
||||||
if len(isRaw) != 0 {
|
|
||||||
ur = isRaw[0]
|
|
||||||
}
|
|
||||||
// 方便
|
|
||||||
m := &message.SendingMessage{Elements: e}
|
|
||||||
reply := m.FirstOrNil(func(e message.IMessageElement) bool {
|
|
||||||
_, ok := e.(*message.ReplyElement)
|
|
||||||
return ok
|
|
||||||
})
|
|
||||||
if reply != nil && source.SourceType&(message.SourceGroup|message.SourcePrivate) != 0 {
|
|
||||||
replyElem := reply.(*message.ReplyElement)
|
|
||||||
id := replyID(replyElem, source)
|
|
||||||
if base.ExtraReplyData {
|
|
||||||
write("[CQ:reply,id=%d,seq=%d,qq=%d,time=%d,text=%s]",
|
|
||||||
id, replyElem.ReplySeq, replyElem.Sender, replyElem.Time,
|
|
||||||
cqcode.EscapeValue(ToStringMessage(replyElem.Elements, source)))
|
|
||||||
} else {
|
|
||||||
write("[CQ:reply,id=%d]", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for i, elem := range e {
|
|
||||||
switch o := elem.(type) {
|
|
||||||
case *message.ReplyElement:
|
|
||||||
if base.RemoveReplyAt && len(e) > i+1 {
|
|
||||||
elem, ok := e[i+1].(*message.AtElement)
|
|
||||||
if ok && elem.Target == o.Sender {
|
|
||||||
e[i+1] = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *message.TextElement:
|
|
||||||
sb.WriteString(cqcode.EscapeText(o.Content))
|
|
||||||
case *message.AtElement:
|
|
||||||
if o.Target == 0 {
|
|
||||||
write("[CQ:at,qq=all]")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
write("[CQ:at,qq=%d]", uint64(o.Target))
|
|
||||||
case *message.RedBagElement:
|
|
||||||
write("[CQ:redbag,title=%s]", o.Title)
|
|
||||||
case *message.ForwardElement:
|
|
||||||
write("[CQ:forward,id=%s]", o.ResId)
|
|
||||||
case *message.FaceElement:
|
|
||||||
write(`[CQ:face,id=%d]`, o.Index)
|
|
||||||
case *message.VoiceElement:
|
|
||||||
if ur {
|
|
||||||
write(`[CQ:record,file=%s]`, o.Name)
|
|
||||||
} else {
|
|
||||||
write(`[CQ:record,file=%s,url=%s]`, o.Name, cqcode.EscapeValue(o.Url))
|
|
||||||
}
|
|
||||||
case *message.ShortVideoElement:
|
|
||||||
if ur {
|
|
||||||
write(`[CQ:video,file=%s]`, o.Name)
|
|
||||||
} else {
|
|
||||||
write(`[CQ:video,file=%s,url=%s]`, o.Name, cqcode.EscapeValue(o.Url))
|
|
||||||
}
|
|
||||||
case *message.GroupImageElement:
|
|
||||||
var arg string
|
|
||||||
if o.Flash {
|
|
||||||
arg = ",type=flash"
|
|
||||||
} else if o.EffectID != 0 {
|
|
||||||
arg = ",type=show,id=" + strconv.FormatInt(int64(o.EffectID), 10)
|
|
||||||
}
|
|
||||||
arg += ",subType=" + strconv.FormatInt(int64(o.ImageBizType), 10)
|
|
||||||
if ur {
|
|
||||||
write("[CQ:image,file=%x.image%s]", o.Md5, arg)
|
|
||||||
} else {
|
|
||||||
write("[CQ:image,file=%x.image,url=%s%s]", o.Md5, cqcode.EscapeValue(o.Url), arg)
|
|
||||||
}
|
|
||||||
case *message.FriendImageElement:
|
|
||||||
var arg string
|
|
||||||
if o.Flash {
|
|
||||||
arg = ",type=flash"
|
|
||||||
}
|
|
||||||
if ur {
|
|
||||||
write("[CQ:image,file=%x.image%s]", o.Md5, arg)
|
|
||||||
} else {
|
|
||||||
write("[CQ:image,file=%x.image,url=%s%s]", cqcode.EscapeValue(o.Url), arg)
|
|
||||||
}
|
|
||||||
case *LocalImageElement:
|
|
||||||
var arg string
|
|
||||||
if o.Flash {
|
|
||||||
arg = ",type=flash"
|
|
||||||
}
|
|
||||||
data, err := os.ReadFile(o.File)
|
|
||||||
if err == nil {
|
|
||||||
m := md5.Sum(data)
|
|
||||||
if ur {
|
|
||||||
write("[CQ:image,file=%x.image%s]", m[:], arg)
|
|
||||||
} else {
|
|
||||||
write("[CQ:image,file=%x.image,url=%s%s]", m[:], cqcode.EscapeValue(o.URL), arg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *message.GuildImageElement:
|
|
||||||
write("[CQ:image,file=%x.image,url=%s]", o.Md5, cqcode.EscapeValue(o.Url))
|
|
||||||
case *message.DiceElement:
|
|
||||||
write("[CQ:dice,value=%v]", o.Value)
|
|
||||||
case *message.MarketFaceElement:
|
|
||||||
sb.WriteString(o.Name)
|
|
||||||
case *message.ServiceElement:
|
|
||||||
if isOk := strings.Contains(o.Content, "<?xml"); isOk {
|
|
||||||
write(`[CQ:xml,data=%s,resid=%d]`, cqcode.EscapeValue(o.Content), o.Id)
|
|
||||||
} else {
|
|
||||||
write(`[CQ:json,data=%s,resid=%d]`, cqcode.EscapeValue(o.Content), o.Id)
|
|
||||||
}
|
|
||||||
case *message.LightAppElement:
|
|
||||||
write(`[CQ:json,data=%s]`, cqcode.EscapeValue(o.Content))
|
|
||||||
case *message.AnimatedSticker:
|
|
||||||
write(`[CQ:face,id=%d,type=sticker]`, o.ID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r = sb.String() // 内部已拷贝
|
|
||||||
global.PutBuffer(sb)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToMessageContent 将消息转换成 Content. 忽略 Reply
|
// ToMessageContent 将消息转换成 Content. 忽略 Reply
|
||||||
// 不同于 onebot 的 Array Message, 此函数转换出来的 Content 的 data 段为实际类型
|
// 不同于 onebot 的 Array Message, 此函数转换出来的 Content 的 data 段为实际类型
|
||||||
// 方便数据库查询
|
// 方便数据库查询
|
||||||
|
51
coolq/cqcode/element.go
Normal file
51
coolq/cqcode/element.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package cqcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Element struct {
|
||||||
|
Type string
|
||||||
|
Data []Pair
|
||||||
|
}
|
||||||
|
|
||||||
|
type Pair struct {
|
||||||
|
K string
|
||||||
|
V string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Element) CQCode() string {
|
||||||
|
if e.Type == "text" {
|
||||||
|
return EscapeText(e.Data[0].V) // must be {"text": value}
|
||||||
|
}
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString("[CQ:")
|
||||||
|
sb.WriteString(e.Type)
|
||||||
|
for _, data := range e.Data {
|
||||||
|
sb.WriteByte(',')
|
||||||
|
sb.WriteString(data.K)
|
||||||
|
sb.WriteByte('=')
|
||||||
|
sb.WriteString(EscapeValue(data.V))
|
||||||
|
}
|
||||||
|
sb.WriteByte(']')
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Element) MarshalJSON() ([]byte, error) {
|
||||||
|
buf := global.NewBuffer()
|
||||||
|
defer global.PutBuffer(buf)
|
||||||
|
|
||||||
|
fmt.Fprintf(buf, `{"type":"%s","data":{`, e.Type)
|
||||||
|
for i, data := range e.Data {
|
||||||
|
if i != 0 {
|
||||||
|
buf.WriteByte(',')
|
||||||
|
}
|
||||||
|
fmt.Fprintf(buf, `"%s":%q`, data.K, data.V)
|
||||||
|
}
|
||||||
|
buf.WriteString(`}}`)
|
||||||
|
|
||||||
|
return append([]byte(nil), buf.Bytes()...), nil
|
||||||
|
}
|
@ -21,11 +21,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ToFormattedMessage 将给定[]message.IMessageElement转换为通过coolq.SetMessageFormat所定义的消息上报格式
|
// ToFormattedMessage 将给定[]message.IMessageElement转换为通过coolq.SetMessageFormat所定义的消息上报格式
|
||||||
func ToFormattedMessage(e []message.IMessageElement, source message.Source, isRaw ...bool) (r interface{}) {
|
func ToFormattedMessage(e []message.IMessageElement, source message.Source, raw bool) (r interface{}) {
|
||||||
if base.PostFormat == "string" {
|
if base.PostFormat == "string" {
|
||||||
r = ToStringMessage(e, source, isRaw...)
|
r = toStringMessage(e, source, raw)
|
||||||
} else if base.PostFormat == "array" {
|
} else if base.PostFormat == "array" {
|
||||||
r = ToArrayMessage(e, source)
|
r = toElements(e, source, raw)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -36,7 +36,7 @@ func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMess
|
|||||||
SourceType: message.SourcePrivate,
|
SourceType: message.SourcePrivate,
|
||||||
PrimaryID: m.Sender.Uin,
|
PrimaryID: m.Sender.Uin,
|
||||||
}
|
}
|
||||||
cqm := ToStringMessage(m.Elements, source, true)
|
cqm := toStringMessage(m.Elements, source, true)
|
||||||
id := bot.InsertPrivateMessage(m)
|
id := bot.InsertPrivateMessage(m)
|
||||||
log.Infof("收到好友 %v(%v) 的消息: %v (%v)", m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
|
log.Infof("收到好友 %v(%v) 的消息: %v (%v)", m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
|
||||||
fm := global.MSG{
|
fm := global.MSG{
|
||||||
@ -93,7 +93,7 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
|
|||||||
SourceType: message.SourceGroup,
|
SourceType: message.SourceGroup,
|
||||||
PrimaryID: m.GroupCode,
|
PrimaryID: m.GroupCode,
|
||||||
}
|
}
|
||||||
cqm := ToStringMessage(m.Elements, source, true)
|
cqm := toStringMessage(m.Elements, source, true)
|
||||||
id := bot.InsertGroupMessage(m)
|
id := bot.InsertGroupMessage(m)
|
||||||
log.Infof("收到群 %v(%v) 内 %v(%v) 的消息: %v (%v)", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
|
log.Infof("收到群 %v(%v) 内 %v(%v) 的消息: %v (%v)", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
|
||||||
gm := bot.formatGroupMessage(m)
|
gm := bot.formatGroupMessage(m)
|
||||||
@ -111,7 +111,7 @@ func (bot *CQBot) tempMessageEvent(c *client.QQClient, e *client.TempMessageEven
|
|||||||
SourceType: message.SourcePrivate,
|
SourceType: message.SourcePrivate,
|
||||||
PrimaryID: e.Session.Sender,
|
PrimaryID: e.Session.Sender,
|
||||||
}
|
}
|
||||||
cqm := ToStringMessage(m.Elements, source, true)
|
cqm := toStringMessage(m.Elements, source, true)
|
||||||
bot.tempSessionCache.Store(m.Sender.Uin, e.Session)
|
bot.tempSessionCache.Store(m.Sender.Uin, e.Session)
|
||||||
id := m.Id
|
id := m.Id
|
||||||
// todo(Mrs4s)
|
// todo(Mrs4s)
|
||||||
@ -154,7 +154,7 @@ func (bot *CQBot) guildChannelMessageEvent(c *client.QQClient, m *message.GuildC
|
|||||||
PrimaryID: int64(m.GuildId),
|
PrimaryID: int64(m.GuildId),
|
||||||
SecondaryID: int64(m.ChannelId),
|
SecondaryID: int64(m.ChannelId),
|
||||||
}
|
}
|
||||||
log.Infof("收到来自频道 %v(%v) 子频道 %v(%v) 内 %v(%v) 的消息: %v", guild.GuildName, guild.GuildId, channel.ChannelName, m.ChannelId, m.Sender.Nickname, m.Sender.TinyId, ToStringMessage(m.Elements, source, true))
|
log.Infof("收到来自频道 %v(%v) 子频道 %v(%v) 内 %v(%v) 的消息: %v", guild.GuildName, guild.GuildId, channel.ChannelName, m.ChannelId, m.Sender.Nickname, m.Sender.TinyId, toStringMessage(m.Elements, source, true))
|
||||||
id := bot.InsertGuildChannelMessage(m)
|
id := bot.InsertGuildChannelMessage(m)
|
||||||
bot.dispatchEventMessage(global.MSG{
|
bot.dispatchEventMessage(global.MSG{
|
||||||
"post_type": "message",
|
"post_type": "message",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user