Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta136 #311

Merged
merged 16 commits into from
Jan 21, 2024
3 changes: 2 additions & 1 deletion Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(GroupID64, 10), data.ID, time.Now())
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
echo.AddLazyMessageIdv2(strconv.FormatInt(GroupID64, 10), strconv.FormatInt(userid64, 10), data.ID, time.Now())
// 调试
PrintStructWithFieldNames(groupMsg)

Expand Down
6 changes: 4 additions & 2 deletions Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
//懒message_id池
echo.AddLazyMessageId(data.ChannelID, data.ID, time.Now())
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageIdv2(data.ChannelID, strconv.FormatInt(userid64, 10), data.ID, time.Now())

//调试
PrintStructWithFieldNames(onebotMsg)
Expand Down Expand Up @@ -267,7 +268,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(ChannelID64, 10), data.ID, time.Now())
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageIdv2(strconv.FormatInt(ChannelID64, 10), strconv.FormatInt(userid64, 10), data.ID, time.Now())

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
6 changes: 4 additions & 2 deletions Processor/ProcessGuildNormalMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
//懒message_id池
echo.AddLazyMessageId(data.ChannelID, data.ID, time.Now())
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageIdv2(data.ChannelID, strconv.FormatInt(userid64, 10), data.ID, time.Now())

//调试
PrintStructWithFieldNames(onebotMsg)
Expand Down Expand Up @@ -275,7 +276,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(ChannelID64, 10), data.ID, time.Now())
//懒message_id池
echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now())
//echo.AddLazyMessageIdv2(strconv.FormatInt(ChannelID64, 10), strconv.FormatInt(userid64, 10), data.ID, time.Now())

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
42 changes: 42 additions & 0 deletions echo/messageidmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func AddLazyMessageId(groupID, messageID string, timestamp time.Time) {
store.records[groupID] = append(store.records[groupID], messageRecord{messageID: messageID, timestamp: timestamp})
}

// AddLazyMessageId 添加 message_id 和它的时间戳到指定群号
func AddLazyMessageIdv2(groupID, userID, messageID string, timestamp time.Time) {
store := initInstance()
store.mu.Lock()
defer store.mu.Unlock()
key := groupID + "." + userID
store.records[key] = append(store.records[key], messageRecord{messageID: messageID, timestamp: timestamp})
}

// GetRecentMessages 获取指定群号中最近5分钟内的 message_id
func GetLazyMessagesId(groupID string) string {
store := initInstance()
Expand Down Expand Up @@ -69,6 +78,39 @@ func GetLazyMessagesId(groupID string) string {
return randomMessageID
}

// GetLazyMessagesIdv2 获取指定群号和用户ID中最近5分钟内的 message_id
func GetLazyMessagesIdv2(groupID, userID string) string {
store := initInstance()
store.mu.RLock()
defer store.mu.RUnlock()

// 构建复合键
key := groupID + "." + userID

fiveMinutesAgo := time.Now().Add(-5 * time.Minute)
var recentMessages []string
for _, record := range store.records[key] {
if record.timestamp.After(fiveMinutesAgo) {
recentMessages = append(recentMessages, record.messageID)
}
}

var randomMessageID string
if len(recentMessages) > 0 {
randomIndex := rand.Intn(len(recentMessages))
randomMessageID = recentMessages[randomIndex]
} else {
// 如果没有找到最近消息,处理默认行为
msgType := GetMessageTypeByGroupidv2(config.GetAppIDStr(), groupID)
if strings.HasPrefix(msgType, "guild") {
randomMessageID = "1000" // 频道主动信息
} else {
randomMessageID = ""
}
}
return randomMessageID
}

// 通过group_id获取类型
func GetMessageTypeByGroupidv2(appID string, GroupID interface{}) string {
// 从appID和userID生成key
Expand Down
3 changes: 2 additions & 1 deletion handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
mylog.Printf("GetLazyMessagesId: %v", messageID)
//如果应用端传递了user_id 就让at不要顺序乱套
if message.Params.UserID != nil {
messageID = echo.GetLazyMessagesId(message.Params.UserID.(string))
//messageID = echo.GetLazyMessagesId(message.Params.UserID.(string))
messageID = echo.GetLazyMessagesIdv2(message.Params.GroupID.(string), message.Params.UserID.(string))
mylog.Printf("GetLazyMessagesIdv2: %v", messageID)
}
if messageID != "" {
Expand Down
Loading