From f2a140c904b30d88c393f916324c452f83996a01 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Fri, 14 Jun 2024 21:25:20 +0800 Subject: [PATCH 01/13] beta430 (#429) --- handlers/message_parser.go | 62 ++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/handlers/message_parser.go b/handlers/message_parser.go index 5c1a7d1a..0f19180b 100644 --- a/handlers/message_parser.go +++ b/handlers/message_parser.go @@ -451,43 +451,41 @@ func parseMessageContent(paramsMessage callapi.ParamsContent, message callapi.Ac qqNumber, _ := segmentMap["data"].(map[string]interface{})["qq"].(string) segmentContent = "[CQ:at,qq=" + qqNumber + "]" case "markdown": - mdContent, ok := segmentMap["data"].(map[string]interface{})["data"] - if ok { - if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { - // mdContent是map[string]interface{},按map处理 - mdContentBytes, err := json.Marshal(mdContentMap) + mdContent := segmentMap["data"] + if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { + // mdContent是map[string]interface{},按map处理 + mdContentBytes, err := json.Marshal(mdContentMap) + if err != nil { + mylog.Printf("Error marshaling mdContentMap to JSON:%v", err) + } + encoded := base64.StdEncoding.EncodeToString(mdContentBytes) + segmentContent = "[CQ:markdown,data=base64://" + encoded + "]" + } else if mdContentStr, isString := mdContent.(string); isString { + // mdContent是string + if strings.HasPrefix(mdContentStr, "base64://") { + // 如果以base64://开头,直接使用 + segmentContent = "[CQ:markdown,data=" + mdContentStr + "]" + } else { + // 处理实体化后的JSON文本 + mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") + mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") + mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") + mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") + + // 将处理过的字符串视为JSON对象,进行序列化和编码 + var jsonMap map[string]interface{} + if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { + mylog.Printf("Error unmarshaling string to JSON:%v", err) + } + mdContentBytes, err := json.Marshal(jsonMap) if err != nil { - mylog.Printf("Error marshaling mdContentMap to JSON:%v", err) + mylog.Printf("Error marshaling jsonMap to JSON:%v", err) } encoded := base64.StdEncoding.EncodeToString(mdContentBytes) - segmentContent = "[CQ:markdown,data=" + encoded + "]" - } else if mdContentStr, isString := mdContent.(string); isString { - // mdContent是string - if strings.HasPrefix(mdContentStr, "base64://") { - // 如果以base64://开头,直接使用 - segmentContent = "[CQ:markdown,data=" + mdContentStr + "]" - } else { - // 处理实体化后的JSON文本 - mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") - mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") - mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") - mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") - - // 将处理过的字符串视为JSON对象,进行序列化和编码 - var jsonMap map[string]interface{} - if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { - mylog.Printf("Error unmarshaling string to JSON:%v", err) - } - mdContentBytes, err := json.Marshal(jsonMap) - if err != nil { - mylog.Printf("Error marshaling jsonMap to JSON:%v", err) - } - encoded := base64.StdEncoding.EncodeToString(mdContentBytes) - segmentContent = "[CQ:markdown,data=" + encoded + "]" - } + segmentContent = "[CQ:markdown,data=base64://" + encoded + "]" } } else { - mylog.Printf("Error marshaling markdown segment to interface,contain type but data is nil.") + mylog.Printf("Error marshaling markdown segment wrong type.") } } From d57b61a0098d8eabbf28a1772e2c077e2621e34e Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Sat, 15 Jun 2024 20:06:37 +0800 Subject: [PATCH 02/13] Beta431 (#430) * beta430 * beta431 --- handlers/message_parser.go | 124 ++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 58 deletions(-) diff --git a/handlers/message_parser.go b/handlers/message_parser.go index 0f19180b..09a45d47 100644 --- a/handlers/message_parser.go +++ b/handlers/message_parser.go @@ -451,41 +451,45 @@ func parseMessageContent(paramsMessage callapi.ParamsContent, message callapi.Ac qqNumber, _ := segmentMap["data"].(map[string]interface{})["qq"].(string) segmentContent = "[CQ:at,qq=" + qqNumber + "]" case "markdown": - mdContent := segmentMap["data"] - if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { - // mdContent是map[string]interface{},按map处理 - mdContentBytes, err := json.Marshal(mdContentMap) - if err != nil { - mylog.Printf("Error marshaling mdContentMap to JSON:%v", err) - } - encoded := base64.StdEncoding.EncodeToString(mdContentBytes) - segmentContent = "[CQ:markdown,data=base64://" + encoded + "]" - } else if mdContentStr, isString := mdContent.(string); isString { - // mdContent是string - if strings.HasPrefix(mdContentStr, "base64://") { - // 如果以base64://开头,直接使用 - segmentContent = "[CQ:markdown,data=" + mdContentStr + "]" - } else { - // 处理实体化后的JSON文本 - mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") - mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") - mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") - mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") - - // 将处理过的字符串视为JSON对象,进行序列化和编码 - var jsonMap map[string]interface{} - if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { - mylog.Printf("Error unmarshaling string to JSON:%v", err) - } - mdContentBytes, err := json.Marshal(jsonMap) + mdContent, ok := segmentMap["data"].(map[string]interface{})["data"] + if ok { + if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { + // mdContent是map[string]interface{},按map处理 + mdContentBytes, err := json.Marshal(mdContentMap) if err != nil { - mylog.Printf("Error marshaling jsonMap to JSON:%v", err) + mylog.Printf("Error marshaling mdContentMap to JSON:%v", err) } encoded := base64.StdEncoding.EncodeToString(mdContentBytes) segmentContent = "[CQ:markdown,data=base64://" + encoded + "]" + } else if mdContentStr, isString := mdContent.(string); isString { + // mdContent是string + if strings.HasPrefix(mdContentStr, "base64://") { + // 如果以base64://开头,直接使用 + segmentContent = "[CQ:markdown,data=" + mdContentStr + "]" + } else { + // 处理实体化后的JSON文本 + mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") + mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") + mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") + mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") + + // 将处理过的字符串视为JSON对象,进行序列化和编码 + var jsonMap map[string]interface{} + if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { + mylog.Printf("Error unmarshaling string to JSON:%v", err) + } + mdContentBytes, err := json.Marshal(jsonMap) + if err != nil { + mylog.Printf("Error marshaling jsonMap to JSON:%v", err) + } + encoded := base64.StdEncoding.EncodeToString(mdContentBytes) + segmentContent = "[CQ:markdown,data=base64://" + encoded + "]" + } + } else { + mylog.Printf("Error marshaling markdown segment wrong type.") } } else { - mylog.Printf("Error marshaling markdown segment wrong type.") + mylog.Printf("Error marshaling markdown segment to interface,contain type but data is nil.") } } @@ -515,41 +519,45 @@ func parseMessageContent(paramsMessage callapi.ParamsContent, message callapi.Ac qqNumber, _ := message["data"].(map[string]interface{})["qq"].(string) messageText = "[CQ:at,qq=" + qqNumber + "]" case "markdown": - mdContent := message["data"] - if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { - // mdContent是map[string]interface{},按map处理 - mdContentBytes, err := json.Marshal(mdContentMap) - if err != nil { - mylog.Printf("Error marshaling mdContentMap to JSON:%v", err) - } - encoded := base64.StdEncoding.EncodeToString(mdContentBytes) - messageText = "[CQ:markdown,data=base64://" + encoded + "]" - } else if mdContentStr, isString := mdContent.(string); isString { - // mdContent是string - if strings.HasPrefix(mdContentStr, "base64://") { - // 如果以base64://开头,直接使用 - messageText = "[CQ:markdown,data=" + mdContentStr + "]" - } else { - // 处理实体化后的JSON文本 - mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") - mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") - mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") - mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") - - // 将处理过的字符串视为JSON对象,进行序列化和编码 - var jsonMap map[string]interface{} - if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { - mylog.Printf("Error unmarshaling string to JSON:%v", err) - } - mdContentBytes, err := json.Marshal(jsonMap) + mdContent, ok := message["data"].(map[string]interface{})["data"] + if ok { + if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { + // mdContent是map[string]interface{},按map处理 + mdContentBytes, err := json.Marshal(mdContentMap) if err != nil { - mylog.Printf("Error marshaling jsonMap to JSON:%v", err) + mylog.Printf("Error marshaling mdContentMap to JSON:%v", err) } encoded := base64.StdEncoding.EncodeToString(mdContentBytes) messageText = "[CQ:markdown,data=base64://" + encoded + "]" + } else if mdContentStr, isString := mdContent.(string); isString { + // mdContent是string + if strings.HasPrefix(mdContentStr, "base64://") { + // 如果以base64://开头,直接使用 + messageText = "[CQ:markdown,data=" + mdContentStr + "]" + } else { + // 处理实体化后的JSON文本 + mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") + mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") + mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") + mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") + + // 将处理过的字符串视为JSON对象,进行序列化和编码 + var jsonMap map[string]interface{} + if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { + mylog.Printf("Error unmarshaling string to JSON:%v", err) + } + mdContentBytes, err := json.Marshal(jsonMap) + if err != nil { + mylog.Printf("Error marshaling jsonMap to JSON:%v", err) + } + encoded := base64.StdEncoding.EncodeToString(mdContentBytes) + messageText = "[CQ:markdown,data=base64://" + encoded + "]" + } + } else { + mylog.Printf("Error marshaling mdContent wrong type.") } } else { - mylog.Printf("Error marshaling mdContent wrong type.") + mylog.Printf("Error marshaling markdown segment to interface,contain type but data is nil.") } } default: From 68fbb1967eed285fc8409f4599ab16ef0dac6fb6 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Sat, 15 Jun 2024 23:49:33 +0800 Subject: [PATCH 03/13] Beta432 (#432) * beta430 * beta431 * beta432 --- handlers/message_parser.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/handlers/message_parser.go b/handlers/message_parser.go index 09a45d47..a3562074 100644 --- a/handlers/message_parser.go +++ b/handlers/message_parser.go @@ -1377,6 +1377,11 @@ func parseMDData(mdData []byte) (*dto.Markdown, *keyboard.MessageKeyboard, error kb = &keyboard.MessageKeyboard{ Content: &keyboard.CustomKeyboard{Rows: temp.Rows}, } + } else if temp.Keyboard.ID != "" { + // 处理嵌套在 Keyboard 中的 ID(当使用按钮模板时) + kb = &keyboard.MessageKeyboard{ + ID: temp.Keyboard.ID, + } } return md, kb, nil From 6ffb264fb91ef2443fb605de369cd50d76be83f4 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Wed, 19 Jun 2024 16:52:13 +0800 Subject: [PATCH 04/13] Beta433 (#433) * beta430 * beta431 * beta432 * beta433 --- handlers/get_friend_list.go | 2 +- handlers/get_group_list.go | 4 ++-- handlers/send_group_msg.go | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/handlers/get_friend_list.go b/handlers/get_friend_list.go index d11a6370..b3b344b1 100644 --- a/handlers/get_friend_list.go +++ b/handlers/get_friend_list.go @@ -56,6 +56,6 @@ func HandleGetFriendList(client callapi.Client, api openapi.OpenAPI, apiv2 opena return "", nil } - mylog.Printf("get_friend_list: %s", result) + //mylog.Printf("get_friend_list: %s", result) return string(result), nil } diff --git a/handlers/get_group_list.go b/handlers/get_group_list.go index 712c5b0c..6c43590d 100644 --- a/handlers/get_group_list.go +++ b/handlers/get_group_list.go @@ -159,7 +159,7 @@ func GetGroupList(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Open } outputMap := structToMap(groupList) - mylog.Printf("getGroupList(频道): %+v\n", outputMap) + //mylog.Printf("getGroupList(频道): %+v\n", outputMap) err = client.SendMessage(outputMap) if err != nil { @@ -172,7 +172,7 @@ func GetGroupList(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Open return "", nil } - mylog.Printf("get_group_list: %s", result) + //mylog.Printf("get_group_list: %s", result) return string(result), nil } diff --git a/handlers/send_group_msg.go b/handlers/send_group_msg.go index dad01f2d..a2b8db3c 100644 --- a/handlers/send_group_msg.go +++ b/handlers/send_group_msg.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "fmt" "io" + "math/rand" "net/http" "os" "strconv" @@ -1591,6 +1592,20 @@ func auto_md(message callapi.ActionMessage, messageText string, richMediaMessage Type: 2, // 所有人可操作 } } + case strings.HasPrefix(whiteLabel, "$"): + // 只有5%的概率执行以下代码 + if rand.Float64() < 0.05 { + // 分割whiteLabel来获取显示内容和URL + parts := strings.SplitN(whiteLabel[1:], " ", 2) // [1:] 用于去除白名单标签开头的'%' + if len(parts) == 2 { + whiteLabel = parts[0] // 显示内容 + actiondata = parts[1] // URL + actiontype = 0 // 链接类型 + permission = &keyboard.Permission{ + Type: 2, // 所有人可操作 + } + } + } case strings.HasPrefix(whiteLabel, "^"): // 分割whiteLabel来获取显示内容和URL parts := strings.SplitN(whiteLabel[1:], " ", 2) // [1:] 用于去除白名单标签开头的'^' From c927456a99c2f89ee5bfc4892f2fa0097d9597be Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:56:37 +0800 Subject: [PATCH 05/13] Beta434 (#434) * beta430 * beta431 * beta432 * beta433 * beta434 --- handlers/get_avatar.go | 73 ++++++++++++++++++++++++++++++ handlers/get_login_info.go | 7 ++- handlers/send_group_msg.go | 53 ++++++++++++---------- handlers/send_guild_channel_msg.go | 4 +- httpapi/httpapi.go | 73 ++++++++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 handlers/get_avatar.go diff --git a/handlers/get_avatar.go b/handlers/get_avatar.go new file mode 100644 index 00000000..925cef4e --- /dev/null +++ b/handlers/get_avatar.go @@ -0,0 +1,73 @@ +package handlers + +import ( + "encoding/json" + "fmt" + + "github.com/hoshinonyaruko/gensokyo/callapi" + "github.com/hoshinonyaruko/gensokyo/config" + "github.com/hoshinonyaruko/gensokyo/idmap" + "github.com/hoshinonyaruko/gensokyo/mylog" + "github.com/tencent-connect/botgo/openapi" +) + +type GetAvatarResponse struct { + Message string `json:"message"` + RetCode int `json:"retcode"` + Echo interface{} `json:"echo"` +} + +func init() { + callapi.RegisterHandler("get_avatar", GetStatus) +} + +func GetAvatar(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) (string, error) { + + var response GetAvatarResponse + var originalUserID string + var err error + + if config.GetIdmapPro() { + // 如果UserID不是nil且配置为使用Pro版本,则调用RetrieveRowByIDv2Pro + _, originalUserID, err = idmap.RetrieveRowByIDv2Pro(message.Params.GroupID.(string), message.Params.UserID.(string)) + if err != nil { + mylog.Printf("Error1 retrieving original GroupID: %v", err) + } + } else { + originalUserID, err = idmap.RetrieveRowByIDv2(message.Params.UserID.(string)) + if err != nil { + mylog.Printf("Error retrieving original UserID: %v", err) + } + } + + avatarurl, _ := GenerateAvatarURLV2(originalUserID) + + response.Message = avatarurl + response.RetCode = 0 + response.Echo = message.Echo + + outputMap := structToMap(response) + + mylog.Printf("get_avatar: %+v\n", outputMap) + + err = client.SendMessage(outputMap) + if err != nil { + mylog.Printf("Error sending message via client: %v", err) + } + //把结果从struct转换为json + result, err := json.Marshal(response) + if err != nil { + mylog.Printf("Error marshaling data: %v", err) + //todo 符合onebotv11 ws返回的错误码 + return "", nil + } + return string(result), nil +} + +// GenerateAvatarURLV2 生成根据32位ID 和 Appid 组合的 新QQ 头像 URL +func GenerateAvatarURLV2(openid string) (string, error) { + + appidstr := config.GetAppIDStr() + // 构建并返回 URL + return fmt.Sprintf("https://q.qlogo.cn/qqapp/%s/%s/640", appidstr, openid), nil +} diff --git a/handlers/get_login_info.go b/handlers/get_login_info.go index a2a4362d..7a9dcf92 100644 --- a/handlers/get_login_info.go +++ b/handlers/get_login_info.go @@ -2,7 +2,6 @@ package handlers import ( "encoding/json" - "fmt" "github.com/hoshinonyaruko/gensokyo/callapi" "github.com/hoshinonyaruko/gensokyo/config" @@ -20,7 +19,7 @@ type LoginInfoResponse struct { type LoginInfoData struct { Nickname string `json:"nickname"` - UserID string `json:"user_id"` // Assuming UserID is a string type based on the pseudocode + UserID int64 `json:"user_id"` // Assuming UserID is a string type based on the pseudocode } func init() { @@ -40,12 +39,12 @@ func GetLoginInfo(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Open globalBotID = config.GetAppID() } - userIDStr := fmt.Sprintf("%d", globalBotID) + //userIDStr := fmt.Sprintf("%d", globalBotID) botname = config.GetCustomBotName() response.Data = LoginInfoData{ Nickname: botname, - UserID: userIDStr, + UserID: int64(globalBotID), } response.Message = "" response.RetCode = 0 diff --git a/handlers/send_group_msg.go b/handlers/send_group_msg.go index a2b8db3c..8d5608c8 100644 --- a/handlers/send_group_msg.go +++ b/handlers/send_group_msg.go @@ -1540,6 +1540,7 @@ func auto_md(message callapi.ActionMessage, messageText string, richMediaMessage var actiontype keyboard.ActionType var permission *keyboard.Permission var actiondata string + var skip bool //检查是否设置了enter数组 enter := checkDataLabelPrefix(dataLabel) //例外规则 @@ -1605,6 +1606,8 @@ func auto_md(message callapi.ActionMessage, messageText string, richMediaMessage Type: 2, // 所有人可操作 } } + } else { + skip = true } case strings.HasPrefix(whiteLabel, "^"): // 分割whiteLabel来获取显示内容和URL @@ -1625,32 +1628,34 @@ func auto_md(message callapi.ActionMessage, messageText string, richMediaMessage } } - // 创建按钮 - button := &keyboard.Button{ - RenderData: &keyboard.RenderData{ - Label: whiteLabel, - VisitedLabel: whiteLabel, - Style: 1, //蓝色边缘 - }, - Action: &keyboard.Action{ - Type: actiontype, - Permission: permission, - Data: actiondata, - UnsupportTips: "请升级新版手机QQ", - Enter: enter, - }, - } + if !skip { + // 创建按钮 + button := &keyboard.Button{ + RenderData: &keyboard.RenderData{ + Label: whiteLabel, + VisitedLabel: whiteLabel, + Style: 1, //蓝色边缘 + }, + Action: &keyboard.Action{ + Type: actiontype, + Permission: permission, + Data: actiondata, + UnsupportTips: "请升级新版手机QQ", + Enter: enter, + }, + } - // 如果当前行为空或已满(4个按钮),则创建一个新行 - if currentRow == nil || buttonCount == 4 { - currentRow = &keyboard.Row{} - customKeyboard.Rows = append(customKeyboard.Rows, currentRow) - buttonCount = 0 // 重置按钮计数 - } + // 如果当前行为空或已满(4个按钮),则创建一个新行 + if currentRow == nil || buttonCount == 4 { + currentRow = &keyboard.Row{} + customKeyboard.Rows = append(customKeyboard.Rows, currentRow) + buttonCount = 0 // 重置按钮计数 + } - // 将按钮添加到当前行 - currentRow.Buttons = append(currentRow.Buttons, button) - buttonCount++ + // 将按钮添加到当前行 + currentRow.Buttons = append(currentRow.Buttons, button) + buttonCount++ + } } // 在循环结束后,最后一行可能不满4个按钮,但已经被正确处理 diff --git a/handlers/send_guild_channel_msg.go b/handlers/send_guild_channel_msg.go index aeb263a9..5975b293 100644 --- a/handlers/send_guild_channel_msg.go +++ b/handlers/send_guild_channel_msg.go @@ -3,7 +3,7 @@ package handlers import ( "context" "encoding/base64" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -463,7 +463,7 @@ func downloadImageAndConvertToBase64(url string) (string, error) { defer resp.Body.Close() // 读取响应的内容 - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) if err != nil { return "", err // 返回错误 } diff --git a/httpapi/httpapi.go b/httpapi/httpapi.go index ec562303..9f536cb0 100644 --- a/httpapi/httpapi.go +++ b/httpapi/httpapi.go @@ -66,6 +66,14 @@ func CombinedMiddleware(api openapi.OpenAPI, apiV2 openapi.OpenAPI) gin.HandlerF handleDeleteMsg(c, api, apiV2) return } + if c.Request.URL.Path == "/get_avatar" { + handleGetAvatar(c, api, apiV2) + return + } + if c.Request.URL.Path == "/get_login_info" { + handleGetLoginInfo(c, api, apiV2) + return + } // 调用c.Next()以继续处理请求链 c.Next() @@ -577,3 +585,68 @@ func handleDeleteMsg(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) } } + +// handleGetAvatar 处理get_avatar的请求 +func handleGetAvatar(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) { + var req struct { + Echo string `json:"echo" form:"echo"` // Echo值用于标识消息 + GroupID int64 `json:"group_id" form:"group_id"` + UserID int64 `json:"user_id" form:"user_id"` + } + + // 根据请求方法解析参数 + if c.Request.Method == http.MethodGet { + // 从URL查询参数解析 + if err := c.ShouldBindQuery(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + } else { + // 从JSON或表单数据解析 + if err := c.ShouldBind(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + } + + // 创建 ActionMessage 实例 + message := callapi.ActionMessage{ + Action: "get_avatar", + Echo: req.Echo, + Params: callapi.ParamsContent{ + GroupID: strconv.FormatInt(req.GroupID, 10), // 注意这里需要转换类型,因为 GroupID 是 int64 + UserID: strconv.FormatInt(req.UserID, 10), + }, + } + + // 调用处理函数 + client := &HttpAPIClient{} // 假设HttpAPIClient实现了callapi.Client接口 + retmsg, err := handlers.GetAvatar(client, api, apiV2, message) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + // 返回处理结果 + c.Header("Content-Type", "application/json") + c.String(http.StatusOK, retmsg) +} + +func handleGetLoginInfo(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) { + // 创建 ActionMessage 实例 + message := callapi.ActionMessage{ + Action: "get_login_info", + } + + // 调用处理函数 + client := &HttpAPIClient{} // 假设HttpAPIClient实现了callapi.Client接口 + retmsg, err := handlers.GetLoginInfo(client, api, apiV2, message) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + // 返回处理结果 + c.Header("Content-Type", "application/json") + c.String(http.StatusOK, retmsg) +} From 2f1950cab4ea8618a26b5371ec855488083c6312 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:07:33 +0800 Subject: [PATCH 06/13] Beta435 (#435) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 --- handlers/get_avatar.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/handlers/get_avatar.go b/handlers/get_avatar.go index 925cef4e..37606505 100644 --- a/handlers/get_avatar.go +++ b/handlers/get_avatar.go @@ -32,6 +32,10 @@ func GetAvatar(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI _, originalUserID, err = idmap.RetrieveRowByIDv2Pro(message.Params.GroupID.(string), message.Params.UserID.(string)) if err != nil { mylog.Printf("Error1 retrieving original GroupID: %v", err) + _, originalUserID, err = idmap.RetrieveRowByIDv2Pro("690426430", message.Params.UserID.(string)) + if err != nil { + mylog.Printf("Error reading private originalUserID: %v", err) + } } } else { originalUserID, err = idmap.RetrieveRowByIDv2(message.Params.UserID.(string)) From 0c7dca9a714a69530ec30a95947281231190a964 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Sun, 23 Jun 2024 17:56:23 +0800 Subject: [PATCH 07/13] Beta436 (#436) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 * beta436 --- handlers/get_avatar.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/get_avatar.go b/handlers/get_avatar.go index 37606505..edfabfaf 100644 --- a/handlers/get_avatar.go +++ b/handlers/get_avatar.go @@ -18,7 +18,7 @@ type GetAvatarResponse struct { } func init() { - callapi.RegisterHandler("get_avatar", GetStatus) + callapi.RegisterHandler("get_avatar", GetAvatar) } func GetAvatar(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) (string, error) { From 6c22ab30a34ae34945dd6f4eb0b7c43082011b9d Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Wed, 26 Jun 2024 00:25:56 +0800 Subject: [PATCH 08/13] Beta437 (#437) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 * beta436 * beta437 --- idmap/service.go | 33 +++++++++++++++++++++++++++++++++ main.go | 4 ++++ 2 files changed, 37 insertions(+) diff --git a/idmap/service.go b/idmap/service.go index 8bba4d9a..6b9334a7 100644 --- a/idmap/service.go +++ b/idmap/service.go @@ -71,6 +71,39 @@ func InitializeDB() { } } +func ClearBucket(bucketName string) { + // 打开数据库文件 + db, err := bbolt.Open(DBName, 0600, nil) + if err != nil { + log.Fatalf("Error opening DB: %v", err) + } + defer db.Close() + + // 清空指定的bucket + err = db.Update(func(tx *bbolt.Tx) error { + // 获取指定的bucket + bucket := tx.Bucket([]byte(bucketName)) + if bucket == nil { + return nil // 如果bucket不存在,直接返回nil + } + + // 删除bucket中的所有键值对 + err := bucket.ForEach(func(k, v []byte) error { + return bucket.Delete(k) + }) + if err != nil { + return err + } + return nil + }) + + if err != nil { + log.Fatalf("Error clearing bucket %s: %v", bucketName, err) + } else { + mylog.Printf("ids清理成功.") + } +} + func CloseDB() { db.Close() } diff --git a/main.go b/main.go index 48433c61..ddd5d109 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ func main() { // 定义faststart命令行标志。默认为false。 fastStart := flag.Bool("faststart", false, "start without initialization if set") tidy := flag.Bool("tidy", false, "backup and tidy your config.yml") + c := flag.Bool("c", false, "clean ids bucket, must backup idmap.db first!") m := flag.Bool("m", false, "Maintenance mode") // 解析命令行参数到定义的标志。 @@ -63,6 +64,9 @@ func main() { log.Println("配置文件已更新为新版,当前配置文件已备份.如产生问题请到群196173384反馈开发者。") return } + if *c { + idmap.ClearBucket("ids") + } if _, err := os.Stat("config.yml"); os.IsNotExist(err) { var ip string var err error From 5741b2e774a3625aa6bff070b716339f5fccd593 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Wed, 26 Jun 2024 19:14:10 +0800 Subject: [PATCH 09/13] Beta438 (#438) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 * beta436 * beta437 * beta438 --- main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.go b/main.go index ddd5d109..d8dd45cc 100644 --- a/main.go +++ b/main.go @@ -208,6 +208,12 @@ func main() { defer idmap.CloseDB() defer botstats.CloseDB() + if *c { + mylog.Printf("开始清理ids\n") + idmap.ClearBucket("ids") + mylog.Printf("ids清理完成\n") + } + if configURL == "" && !fix11300 { //初始化handlers handlers.BotID = me.ID } else { //初始化handlers From 41338ccbdf957c877badf34fe9d35dbe40a3845a Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:09:28 +0800 Subject: [PATCH 10/13] Update main.go (#439) --- main.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/main.go b/main.go index d8dd45cc..2387ef5a 100644 --- a/main.go +++ b/main.go @@ -64,9 +64,6 @@ func main() { log.Println("配置文件已更新为新版,当前配置文件已备份.如产生问题请到群196173384反馈开发者。") return } - if *c { - idmap.ClearBucket("ids") - } if _, err := os.Stat("config.yml"); os.IsNotExist(err) { var ip string var err error From 515c2ccc436ed7df512ef506cd7b4e631d602801 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Thu, 27 Jun 2024 21:10:38 +0800 Subject: [PATCH 11/13] Beta440 (#440) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 * beta436 * beta437 * beta438 * beta440 --- idmap/service.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/idmap/service.go b/idmap/service.go index 6b9334a7..bca6c94a 100644 --- a/idmap/service.go +++ b/idmap/service.go @@ -72,15 +72,8 @@ func InitializeDB() { } func ClearBucket(bucketName string) { - // 打开数据库文件 - db, err := bbolt.Open(DBName, 0600, nil) - if err != nil { - log.Fatalf("Error opening DB: %v", err) - } - defer db.Close() - // 清空指定的bucket - err = db.Update(func(tx *bbolt.Tx) error { + err := db.Update(func(tx *bbolt.Tx) error { // 获取指定的bucket bucket := tx.Bucket([]byte(bucketName)) if bucket == nil { From 886e79772aa354c720c95d31ad6c36d63f4f349f Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:21:35 +0800 Subject: [PATCH 12/13] Beta441 (#441) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 * beta436 * beta437 * beta438 * beta440 * beta441 --- Processor/ProcessGroupAddBot.go | 10 ++- Processor/Processor.go | 143 +++++++++++++++++++++++--------- config/config.go | 25 ++++++ handlers/send_group_msg.go | 6 ++ idmap/service.go | 50 +++++++++++ main.go | 1 + structs/structs.go | 2 + template/config_template.go | 4 +- 8 files changed, 200 insertions(+), 41 deletions(-) diff --git a/Processor/ProcessGroupAddBot.go b/Processor/ProcessGroupAddBot.go index 4bf65c5a..912b0ed8 100644 --- a/Processor/ProcessGroupAddBot.go +++ b/Processor/ProcessGroupAddBot.go @@ -159,7 +159,7 @@ func (p *Processors) ProcessGroupAddBot(data *dto.GroupAddBotEvent) error { // 创建 ActionMessage 实例 message := callapi.ActionMessage{ - Action: "send_group_msg", + Action: "send_group_msg_group", Params: callapi.ParamsContent{ GroupID: strconv.FormatInt(GroupID64, 10), // 转换 GroupID 类型 UserID: strconv.FormatInt(userid64, 10), @@ -172,7 +172,13 @@ func (p *Processors) ProcessGroupAddBot(data *dto.GroupAddBotEvent) error { _, err = handlers.HandleSendGroupMsg(client, p.Api, p.Apiv2, message) if err != nil { mylog.Printf("自我介绍发送失败%v", err) - return nil } + + //link指令 + if config.GetAutoLink() { + md, kb := generateMdByConfig() + SendMessageMdAddBot(md, kb, data, p.Api, p.Apiv2) + } + return nil } diff --git a/Processor/Processor.go b/Processor/Processor.go index c10db3fb..12af8330 100644 --- a/Processor/Processor.go +++ b/Processor/Processor.go @@ -928,6 +928,31 @@ func SendMessageMd(md *dto.Markdown, kb *keyboard.MessageKeyboard, data interfac return nil } +// SendMessageMdAddBot 发送Md消息在AddBot事件 +func SendMessageMdAddBot(md *dto.Markdown, kb *keyboard.MessageKeyboard, data *dto.GroupAddBotEvent, api openapi.OpenAPI, apiv2 openapi.OpenAPI) error { + + // 处理群组消息 + msgseq := echo.GetMappingSeq(data.EventID) + echo.AddMappingSeq(data.ID, msgseq+1) + Message := &dto.MessageToCreate{ + Content: "markdown", + EventID: data.EventID, + MsgSeq: msgseq, + Markdown: md, + Keyboard: kb, + MsgType: 2, //md信息 + } + + Message.Timestamp = time.Now().Unix() // 设置时间戳 + _, err := apiv2.PostGroupMessage(context.TODO(), data.GroupOpenID, Message) + if err != nil { + mylog.Printf("发送文本群组信息失败: %v", err) + return err + } + + return nil +} + // autobind 函数接受 interface{} 类型的数据 // commit by 紫夜 2023-11-19 func (p *Processors) Autobind(data interface{}) error { @@ -1084,30 +1109,50 @@ func generateMdByConfig() (md *dto.Markdown, kb *keyboard.MessageKeyboard) { linkBots = getRandomSelection(linkBots, 16) } - //组合 mdParams var mdParams []*dto.MarkdownParams - if imgURL != "" { - height, width, err := images.GetImageDimensions(imgURL) - if err != nil { - mylog.Printf("获取图片宽高出错") + if !config.GetNativeMD() { + //组合 mdParams + if imgURL != "" { + height, width, err := images.GetImageDimensions(imgURL) + if err != nil { + mylog.Printf("获取图片宽高出错") + } + imgDesc := fmt.Sprintf("图片 #%dpx #%dpx", width, height) + // 创建 MarkdownParams 的实例 + mdParams = []*dto.MarkdownParams{ + {Key: "img_dec", Values: []string{imgDesc}}, + {Key: "img_url", Values: []string{imgURL}}, + {Key: "text_end", Values: []string{mdtext}}, + } + } else { + mdParams = []*dto.MarkdownParams{ + {Key: "text_end", Values: []string{mdtext}}, + } } - imgDesc := fmt.Sprintf("图片 #%dpx #%dpx", width, height) - // 创建 MarkdownParams 的实例 - mdParams = []*dto.MarkdownParams{ - {Key: "img_dec", Values: []string{imgDesc}}, - {Key: "img_url", Values: []string{imgURL}}, - {Key: "text_end", Values: []string{mdtext}}, + + // 组合模板 Markdown + md = &dto.Markdown{ + CustomTemplateID: CustomTemplateID, + Params: mdParams, } } else { - mdParams = []*dto.MarkdownParams{ - {Key: "text_end", Values: []string{mdtext}}, + // 使用原生Markdown格式 + var content string + if imgURL != "" { + height, width, err := images.GetImageDimensions(imgURL) + if err != nil { + mylog.Printf("获取图片宽高出错") + } + imgDesc := fmt.Sprintf("图片 #%dpx #%dpx", width, height) + content = fmt.Sprintf("![%s](%s)\n%s", imgDesc, imgURL, mdtext) + } else { + content = mdtext } - } - // 组合模板 Markdown - md = &dto.Markdown{ - CustomTemplateID: CustomTemplateID, - Params: mdParams, + // 原生 Markdown + md = &dto.Markdown{ + Content: content, + } } // 创建自定义键盘 @@ -1117,30 +1162,52 @@ func generateMdByConfig() (md *dto.Markdown, kb *keyboard.MessageKeyboard) { for _, bot := range linkBots { parts := strings.SplitN(bot, "-", 3) - if len(parts) < 3 { + if len(parts) != 3 && len(parts) != 2 { continue // 跳过无效的格式 } - name := parts[2] - botuin := parts[1] - botappid := parts[0] - boturl := handlers.BuildQQBotShareLink(botuin, botappid) - - button := &keyboard.Button{ - RenderData: &keyboard.RenderData{ - Label: name, - VisitedLabel: name, - Style: 1, // 蓝色边缘 - }, - Action: &keyboard.Action{ - Type: 0, // 链接类型 - Permission: &keyboard.Permission{Type: 2}, // 所有人可操作 - Data: boturl, - UnsupportTips: "请升级新版手机QQ", - }, + var button *keyboard.Button + if len(parts) == 3 { + name := parts[2] + botuin := parts[1] + botappid := parts[0] + boturl := handlers.BuildQQBotShareLink(botuin, botappid) + + button = &keyboard.Button{ + RenderData: &keyboard.RenderData{ + Label: name, + VisitedLabel: name, + Style: 1, // 蓝色边缘 + }, + Action: &keyboard.Action{ + Type: 0, // 链接类型 + Permission: &keyboard.Permission{Type: 2}, // 所有人可操作 + Data: boturl, + UnsupportTips: "请升级新版手机QQ", + }, + } + } else if len(parts) == 2 { + boturl := parts[0] + name := parts[1] + + button = &keyboard.Button{ + RenderData: &keyboard.RenderData{ + Label: name, + VisitedLabel: name, + Style: 1, // 蓝色边缘 + }, + Action: &keyboard.Action{ + Type: 0, // 链接类型 + Permission: &keyboard.Permission{Type: 2}, // 所有人可操作 + Data: boturl, + UnsupportTips: "请升级新版手机QQ", + }, + } } - // 如果当前行为空或已满(4个按钮),则创建一个新行 - if currentRow == nil || buttonCount == 4 { + lines := config.GetLinkLines() + + // 如果当前行为空或已满(lines个按钮),则创建一个新行 + if currentRow == nil || buttonCount == lines { currentRow = &keyboard.Row{} customKeyboard.Rows = append(customKeyboard.Rows, currentRow) buttonCount = 0 diff --git a/config/config.go b/config/config.go index 79f57b77..32930042 100644 --- a/config/config.go +++ b/config/config.go @@ -2302,3 +2302,28 @@ func GetDowntimeMessage() string { } return "" } + +// 获取GetAutoLink的值 +func GetAutoLink() bool { + mu.Lock() + defer mu.Unlock() + + if instance == nil { + mylog.Println("Warning: instance is nil when trying to AutoLink value.") + return false + } + return instance.Settings.AutoLink +} + +// 获取GetLinkLines的值 +func GetLinkLines() int { + mu.Lock() + defer mu.Unlock() + + if instance == nil { + mylog.Println("Warning: instance is nil when trying to LinkLines value.") + return 2 //默认2个一行 + } + + return instance.Settings.LinkLines +} diff --git a/handlers/send_group_msg.go b/handlers/send_group_msg.go index 8d5608c8..1ad8256b 100644 --- a/handlers/send_group_msg.go +++ b/handlers/send_group_msg.go @@ -84,6 +84,12 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap mylog.Printf("send_group_msgs接收到错误action: %v", message) return "", nil } + + // 内部逻辑 ProcessGroupAddBot.go 中定义的 通过http和ws无法触发 锁定类型 + if message.Action == "send_group_msg_group" { + msgType = "group" + } + mylog.Printf("send_group_msg获取到信息类型:%v", msgType) var idInt64 int64 var err error diff --git a/idmap/service.go b/idmap/service.go index bca6c94a..d6029185 100644 --- a/idmap/service.go +++ b/idmap/service.go @@ -15,6 +15,7 @@ import ( "strconv" "strings" "sync" + "time" "github.com/hoshinonyaruko/gensokyo/config" "github.com/hoshinonyaruko/gensokyo/mylog" @@ -77,6 +78,7 @@ func ClearBucket(bucketName string) { // 获取指定的bucket bucket := tx.Bucket([]byte(bucketName)) if bucket == nil { + mylog.Printf("ids表不存在.") return nil // 如果bucket不存在,直接返回nil } @@ -94,9 +96,57 @@ func ClearBucket(bucketName string) { log.Fatalf("Error clearing bucket %s: %v", bucketName, err) } else { mylog.Printf("ids清理成功.") + err := Compaction("idmap.db", "idmap_compacted.db") + if err != nil { + log.Fatalf("Failed to compact database: %v", err) + } else { + log.Println("Database compaction successful.") + // 可选:替换旧数据库文件 + // os.Remove("idmap.db") + // os.Rename("idmap_compacted.db", "idmap.db") + log.Println("请手动备份原始idmap.db(可选)并将idmap_compacted.db改名为idmap.db") + } } } +// Compaction 创建一个新的数据库文件并复制现有的数据到这个新文件中 +func Compaction(sourceDBPath, targetDBPath string) error { + // 创建目标数据库文件 + targetDB, err := bbolt.Open(targetDBPath, 0600, &bbolt.Options{Timeout: 1 * time.Second}) + if err != nil { + return err + } + defer targetDB.Close() + + // 从源数据库复制数据到目标数据库 + err = db.View(func(tx *bbolt.Tx) error { + return tx.ForEach(func(name []byte, b *bbolt.Bucket) error { + // 在目标数据库中创建相同的bucket + return targetDB.Update(func(tx2 *bbolt.Tx) error { + bucket, err := tx2.CreateBucketIfNotExists(name) + if err != nil { + return err + } + // 复制所有键值对 + return b.ForEach(func(k, v []byte) error { + return bucket.Put(k, v) + }) + }) + }) + }) + + if err != nil { + return err + } + + // 确保所有操作都已完成 + if err := targetDB.Sync(); err != nil { + return err + } + + return nil +} + func CloseDB() { db.Close() } diff --git a/main.go b/main.go index 2387ef5a..ac6d724b 100644 --- a/main.go +++ b/main.go @@ -209,6 +209,7 @@ func main() { mylog.Printf("开始清理ids\n") idmap.ClearBucket("ids") mylog.Printf("ids清理完成\n") + return } if configURL == "" && !fix11300 { //初始化handlers diff --git a/structs/structs.go b/structs/structs.go index 29c7c725..5c1bfa09 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -148,10 +148,12 @@ type Settings struct { MePrefix string `yaml:"me_prefix"` UnlockPrefix string `yaml:"unlock_prefix"` LinkPrefix string `yaml:"link_prefix"` + AutoLink bool `yaml:"auto_link"` MusicPrefix string `yaml:"music_prefix"` LinkBots []string `yaml:"link_bots"` LinkText string `yaml:"link_text"` LinkPic string `yaml:"link_pic"` + LinkLines int `yaml:"link_lines"` //HTTP API配置 HttpAddress string `yaml:"http_address"` AccessToken string `yaml:"http_access_token"` diff --git a/template/config_template.go b/template/config_template.go index 48863c48..cc137df2 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -190,10 +190,12 @@ settings: me_prefix : "/me" #需设置 #增强配置项 master_id 可触发 unlock_prefix : "/unlock" #频道私信卡住了? gsk可以帮到你 在任意子频道发送unlock 你会收到来自机器人的频道私信 link_prefix : "/link" #友情链接配置 配置custom_template_id后可用(https://www.yuque.com/km57bt/hlhnxg/tzbr84y59dbz6pib) + auto_link : false #友情链接最高礼仪,机器人被添加到群内时发送友情链接. music_prefix : "点歌" #[CQ:music,type=qq,id=123] 在消息文本组合qq音乐歌曲id,可以发送点歌,这是歌曲按钮第二个按钮的填充内容,应为你的机器人点歌插件的指令. - link_bots : ["",""] #发送友情链接时 下方按钮携带的机器人 格式 "appid-qq-name","appid-qq-name" + link_bots : ["",""] #发送友情链接时 下方按钮携带的机器人 格式 "appid-qq-name","appid-qq-name"或"http://xxx.com-文字" 链接中的-号自行用%2D替换 如 cgi-bin替换为cgi%2Dbin link_text : "" #友情链接文本 不可为空! link_pic : "" #友情链接图片 可为空 需url图片 可带端口 不填可能会有显示错误 + link_lines : 2 #内置的/link指令按钮列数(默认一行2个按钮) #HTTP API配置-正向http http_address: "" #http监听地址 与websocket独立 示例:0.0.0.0:5700 为空代表不开启 From 0c71e32ead2d7b17c6fd236e51abd94cccca7fb5 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:39:01 +0800 Subject: [PATCH 13/13] Beta442 (#442) * beta430 * beta431 * beta432 * beta433 * beta434 * bet435 * beta436 * beta437 * beta438 * beta440 * beta441 * beta442 --- Processor/Processor.go | 8 +++++--- config/config.go | 13 +++++++++++++ structs/structs.go | 1 + template/config_template.go | 1 + 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Processor/Processor.go b/Processor/Processor.go index 12af8330..33dc5de8 100644 --- a/Processor/Processor.go +++ b/Processor/Processor.go @@ -1104,9 +1104,11 @@ func generateMdByConfig() (md *dto.Markdown, kb *keyboard.MessageKeyboard) { linkBots := config.GetLinkBots() imgURL := config.GetLinkPic() - //超过16个时候随机显示 - if len(linkBots) > 16 { - linkBots = getRandomSelection(linkBots, 16) + linknum := config.GetLinkNum() + + //超过n个时候随机显示 + if len(linkBots) > linknum { + linkBots = getRandomSelection(linkBots, linknum) } var mdParams []*dto.MarkdownParams diff --git a/config/config.go b/config/config.go index 32930042..887598e8 100644 --- a/config/config.go +++ b/config/config.go @@ -2327,3 +2327,16 @@ func GetLinkLines() int { return instance.Settings.LinkLines } + +// 获取GetLinkNum的值 +func GetLinkNum() int { + mu.Lock() + defer mu.Unlock() + + if instance == nil { + mylog.Println("Warning: instance is nil when trying to LinkNum value.") + return 6 //默认6个 + } + + return instance.Settings.LinkNum +} diff --git a/structs/structs.go b/structs/structs.go index 5c1bfa09..c524b209 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -154,6 +154,7 @@ type Settings struct { LinkText string `yaml:"link_text"` LinkPic string `yaml:"link_pic"` LinkLines int `yaml:"link_lines"` + LinkNum int `yaml:"link_num"` //HTTP API配置 HttpAddress string `yaml:"http_address"` AccessToken string `yaml:"http_access_token"` diff --git a/template/config_template.go b/template/config_template.go index cc137df2..a377af81 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -196,6 +196,7 @@ settings: link_text : "" #友情链接文本 不可为空! link_pic : "" #友情链接图片 可为空 需url图片 可带端口 不填可能会有显示错误 link_lines : 2 #内置的/link指令按钮列数(默认一行2个按钮) + link_num : 6 #在link_bots随机选n个bot上友情链接,在link_bots中随机n个,避免用户选择困难,少即是多 #HTTP API配置-正向http http_address: "" #http监听地址 与websocket独立 示例:0.0.0.0:5700 为空代表不开启