From c9d78bd832c9fd6d1255f607c57d025326173f2b Mon Sep 17 00:00:00 2001 From: LyMysterious Date: Sat, 20 Jun 2026 13:32:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=98=E5=A2=83=E7=BB=93=E7=AE=97?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/EntryController.cs | 3 +- .../Group/TextProcess/MiJingNotifyProcess.cs | 80 ++++++++++++++----- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/NapCatRobotClient/NapCatRobotClient.API/Controllers/EntryController.cs b/NapCatRobotClient/NapCatRobotClient.API/Controllers/EntryController.cs index 7fa2ee8..7590666 100644 --- a/NapCatRobotClient/NapCatRobotClient.API/Controllers/EntryController.cs +++ b/NapCatRobotClient/NapCatRobotClient.API/Controllers/EntryController.cs @@ -24,8 +24,7 @@ namespace NapCatRobotClient.API.Controllers { using StreamReader reader = new(Request.Body); string body = await reader.ReadToEndAsync(); - Log.Information($"接收到群消息:{body} \r\n"); - body = "{\"self_id\":3902582794,\"user_id\":3889001741,\"time\":1781882943,\"message_id\":650215683,\"message_seq\":650215683,\"real_id\":650215683,\"real_seq\":\"1698687\",\"message_type\":\"group\",\"sender\":{\"user_id\":3889001741,\"nickname\":\"小小\",\"card\":\"\",\"role\":\"member\"},\"raw_message\":\"[CQ:markdown,content=[](%7B%22version%22%3A2%7D)\\n道友的灵田还不能收取,下次收取时间为:44.77小时之后]道友的灵田还不能收取,下次收取时间为:44.77小时之后\",\"font\":14,\"sub_type\":\"normal\",\"message\":[{\"type\":\"markdown\",\"data\":{\"content\":\"[](%7B%22version%22%3A2%7D)\\n道友的灵田还不能收取,下次收取时间为:44.77小时之后\"}},{\"type\":\"text\",\"data\":{\"text\":\"道友的灵田还不能收取,下次收取时间为:44.77小时之后\"}}],\"message_format\":\"array\",\"post_type\":\"message\",\"group_id\":285855428,\"group_name\":\"??????颂我名者轮回得永生??????\"}"; + //Log.Information($"接收到群消息:{body} \r\n"); bool result = await _dispatcherService.ReceiveMessageAndProcess(body); return Ok(result); } diff --git a/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs b/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs index 22373f5..86457b5 100644 --- a/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs +++ b/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs @@ -1,6 +1,5 @@ using Furion.Logging; using System.Globalization; -using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource; namespace NapCatRobotClient.Service.Group.TextProcess { @@ -14,41 +13,78 @@ namespace NapCatRobotClient.Service.Group.TextProcess try { JObject json = JObject.Parse(message); - string xx = json["user_id"]?.ToString(); - if (xx != App.Configuration["QQConfig:XiaoXiaoRobotQQ"]) - { + + string userId = json["user_id"]?.ToString(); + + string robotId = App.Configuration["QQConfig:XiaoXiaoRobotQQ"]; + + if (userId != robotId) return false; - } - var messageArray = JArray.Parse(json["message"].ToString()); + if (json["message"] is not JArray messageArray || messageArray.Count == 0) + return false; - List keywords = new() { "进入秘境", "道友已踏入", "道友已破界", "道友已遁入", "道友已降临" }; + string[] keywords = { "进入秘境", "道友已踏入", "道友已破界", "道友已遁入", "道友已降临" }; - string groupMsg = messageArray - .FirstOrDefault(o => - o["type"]?.ToString() == "text" - && !string.IsNullOrWhiteSpace(o["data"]?["text"]?.ToString()) - && keywords.Any(k => o["data"]["text"].ToString().Contains(k)) - )?["data"]?["text"]?.ToString(); - if (string.IsNullOrWhiteSpace(groupMsg)) return false; + JToken textMsg = messageArray + .FirstOrDefault(o => + { + string type = o["type"]?.ToString(); + string text = o["data"]?["text"]?.ToString(); + + return type == "text" + && !string.IsNullOrWhiteSpace(text) + && keywords.Any(k => text.Contains(k)); + }); + + string groupMsg = textMsg?["data"]?["text"]?.ToString(); + if (string.IsNullOrWhiteSpace(groupMsg)) + return false; var minutes = ParseMinutes(groupMsg); - if (minutes > 0) + if (minutes <= 0) + return true; + + JToken atData = messageArray.FirstOrDefault(o => o["type"]?.ToString() == "at"); + + string targetQQ = null; + + if (atData != null) { - var atData = messageArray.FirstOrDefault(o => o["type"].ToString() == "at"); - if (atData is not null) + targetQQ = atData["data"]?["qq"]?.ToString(); + } + else + { + string markdown = messageArray + .FirstOrDefault(o => o["type"]?.ToString() == "markdown") + ?["data"]?["content"]?.ToString(); + + if (!string.IsNullOrWhiteSpace(markdown)) { - ScheduleReminder(groupId, atData["data"]["qq"].ToString(), minutes); + var match = Regex.Match(markdown, @"at_tinyid=(\d+)"); + if (match.Success) + { + targetQQ = match.Groups[1].Value; + } } } + + if (!string.IsNullOrWhiteSpace(targetQQ)) + { + ScheduleReminder(groupId, targetQQ, minutes); + } + + return true; } catch (Exception ex) { - Log.Error($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 秘境结束通知 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}", true); - } - return await Task.FromResult(true); - } + Log.Error( + $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 秘境结束通知 发生异常:{ex.Message}\n{ex.StackTrace}", + true); + return false; + } + } private static void ScheduleReminder(string groupId, string userId, double minutes) { DateTime now = DateTime.Now.AddMinutes(minutes);