1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 11:07:39 +08:00

fix: interface convert panic

This commit is contained in:
源文雨 2022-11-10 17:30:54 +08:00
parent a6613d88bf
commit 92d78839f1
3 changed files with 72 additions and 51 deletions

View File

@ -11,6 +11,7 @@ import (
"net/url"
"os"
"path"
"reflect"
"runtime"
"strconv"
"strings"
@ -725,7 +726,17 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message
flash = true
}
if t.(string) == "show" {
id = data["id"].(int32)
id := 0
switch idn := data["id"].(type) {
case int32:
id = int(idn)
case int:
id = idn
case int64:
id = int(idn)
default:
id = int(reflect.ValueOf(data["id"]).Convert(reflect.TypeOf(0)).Int())
}
if id < 40000 || id >= 40006 {
id = 40000
}
@ -753,7 +764,7 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message
case "all":
r = append(r, message.NewAt(0))
case "user":
r = append(r, message.NewAt(data["target"].(int64), data["display"].(string)))
r = append(r, message.NewAt(reflect.ValueOf(data["target"]).Int(), data["display"].(string)))
default:
continue
}
@ -767,7 +778,18 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message
ResId: data["id"].(string),
})
case "face":
r = append(r, message.NewFace(data["id"].(int32)))
id := int32(0)
switch idn := data["id"].(type) {
case int32:
id = idn
case int:
id = int32(idn)
case int64:
id = int32(idn)
default:
id = int32(reflect.ValueOf(data["id"]).Convert(reflect.TypeOf(0)).Int())
}
r = append(r, message.NewFace(id))
case "video":
e, err := bot.makeImageOrVideoElem(map[string]string{"file": data["file"].(string)}, true, sourceType)
if err != nil {

View File

@ -40,61 +40,61 @@ type (
// StoredGroupMessage 持久化群消息
StoredGroupMessage struct {
ID string `bson:"_id"`
GlobalID int32 `bson:"globalId"`
Attribute *StoredMessageAttribute `bson:"attribute"`
SubType string `bson:"subType"`
QuotedInfo *QuotedInfo `bson:"quotedInfo"`
GroupCode int64 `bson:"groupCode"`
AnonymousID string `bson:"anonymousId"`
Content []global.MSG `bson:"content"`
ID string `bson:"_id" yaml:"-"`
GlobalID int32 `bson:"globalId" yaml:"-"`
Attribute *StoredMessageAttribute `bson:"attribute" yaml:"-"`
SubType string `bson:"subType" yaml:"-"`
QuotedInfo *QuotedInfo `bson:"quotedInfo" yaml:"-"`
GroupCode int64 `bson:"groupCode" yaml:"-"`
AnonymousID string `bson:"anonymousId" yaml:"-"`
Content []global.MSG `bson:"content" yaml:"content"`
}
// StoredPrivateMessage 持久化私聊消息
StoredPrivateMessage struct {
ID string `bson:"_id"`
GlobalID int32 `bson:"globalId"`
Attribute *StoredMessageAttribute `bson:"attribute"`
SubType string `bson:"subType"`
QuotedInfo *QuotedInfo `bson:"quotedInfo"`
SessionUin int64 `bson:"sessionUin"`
TargetUin int64 `bson:"targetUin"`
Content []global.MSG `bson:"content"`
ID string `bson:"_id" yaml:"-"`
GlobalID int32 `bson:"globalId" yaml:"-"`
Attribute *StoredMessageAttribute `bson:"attribute" yaml:"-"`
SubType string `bson:"subType" yaml:"-"`
QuotedInfo *QuotedInfo `bson:"quotedInfo" yaml:"-"`
SessionUin int64 `bson:"sessionUin" yaml:"-"`
TargetUin int64 `bson:"targetUin" yaml:"-"`
Content []global.MSG `bson:"content" yaml:"content"`
}
// StoredGuildChannelMessage 持久化频道消息
StoredGuildChannelMessage struct {
ID string `bson:"_id"`
Attribute *StoredGuildMessageAttribute `bson:"attribute"`
GuildID uint64 `bson:"guildId"`
ChannelID uint64 `bson:"channelId"`
QuotedInfo *QuotedInfo `bson:"quotedInfo"`
Content []global.MSG `bson:"content"`
ID string `bson:"_id" yaml:"-"`
Attribute *StoredGuildMessageAttribute `bson:"attribute" yaml:"-"`
GuildID uint64 `bson:"guildId" yaml:"-"`
ChannelID uint64 `bson:"channelId" yaml:"-"`
QuotedInfo *QuotedInfo `bson:"quotedInfo" yaml:"-"`
Content []global.MSG `bson:"content" yaml:"content"`
}
// StoredMessageAttribute 持久化消息属性
StoredMessageAttribute struct {
MessageSeq int32 `bson:"messageSeq"`
InternalID int32 `bson:"internalId"`
SenderUin int64 `bson:"senderUin"`
SenderName string `bson:"senderName"`
Timestamp int64 `bson:"timestamp"`
MessageSeq int32 `bson:"messageSeq" yaml:"-"`
InternalID int32 `bson:"internalId" yaml:"-"`
SenderUin int64 `bson:"senderUin" yaml:"-"`
SenderName string `bson:"senderName" yaml:"-"`
Timestamp int64 `bson:"timestamp" yaml:"-"`
}
// StoredGuildMessageAttribute 持久化频道消息属性
StoredGuildMessageAttribute struct {
MessageSeq uint64 `bson:"messageSeq"`
InternalID uint64 `bson:"internalId"`
SenderTinyID uint64 `bson:"senderTinyId"`
SenderName string `bson:"senderName"`
Timestamp int64 `bson:"timestamp"`
MessageSeq uint64 `bson:"messageSeq" yaml:"-"`
InternalID uint64 `bson:"internalId" yaml:"-"`
SenderTinyID uint64 `bson:"senderTinyId" yaml:"-"`
SenderName string `bson:"senderName" yaml:"-"`
Timestamp int64 `bson:"timestamp" yaml:"-"`
}
// QuotedInfo 引用回复
QuotedInfo struct {
PrevID string `bson:"prevId"`
PrevGlobalID int32 `bson:"prevGlobalId"`
QuotedContent []global.MSG `bson:"quotedContent"`
PrevID string `bson:"prevId" yaml:"-"`
PrevGlobalID int32 `bson:"prevGlobalId" yaml:"-"`
QuotedContent []global.MSG `bson:"quotedContent" yaml:"quoted_content"`
}
)

View File

@ -5,7 +5,6 @@ package sqlite3
import (
"encoding/base64"
"encoding/json"
"hash/crc64"
"os"
"path"
@ -146,7 +145,7 @@ func (s *database) GetGroupMessageByGlobalID(id int32) (*db.StoredGroupMessage,
ret.SubType = grpmsg.SubType
ret.GroupCode = grpmsg.GroupCode
ret.AnonymousID = grpmsg.AnonymousID
_ = json.Unmarshal(utils.S2B(grpmsg.Content), &ret.Content)
_ = yaml.Unmarshal(utils.S2B(grpmsg.Content), &ret)
if grpmsg.AttributeID != 0 {
var attr StoredMessageAttribute
s.RLock()
@ -178,7 +177,7 @@ func (s *database) GetGroupMessageByGlobalID(id int32) (*db.StoredGroupMessage,
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
_ = yaml.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo)
}
}
return &ret, nil
@ -198,7 +197,7 @@ func (s *database) GetPrivateMessageByGlobalID(id int32) (*db.StoredPrivateMessa
ret.SubType = privmsg.SubType
ret.SessionUin = privmsg.SessionUin
ret.TargetUin = privmsg.TargetUin
_ = json.Unmarshal(utils.S2B(privmsg.Content), &ret.Content)
_ = yaml.Unmarshal(utils.S2B(privmsg.Content), &ret)
if privmsg.AttributeID != 0 {
var attr StoredMessageAttribute
s.RLock()
@ -230,7 +229,7 @@ func (s *database) GetPrivateMessageByGlobalID(id int32) (*db.StoredPrivateMessa
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
_ = yaml.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo)
}
}
return &ret, nil
@ -255,7 +254,7 @@ func (s *database) GetGuildChannelMessageByID(id string) (*db.StoredGuildChannel
ret.ID = guildmsg.ID
ret.GuildID = uint64(guildmsg.GuildID)
ret.ChannelID = uint64(guildmsg.ChannelID)
_ = json.Unmarshal(utils.S2B(guildmsg.Content), &ret.Content)
_ = yaml.Unmarshal(utils.S2B(guildmsg.Content), &ret)
if guildmsg.AttributeID != 0 {
var attr StoredGuildMessageAttribute
s.RLock()
@ -287,7 +286,7 @@ func (s *database) GetGuildChannelMessageByID(id string) (*db.StoredGuildChannel
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
_ = yaml.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo)
}
}
return &ret, nil
@ -339,7 +338,7 @@ func (s *database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
}))
content, err := json.Marshal(&msg.QuotedInfo.QuotedContent)
content, err := yaml.Marshal(&msg.QuotedInfo)
if err != nil {
return errors.Wrap(err, "insert marshal QuotedContent error")
}
@ -360,7 +359,7 @@ func (s *database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
grpmsg.QuotedInfoID = id
}
}
content, err := json.Marshal(&msg.Content)
content, err := yaml.Marshal(&msg)
if err != nil {
return errors.Wrap(err, "insert marshal Content error")
}
@ -420,7 +419,7 @@ func (s *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
}))
content, err := json.Marshal(&msg.QuotedInfo.QuotedContent)
content, err := yaml.Marshal(&msg.QuotedInfo)
if err != nil {
return errors.Wrap(err, "insert marshal QuotedContent error")
}
@ -441,7 +440,7 @@ func (s *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
privmsg.QuotedInfoID = id
}
}
content, err := json.Marshal(&msg.Content)
content, err := yaml.Marshal(&msg)
if err != nil {
return errors.Wrap(err, "insert marshal Content error")
}
@ -499,7 +498,7 @@ func (s *database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage)
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
}))
content, err := json.Marshal(&msg.QuotedInfo.QuotedContent)
content, err := yaml.Marshal(&msg.QuotedInfo)
if err != nil {
return errors.Wrap(err, "insert marshal QuotedContent error")
}
@ -520,7 +519,7 @@ func (s *database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage)
guildmsg.QuotedInfoID = id
}
}
content, err := json.Marshal(&msg.Content)
content, err := yaml.Marshal(&msg)
if err != nil {
return errors.Wrap(err, "insert marshal Content error")
}