1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +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" "net/url"
"os" "os"
"path" "path"
"reflect"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -725,7 +726,17 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message
flash = true flash = true
} }
if t.(string) == "show" { 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 { if id < 40000 || id >= 40006 {
id = 40000 id = 40000
} }
@ -753,7 +764,7 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message
case "all": case "all":
r = append(r, message.NewAt(0)) r = append(r, message.NewAt(0))
case "user": 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: default:
continue continue
} }
@ -767,7 +778,18 @@ func (bot *CQBot) ConvertContentMessage(content []global.MSG, sourceType message
ResId: data["id"].(string), ResId: data["id"].(string),
}) })
case "face": 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": case "video":
e, err := bot.makeImageOrVideoElem(map[string]string{"file": data["file"].(string)}, true, sourceType) e, err := bot.makeImageOrVideoElem(map[string]string{"file": data["file"].(string)}, true, sourceType)
if err != nil { if err != nil {

View File

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

View File

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