mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 12:03:50 +08:00
coolq: move MessageSource to MiraiGo/message
This commit is contained in:
parent
b833193926
commit
19230b1511
40
coolq/api.go
40
coolq/api.go
@ -712,7 +712,7 @@ func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape b
|
||||
|
||||
var elem []message.IMessageElement
|
||||
if m.Type == gjson.JSON {
|
||||
elem = bot.ConvertObjectMessage(m, MessageSourceGroup)
|
||||
elem = bot.ConvertObjectMessage(m, message.SourceGroup)
|
||||
} else {
|
||||
str := m.String()
|
||||
if str == "" {
|
||||
@ -722,7 +722,7 @@ func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape b
|
||||
if autoEscape {
|
||||
elem = []message.IMessageElement{message.NewText(str)}
|
||||
} else {
|
||||
elem = bot.ConvertStringMessage(str, MessageSourceGroup)
|
||||
elem = bot.ConvertStringMessage(str, message.SourceGroup)
|
||||
}
|
||||
}
|
||||
fixAt(elem)
|
||||
@ -766,7 +766,7 @@ func (bot *CQBot) CQSendGuildChannelMessage(guildID, channelID uint64, m gjson.R
|
||||
|
||||
var elem []message.IMessageElement
|
||||
if m.Type == gjson.JSON {
|
||||
elem = bot.ConvertObjectMessage(m, MessageSourceGuildChannel)
|
||||
elem = bot.ConvertObjectMessage(m, message.SourceGuildChannel)
|
||||
} else {
|
||||
str := m.String()
|
||||
if str == "" {
|
||||
@ -776,7 +776,7 @@ func (bot *CQBot) CQSendGuildChannelMessage(guildID, channelID uint64, m gjson.R
|
||||
if autoEscape {
|
||||
elem = []message.IMessageElement{message.NewText(str)}
|
||||
} else {
|
||||
elem = bot.ConvertStringMessage(str, MessageSourceGuildChannel)
|
||||
elem = bot.ConvertStringMessage(str, message.SourceGuildChannel)
|
||||
}
|
||||
}
|
||||
fixAt(elem)
|
||||
@ -833,7 +833,7 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, groupID int64) *message.F
|
||||
}
|
||||
return int32(msgTime)
|
||||
}(),
|
||||
Message: resolveElement(bot.ConvertContentMessage(m.Content, MessageSourceGroup)),
|
||||
Message: resolveElement(bot.ConvertContentMessage(m.Content, message.SourceGroup)),
|
||||
}
|
||||
}
|
||||
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
|
||||
@ -865,7 +865,7 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, groupID int64) *message.F
|
||||
}
|
||||
}
|
||||
}
|
||||
content := bot.ConvertObjectMessage(c, MessageSourceGroup)
|
||||
content := bot.ConvertObjectMessage(c, message.SourceGroup)
|
||||
if uin != 0 && name != "" && len(content) > 0 {
|
||||
return &message.ForwardNode{
|
||||
SenderId: uin,
|
||||
@ -932,7 +932,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
|
||||
func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, m gjson.Result, autoEscape bool) global.MSG {
|
||||
var elem []message.IMessageElement
|
||||
if m.Type == gjson.JSON {
|
||||
elem = bot.ConvertObjectMessage(m, MessageSourcePrivate)
|
||||
elem = bot.ConvertObjectMessage(m, message.SourcePrivate)
|
||||
} else {
|
||||
str := m.String()
|
||||
if str == "" {
|
||||
@ -941,7 +941,7 @@ func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, m gjson.Resu
|
||||
if autoEscape {
|
||||
elem = []message.IMessageElement{message.NewText(str)}
|
||||
} else {
|
||||
elem = bot.ConvertStringMessage(str, MessageSourcePrivate)
|
||||
elem = bot.ConvertStringMessage(str, message.SourcePrivate)
|
||||
}
|
||||
}
|
||||
mid := bot.SendPrivateMessage(userID, groupID, &message.SendingMessage{Elements: elem})
|
||||
@ -1525,7 +1525,7 @@ func (bot *CQBot) CQGetForwardMessage(resID string) global.MSG {
|
||||
r := make([]global.MSG, len(nodes))
|
||||
for i, n := range nodes {
|
||||
bot.checkMedia(n.Message, 0)
|
||||
content := ToFormattedMessage(n.Message, MessageSource{SourceType: MessageSourceGroup}, false)
|
||||
content := ToFormattedMessage(n.Message, message.Source{SourceType: message.SourceGroup}, false)
|
||||
if len(n.Message) == 1 {
|
||||
if forward, ok := n.Message[0].(*message.ForwardMessage); ok {
|
||||
content = transformNodes(forward.Nodes)
|
||||
@ -1573,9 +1573,9 @@ func (bot *CQBot) CQGetMessage(messageID int32) global.MSG {
|
||||
switch o := msg.(type) {
|
||||
case *db.StoredGroupMessage:
|
||||
m["group_id"] = o.GroupCode
|
||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(o.Content, MessageSourceGroup), MessageSource{SourceType: MessageSourceGroup, PrimaryID: uint64(o.GroupCode)}, false)
|
||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(o.Content, message.SourceGroup), message.Source{SourceType: message.SourceGroup, PrimaryID: o.GroupCode}, false)
|
||||
case *db.StoredPrivateMessage:
|
||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(o.Content, MessageSourcePrivate), MessageSource{SourceType: MessageSourcePrivate}, false)
|
||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(o.Content, message.SourcePrivate), message.Source{SourceType: message.SourcePrivate}, false)
|
||||
}
|
||||
return OK(m)
|
||||
}
|
||||
@ -1591,21 +1591,21 @@ func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG {
|
||||
m := global.MSG{
|
||||
"message_id": messageID,
|
||||
"message_source": func() string {
|
||||
if source.SourceType == MessageSourceGuildDirect {
|
||||
if source.SourceType == message.SourceGuildDirect {
|
||||
return "direct"
|
||||
}
|
||||
return "channel"
|
||||
}(),
|
||||
"message_seq": seq,
|
||||
"guild_id": fU64(source.PrimaryID),
|
||||
"guild_id": fU64(uint64(source.PrimaryID)),
|
||||
"reactions": []int{},
|
||||
}
|
||||
// nolint: exhaustive
|
||||
switch source.SourceType {
|
||||
case MessageSourceGuildChannel:
|
||||
m["channel_id"] = fU64(source.SubID)
|
||||
case message.SourceGuildChannel:
|
||||
m["channel_id"] = fU64(uint64(source.SecondaryID))
|
||||
if noCache {
|
||||
pull, err := bot.Client.GuildService.PullGuildChannelMessage(source.PrimaryID, source.SubID, seq, seq)
|
||||
pull, err := bot.Client.GuildService.PullGuildChannelMessage(uint64(source.PrimaryID), uint64(source.SecondaryID), seq, seq)
|
||||
if err != nil {
|
||||
log.Warnf("获取消息时出现错误: %v", err)
|
||||
return Failed(100, "API_ERROR", err.Error())
|
||||
@ -1635,11 +1635,11 @@ func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG {
|
||||
"tiny_id": fU64(channelMsgByDB.Attribute.SenderTinyID),
|
||||
"nickname": channelMsgByDB.Attribute.SenderName,
|
||||
}
|
||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(channelMsgByDB.Content, MessageSourceGuildChannel), *source)
|
||||
m["message"] = ToFormattedMessage(bot.ConvertContentMessage(channelMsgByDB.Content, message.SourceGuildChannel), *source)
|
||||
}
|
||||
case MessageSourceGuildDirect:
|
||||
case message.SourceGuildDirect:
|
||||
// todo(mrs4s): 支持 direct 消息
|
||||
m["tiny_id"] = fU64(source.SubID)
|
||||
m["tiny_id"] = fU64(uint64(source.SecondaryID))
|
||||
}
|
||||
return OK(m)
|
||||
}
|
||||
@ -1737,7 +1737,7 @@ func (bot *CQBot) CQCanSendRecord() global.MSG {
|
||||
// @route(ocr_image,".ocr_image")
|
||||
// @rename(image_id->image)
|
||||
func (bot *CQBot) CQOcrImage(imageID string) global.MSG {
|
||||
img, err := bot.makeImageOrVideoElem(map[string]string{"file": imageID}, false, MessageSourceGroup)
|
||||
img, err := bot.makeImageOrVideoElem(map[string]string{"file": imageID}, false, message.SourceGroup)
|
||||
if err != nil {
|
||||
log.Warnf("load image error: %v", err)
|
||||
return Failed(100, "LOAD_FILE_ERROR", err.Error())
|
||||
|
43
coolq/bot.go
43
coolq/bot.go
@ -5,7 +5,6 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
@ -152,13 +151,13 @@ func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElemen
|
||||
}
|
||||
|
||||
// UploadLocalVideo 上传本地短视频至群聊
|
||||
func (bot *CQBot) UploadLocalVideo(target int64, v *LocalVideoElement) (*message.ShortVideoElement, error) {
|
||||
func (bot *CQBot) UploadLocalVideo(target message.Source, v *LocalVideoElement) (*message.ShortVideoElement, error) {
|
||||
video, err := os.Open(v.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = video.Close() }()
|
||||
return bot.Client.UploadGroupShortVideo(target, video, v.thumb, 4)
|
||||
return bot.Client.UploadShortVideo(target, video, v.thumb, 4)
|
||||
}
|
||||
|
||||
// UploadLocalImageAsPrivate 上传本地图片至私聊
|
||||
@ -197,18 +196,6 @@ func (bot *CQBot) UploadLocalImageAsGuildChannel(guildID, channelID uint64, img
|
||||
return bot.Client.GuildService.UploadGuildImage(guildID, channelID, img.Stream)
|
||||
}
|
||||
|
||||
func (bot *CQBot) uploadGuildVideo(i *LocalVideoElement, guildID, channelID uint64) (*message.ShortVideoElement, error) {
|
||||
video, err := os.Open(i.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = video.Close() }()
|
||||
_, _ = video.Seek(0, io.SeekStart)
|
||||
_, _ = i.thumb.Seek(0, io.SeekStart)
|
||||
n, err := bot.Client.UploadGuildShortVideo(guildID, channelID, video, i.thumb)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// SendGroupMessage 发送群消息
|
||||
func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int32 {
|
||||
newElem := make([]message.IMessageElement, 0, len(m.Elements))
|
||||
@ -368,7 +355,11 @@ func (bot *CQBot) SendGuildChannelMessage(guildID, channelID uint64, m *message.
|
||||
e = n
|
||||
|
||||
case *LocalVideoElement:
|
||||
n, err := bot.uploadGuildVideo(i, guildID, channelID)
|
||||
n, err := bot.UploadLocalVideo(message.Source{
|
||||
SourceType: message.SourceGuildChannel,
|
||||
PrimaryID: int64(guildID),
|
||||
SecondaryID: int64(channelID),
|
||||
}, i)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 频道 %d 消息%s上传失败: %v", channelID, e.Type().String(), err)
|
||||
continue
|
||||
@ -518,7 +509,7 @@ func (bot *CQBot) InsertTempMessage(target int64, m *message.TempMessage) int32
|
||||
|
||||
// InsertGuildChannelMessage 频道消息入数据库
|
||||
func (bot *CQBot) InsertGuildChannelMessage(m *message.GuildChannelMessage) string {
|
||||
id := encodeGuildMessageID(m.GuildId, m.ChannelId, m.Id, MessageSourceGuildChannel)
|
||||
id := encodeGuildMessageID(m.GuildId, m.ChannelId, m.Id, message.SourceGuildChannel)
|
||||
msg := &db.StoredGuildChannelMessage{
|
||||
ID: id,
|
||||
Attribute: &db.StoredGuildMessageAttribute{
|
||||
@ -597,7 +588,11 @@ func (bot *CQBot) uploadMedia(raw message.IMessageElement, target int64, group b
|
||||
}
|
||||
return bot.Client.UploadPrivatePtt(target, bytes.NewReader(m.Data))
|
||||
case *LocalVideoElement:
|
||||
return bot.UploadLocalVideo(target, m)
|
||||
source := message.Source{
|
||||
SourceType: message.SourceGroup,
|
||||
PrimaryID: target,
|
||||
}
|
||||
return bot.UploadLocalVideo(source, m)
|
||||
}
|
||||
return nil, errors.New("unsupported message element type")
|
||||
}
|
||||
@ -613,7 +608,7 @@ func encodeMessageID(target int64, seq int32) string {
|
||||
// encodeGuildMessageID 将频道信息编码为字符串
|
||||
// 当信息来源为 Channel 时 primaryID 为 guildID , subID 为 channelID
|
||||
// 当信息来源为 Direct 时 primaryID 为 guildID , subID 为 tinyID
|
||||
func encodeGuildMessageID(primaryID, subID, seq uint64, source MessageSourceType) string {
|
||||
func encodeGuildMessageID(primaryID, subID, seq uint64, source message.SourceType) string {
|
||||
return base64.StdEncoding.EncodeToString(binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.WriteByte(byte(source))
|
||||
w.WriteUInt64(primaryID)
|
||||
@ -622,16 +617,16 @@ func encodeGuildMessageID(primaryID, subID, seq uint64, source MessageSourceType
|
||||
}))
|
||||
}
|
||||
|
||||
func decodeGuildMessageID(id string) (source *MessageSource, seq uint64) {
|
||||
func decodeGuildMessageID(id string) (source *message.Source, seq uint64) {
|
||||
b, _ := base64.StdEncoding.DecodeString(id)
|
||||
if len(b) < 25 {
|
||||
return
|
||||
}
|
||||
r := binary.NewReader(b)
|
||||
source = &MessageSource{
|
||||
SourceType: MessageSourceType(r.ReadByte()),
|
||||
PrimaryID: uint64(r.ReadInt64()),
|
||||
SubID: uint64(r.ReadInt64()),
|
||||
source = &message.Source{
|
||||
SourceType: message.SourceType(r.ReadByte()),
|
||||
PrimaryID: r.ReadInt64(),
|
||||
SecondaryID: r.ReadInt64(),
|
||||
}
|
||||
seq = uint64(r.ReadInt64())
|
||||
return
|
||||
|
@ -60,9 +60,9 @@ func convertGuildMemberInfo(m []*client.GuildMemberInfo) (r []global.MSG) {
|
||||
}
|
||||
|
||||
func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) global.MSG {
|
||||
source := MessageSource{
|
||||
SourceType: MessageSourceGroup,
|
||||
PrimaryID: uint64(m.GroupCode),
|
||||
source := message.Source{
|
||||
SourceType: message.SourceGroup,
|
||||
PrimaryID: m.GroupCode,
|
||||
}
|
||||
cqm := ToStringMessage(m.Elements, source, true)
|
||||
postType := "message"
|
||||
|
@ -63,26 +63,6 @@ type LocalVideoElement struct {
|
||||
thumb io.ReadSeeker
|
||||
}
|
||||
|
||||
// MessageSource 消息来源
|
||||
// 如果为私聊或者群聊, PrimaryID 将代表群号/QQ号
|
||||
// 如果为频道, PrimaryID 为 GuildID, SubID 为 ChannelID
|
||||
type MessageSource struct {
|
||||
SourceType MessageSourceType
|
||||
PrimaryID uint64
|
||||
SubID uint64
|
||||
}
|
||||
|
||||
// MessageSourceType 消息来源类型
|
||||
type MessageSourceType byte
|
||||
|
||||
// MessageSourceType 常量
|
||||
const (
|
||||
MessageSourcePrivate MessageSourceType = 1 << iota
|
||||
MessageSourceGroup
|
||||
MessageSourceGuildChannel
|
||||
MessageSourceGuildDirect
|
||||
)
|
||||
|
||||
const (
|
||||
maxImageSize = 1024 * 1024 * 30 // 30MB
|
||||
maxVideoSize = 1024 * 1024 * 100 // 100MB
|
||||
@ -104,10 +84,10 @@ func (e *PokeElement) Type() message.ElementType {
|
||||
return message.At
|
||||
}
|
||||
|
||||
func replyID(r *message.ReplyElement, source MessageSource) int32 {
|
||||
func replyID(r *message.ReplyElement, source message.Source) int32 {
|
||||
id := int64(source.PrimaryID)
|
||||
seq := r.ReplySeq
|
||||
if source.SourceType == MessageSourcePrivate {
|
||||
if source.SourceType == message.SourcePrivate {
|
||||
// 私聊似乎腾讯服务器有bug?
|
||||
seq = int32(uint16(seq))
|
||||
id = r.Sender
|
||||
@ -119,14 +99,14 @@ func replyID(r *message.ReplyElement, source MessageSource) int32 {
|
||||
}
|
||||
|
||||
// ToArrayMessage 将消息元素数组转为MSG数组以用于消息上报
|
||||
func ToArrayMessage(e []message.IMessageElement, source MessageSource) (r []global.MSG) {
|
||||
func ToArrayMessage(e []message.IMessageElement, source message.Source) (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)
|
||||
return ok
|
||||
})
|
||||
if reply != nil && source.SourceType&(MessageSourceGroup|MessageSourcePrivate) != 0 {
|
||||
if reply != nil && source.SourceType&(message.SourceGroup|message.SourcePrivate) != 0 {
|
||||
replyElem := reply.(*message.ReplyElement)
|
||||
id := replyID(replyElem, source)
|
||||
if base.ExtraReplyData {
|
||||
@ -270,7 +250,7 @@ func ToArrayMessage(e []message.IMessageElement, source MessageSource) (r []glob
|
||||
}
|
||||
|
||||
// ToStringMessage 将消息元素数组转为字符串以用于消息上报
|
||||
func ToStringMessage(e []message.IMessageElement, source MessageSource, isRaw ...bool) (r string) {
|
||||
func ToStringMessage(e []message.IMessageElement, source message.Source, isRaw ...bool) (r string) {
|
||||
sb := global.NewBuffer()
|
||||
sb.Reset()
|
||||
write := func(format string, a ...interface{}) {
|
||||
@ -286,7 +266,7 @@ func ToStringMessage(e []message.IMessageElement, source MessageSource, isRaw ..
|
||||
_, ok := e.(*message.ReplyElement)
|
||||
return ok
|
||||
})
|
||||
if reply != nil && source.SourceType&(MessageSourceGroup|MessageSourcePrivate) != 0 {
|
||||
if reply != nil && source.SourceType&(message.SourceGroup|message.SourcePrivate) != 0 {
|
||||
replyElem := reply.(*message.ReplyElement)
|
||||
id := replyID(replyElem, source)
|
||||
if base.ExtraReplyData {
|
||||
@ -511,7 +491,7 @@ func ToMessageContent(e []message.IMessageElement) (r []global.MSG) {
|
||||
}
|
||||
|
||||
// ConvertStringMessage 将消息字符串转为消息元素数组
|
||||
func (bot *CQBot) ConvertStringMessage(raw string, sourceType MessageSourceType) (r []message.IMessageElement) {
|
||||
func (bot *CQBot) ConvertStringMessage(raw string, sourceType message.SourceType) (r []message.IMessageElement) {
|
||||
var t, key string
|
||||
d := map[string]string{}
|
||||
|
||||
@ -684,11 +664,11 @@ func (bot *CQBot) ConvertStringMessage(raw string, sourceType MessageSourceType)
|
||||
}
|
||||
|
||||
// ConvertObjectMessage 将消息JSON对象转为消息元素数组
|
||||
func (bot *CQBot) ConvertObjectMessage(m gjson.Result, sourceType MessageSourceType) (r []message.IMessageElement) {
|
||||
func (bot *CQBot) ConvertObjectMessage(m gjson.Result, sourceType message.SourceType) (r []message.IMessageElement) {
|
||||
d := make(map[string]string)
|
||||
convertElem := func(e gjson.Result) {
|
||||
t := e.Get("type").Str
|
||||
if t == "reply" && sourceType&(MessageSourceGroup|MessageSourcePrivate) != 0 {
|
||||
if t == "reply" && sourceType&(message.SourceGroup|message.SourcePrivate) != 0 {
|
||||
if len(r) > 0 {
|
||||
if _, ok := r[0].(*message.ReplyElement); ok {
|
||||
log.Warnf("警告: 一条信息只能包含一个 Reply 元素.")
|
||||
@ -804,7 +784,7 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, sourceType MessageSourceT
|
||||
}
|
||||
|
||||
// ConvertContentMessage 将数据库用的 content 转换为消息元素数组
|
||||
func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType MessageSourceType) (r []message.IMessageElement) {
|
||||
func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message.SourceType) (r []message.IMessageElement) {
|
||||
for _, c := range content {
|
||||
data := c["data"].(global.MSG)
|
||||
switch c["type"] {
|
||||
@ -883,7 +863,7 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType Message
|
||||
// 返回 interface{} 存在三种类型
|
||||
//
|
||||
// message.IMessageElement []message.IMessageElement nil
|
||||
func (bot *CQBot) ToElement(t string, d map[string]string, sourceType MessageSourceType) (m interface{}, err error) {
|
||||
func (bot *CQBot) ToElement(t string, d map[string]string, sourceType message.SourceType) (m interface{}, err error) {
|
||||
switch t {
|
||||
case "text":
|
||||
if base.SplitURL {
|
||||
@ -1117,7 +1097,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, sourceType MessageSou
|
||||
if err != nil {
|
||||
return nil, errors.New("send cardimage faild")
|
||||
}
|
||||
return bot.makeShowPic(img, source, brief, icon, minWidth, minHeight, maxWidth, maxHeight, sourceType == MessageSourceGroup)
|
||||
return bot.makeShowPic(img, source, brief, icon, minWidth, minHeight, maxWidth, maxHeight, sourceType == message.SourceGroup)
|
||||
case "video":
|
||||
file, err := bot.makeImageOrVideoElem(d, true, sourceType)
|
||||
if err != nil {
|
||||
@ -1170,7 +1150,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, sourceType MessageSou
|
||||
}
|
||||
|
||||
// makeImageOrVideoElem 图片 elem 生成器,单独拎出来,用于公用
|
||||
func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceType MessageSourceType) (message.IMessageElement, error) {
|
||||
func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceType message.SourceType) (message.IMessageElement, error) {
|
||||
f := d["file"]
|
||||
u, ok := d["url"]
|
||||
if !ok {
|
||||
@ -1254,7 +1234,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
||||
return bot.readVideoCache(b), nil
|
||||
}
|
||||
// 目前频道内上传的图片均无法被查询到, 需要单独处理
|
||||
if sourceType == MessageSourceGuildChannel {
|
||||
if sourceType == message.SourceGuildChannel {
|
||||
cacheFile := path.Join(global.ImagePath, "guild-images", f)
|
||||
if global.PathExists(cacheFile) {
|
||||
return &LocalImageElement{File: cacheFile}, nil
|
||||
@ -1289,7 +1269,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
||||
return bot.readImageCache(b, sourceType)
|
||||
}
|
||||
|
||||
func (bot *CQBot) readImageCache(b []byte, sourceType MessageSourceType) (message.IMessageElement, error) {
|
||||
func (bot *CQBot) readImageCache(b []byte, sourceType message.SourceType) (message.IMessageElement, error) {
|
||||
var err error
|
||||
if len(b) < 20 {
|
||||
return nil, errors.New("invalid cache")
|
||||
@ -1304,9 +1284,9 @@ func (bot *CQBot) readImageCache(b []byte, sourceType MessageSourceType) (messag
|
||||
}
|
||||
var rsp message.IMessageElement
|
||||
switch sourceType { // nolint:exhaustive
|
||||
case MessageSourceGroup:
|
||||
case message.SourceGroup:
|
||||
rsp, err = bot.Client.QueryGroupImage(int64(rand.Uint32()), hash, size)
|
||||
case MessageSourceGuildChannel:
|
||||
case message.SourceGuildChannel:
|
||||
if len(bot.Client.GuildService.Guilds) == 0 {
|
||||
err = errors.New("cannot query guild image: not any joined guild")
|
||||
break
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/message"
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/gjson"
|
||||
@ -15,7 +16,7 @@ import (
|
||||
var bot = CQBot{}
|
||||
|
||||
func TestCQBot_ConvertStringMessage(t *testing.T) {
|
||||
for _, v := range bot.ConvertStringMessage(`[CQ:face,id=115,text=111][CQ:face,id=217]] [CQ:text,text=123] [`, MessageSourcePrivate) {
|
||||
for _, v := range bot.ConvertStringMessage(`[CQ:face,id=115,text=111][CQ:face,id=217]] [CQ:text,text=123] [`, message.SourcePrivate) {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
@ -27,14 +28,14 @@ var (
|
||||
|
||||
func BenchmarkCQBot_ConvertStringMessage(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
bot.ConvertStringMessage(bench, MessageSourcePrivate)
|
||||
bot.ConvertStringMessage(bench, message.SourcePrivate)
|
||||
}
|
||||
b.SetBytes(int64(len(bench)))
|
||||
}
|
||||
|
||||
func BenchmarkCQBot_ConvertObjectMessage(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
bot.ConvertObjectMessage(benchArray, MessageSourcePrivate)
|
||||
bot.ConvertObjectMessage(benchArray, message.SourcePrivate)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
// ToFormattedMessage 将给定[]message.IMessageElement转换为通过coolq.SetMessageFormat所定义的消息上报格式
|
||||
func ToFormattedMessage(e []message.IMessageElement, source MessageSource, isRaw ...bool) (r interface{}) {
|
||||
func ToFormattedMessage(e []message.IMessageElement, source message.Source, isRaw ...bool) (r interface{}) {
|
||||
if base.PostFormat == "string" {
|
||||
r = ToStringMessage(e, source, isRaw...)
|
||||
} else if base.PostFormat == "array" {
|
||||
@ -32,9 +32,9 @@ func ToFormattedMessage(e []message.IMessageElement, source MessageSource, isRaw
|
||||
|
||||
func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMessage) {
|
||||
bot.checkMedia(m.Elements, m.Sender.Uin)
|
||||
source := MessageSource{
|
||||
SourceType: MessageSourcePrivate,
|
||||
PrimaryID: uint64(m.Sender.Uin),
|
||||
source := message.Source{
|
||||
SourceType: message.SourcePrivate,
|
||||
PrimaryID: m.Sender.Uin,
|
||||
}
|
||||
cqm := ToStringMessage(m.Elements, source, true)
|
||||
id := bot.InsertPrivateMessage(m)
|
||||
@ -89,9 +89,9 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
|
||||
return
|
||||
}
|
||||
}
|
||||
source := MessageSource{
|
||||
SourceType: MessageSourceGroup,
|
||||
PrimaryID: uint64(m.GroupCode),
|
||||
source := message.Source{
|
||||
SourceType: message.SourceGroup,
|
||||
PrimaryID: m.GroupCode,
|
||||
}
|
||||
cqm := ToStringMessage(m.Elements, source, true)
|
||||
id := bot.InsertGroupMessage(m)
|
||||
@ -107,9 +107,9 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
|
||||
func (bot *CQBot) tempMessageEvent(c *client.QQClient, e *client.TempMessageEvent) {
|
||||
m := e.Message
|
||||
bot.checkMedia(m.Elements, m.Sender.Uin)
|
||||
source := MessageSource{
|
||||
SourceType: MessageSourcePrivate,
|
||||
PrimaryID: uint64(e.Session.Sender),
|
||||
source := message.Source{
|
||||
SourceType: message.SourcePrivate,
|
||||
PrimaryID: e.Session.Sender,
|
||||
}
|
||||
cqm := ToStringMessage(m.Elements, source, true)
|
||||
bot.tempSessionCache.Store(m.Sender.Uin, e.Session)
|
||||
@ -149,10 +149,10 @@ func (bot *CQBot) guildChannelMessageEvent(c *client.QQClient, m *message.GuildC
|
||||
return
|
||||
}
|
||||
channel := guild.FindChannel(m.ChannelId)
|
||||
source := MessageSource{
|
||||
SourceType: MessageSourceGuildChannel,
|
||||
PrimaryID: m.GuildId,
|
||||
SubID: m.ChannelId,
|
||||
source := message.Source{
|
||||
SourceType: message.SourceGuildChannel,
|
||||
PrimaryID: int64(m.GuildId),
|
||||
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))
|
||||
id := bot.InsertGuildChannelMessage(m)
|
||||
@ -181,7 +181,7 @@ func (bot *CQBot) guildMessageReactionsUpdatedEvent(c *client.QQClient, e *clien
|
||||
if guild == nil {
|
||||
return
|
||||
}
|
||||
msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId, MessageSourceGuildChannel)
|
||||
msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId, message.SourceGuildChannel)
|
||||
str := fmt.Sprintf("频道 %v(%v) 消息 %v 表情贴片已更新: ", guild.GuildName, guild.GuildId, msgID)
|
||||
currentReactions := make([]global.MSG, len(e.CurrentReactions))
|
||||
for i, r := range e.CurrentReactions {
|
||||
@ -228,7 +228,7 @@ func (bot *CQBot) guildChannelMessageRecalledEvent(c *client.QQClient, e *client
|
||||
log.Errorf("处理频道撤回事件时出现错误: 获取操作者资料时出现错误 %v", err)
|
||||
return
|
||||
}
|
||||
msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId, MessageSourceGuildChannel)
|
||||
msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId, message.SourceGuildChannel)
|
||||
log.Infof("用户 %v(%v) 撤回了频道 %v(%v) 子频道 %v(%v) 的消息 %v", operator.Nickname, operator.TinyId, guild.GuildName, guild.GuildId, channel.ChannelName, channel.ChannelId, msgID)
|
||||
bot.dispatchEventMessage(global.MSG{
|
||||
"post_type": "notice",
|
||||
|
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.17
|
||||
|
||||
require (
|
||||
github.com/Microsoft/go-winio v0.5.1
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20220220152607-5e8a5126989d
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20220221093105-1e32793eef06
|
||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
||||
github.com/fumiama/go-hide-param v0.1.4
|
||||
github.com/gabriel-vasile/mimetype v1.4.0
|
||||
|
4
go.sum
4
go.sum
@ -1,7 +1,7 @@
|
||||
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
|
||||
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20220220152607-5e8a5126989d h1:Og/CXvf7cNq+GFpIe3Tjll500qD8L8wunTXC6bQRUxI=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20220220152607-5e8a5126989d/go.mod h1:T66Ua3SOfpJMx+DcfQxk95MeR8RmvAVmjYSkoQQ8nwI=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20220221093105-1e32793eef06 h1:9V7AhNUPJ1vqLAoXjwLdpv06hJWs15UdgYbKaHww60s=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20220221093105-1e32793eef06/go.mod h1:T66Ua3SOfpJMx+DcfQxk95MeR8RmvAVmjYSkoQQ8nwI=
|
||||
github.com/RomiChan/protobuf v0.0.0-20220213164748-44b69c8bdec0 h1:8CK7Hg+CRGTFhpjvp5V+7wd8/TkuZ6fSuztLVV3bwoQ=
|
||||
github.com/RomiChan/protobuf v0.0.0-20220213164748-44b69c8bdec0/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE=
|
||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
|
||||
|
Loading…
x
Reference in New Issue
Block a user