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

Beta137 #313

Closed
wants to merge 17 commits into from
49 changes: 47 additions & 2 deletions echo/messageidmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/hoshinonyaruko/gensokyo/mylog"
)

type messageRecord struct {
Expand Down Expand Up @@ -68,18 +70,61 @@
randomIndex := rand.Intn(len(recentMessages))
randomMessageID = recentMessages[randomIndex]
} else {
msgType := GetMessageTypeByGroupidv2(config.GetAppIDStr(), groupID)
groupIDint64, err := idmap.StoreIDv2(groupID)
if err != nil {
mylog.Printf("Error storing ID 75: %v", err)
return "2000" //主动信息(不知道消息类型,按2000,纯主动信息处理)
}
msgType := GetMessageTypeByGroupidv2(config.GetAppIDStr(), groupIDint64)
if strings.HasPrefix(msgType, "guild") {
randomMessageID = "1000" // 频道主动信息
} else {
randomMessageID = ""
randomMessageID = "2000" //群主动信息
}
}
return randomMessageID
}

// GetLazyMessagesIdv2 获取指定群号和用户ID中最近5分钟内的 message_id
func GetLazyMessagesIdv2(groupID, userID string) string {

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for amd64

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for 386

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for arm

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for arm64

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on darwin for amd64

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on darwin for arm64

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on windows for amd64

other declaration of GetLazyMessagesIdv2

Check failure on line 89 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on windows for 386

other declaration of GetLazyMessagesIdv2
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 {
// 如果没有找到最近消息,处理默认行为
groupIDint64, err := idmap.StoreIDv2(groupID)
if err != nil {
mylog.Printf("Error storing ID 75: %v", err)
return "2000" //主动信息(不知道消息类型,按2000,纯主动信息处理)
}
msgType := GetMessageTypeByGroupidv2(config.GetAppIDStr(), groupIDint64)
if strings.HasPrefix(msgType, "guild") {
randomMessageID = "1000" // 频道主动信息
} else {
randomMessageID = "2000" //群主动信息
}
}
return randomMessageID
}

// GetLazyMessagesIdv2 获取指定群号和用户ID中最近5分钟内的 message_id
func GetLazyMessagesIdv2(groupID, userID string) string {

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for amd64

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for 386

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for arm

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on linux for arm64

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on darwin for amd64

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on darwin for arm64

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on windows for amd64

GetLazyMessagesIdv2 redeclared in this block

Check failure on line 127 in echo/messageidmap.go

View workflow job for this annotation

GitHub Actions / Build on windows for 386

GetLazyMessagesIdv2 redeclared in this block
store := initInstance()
store.mu.RLock()
defer store.mu.RUnlock()
Expand Down
16 changes: 11 additions & 5 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ 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.GetLazyMessagesIdv2(message.Params.GroupID.(string), message.Params.UserID.(string))
mylog.Printf("GetLazyMessagesIdv2: %v", messageID)
}
if messageID != "" {
//2000是群主动 此时不能被动转主动
//仅在开启lazy_message_id时,有信息主动转被动特性,即,SSM
if messageID != "2000" {
//尝试发送栈内信息
SSM = true
}
Expand Down Expand Up @@ -144,6 +145,7 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
}
message.Params.GroupID = originalGroupID
message.Params.UserID = originalUserID
//2000是群主动 此时不能被动转主动
if SSM {
//mylog.Printf("正在使用Msgid:%v 补发之前失败的主动信息,请注意AtoP不要设置超过3,否则可能会影响正常信息发送", messageID)
//mylog.Printf("originalGroupID:%v ", originalGroupID)
Expand All @@ -166,9 +168,13 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
messageID = GetMessageIDByUseridOrGroupid(config.GetAppIDStr(), message.Params.GroupID)
mylog.Println("通过GetMessageIDByUseridOrGroupid函数获取的message_id:", message.Params.GroupID, messageID)
}
//开发环境用
if config.GetDevMsgID() {
messageID = "1000"
//开发环境用 1000在群里无效
// if config.GetDevMsgID() {
// messageID = "1000"
// }
if messageID == "2000" {
messageID = ""
mylog.Println("通过lazymessage_id模式发送群聊/频道主动信息,群聊每月仅4次机会,如果本信息非主动推送信息,请提交issue")
}
mylog.Printf("群组发信息使用messageID:[%v]", messageID)
var singleItem = make(map[string][]string)
Expand Down
5 changes: 5 additions & 0 deletions handlers/send_guild_channel_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func HandleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2
messageID = GetMessageIDByUseridOrGroupid(config.GetAppIDStr(), channelID)
mylog.Println("通过GetMessageIDByUseridOrGroupid函数获取的message_id:", messageID)
}
//主动信息
if messageID == "2000" {
messageID = ""
mylog.Println("通过lazymsgid发送频道主动信息,若非主动信息请提交issue")
}
//开发环境用
if config.GetDevMsgID() {
messageID = "1000"
Expand Down
14 changes: 11 additions & 3 deletions handlers/send_private_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ func HandleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
messageID = GetMessageIDByUseridOrGroupid(config.GetAppIDStr(), UserID)
mylog.Println("通过GetMessageIDByUserid函数获取的message_id:", messageID)
}
//开发环境用
if config.GetDevMsgID() {
messageID = "1000"
if messageID == "2000" {
messageID = ""
mylog.Println("通过lazymsgid发送群私聊主动信息,每月可发送1次")
}
//开发环境用 私聊不可用1000
// if config.GetDevMsgID() {
// messageID = "1000"
// }
mylog.Println("私聊发信息messageText:", messageText)
//mylog.Println("foundItems:", foundItems)

Expand Down Expand Up @@ -444,6 +448,10 @@ func HandleSendGuildChannelPrivateMsg(client callapi.Client, api openapi.OpenAPI
}
}
}
if messageID == "2000" {
messageID = ""
mylog.Println("通过lazymsgid发送频道私聊主动信息,若非主动信息请提交issue")
}
//开发环境用
if config.GetDevMsgID() {
messageID = "1000"
Expand Down
Loading