From 7c6b2580d64e3ac01d1ee7950fa4c537473f8796 Mon Sep 17 00:00:00 2001 From: LyMysterious Date: Sun, 2 Nov 2025 18:01:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E7=81=B5=E7=94=B0=20?= =?UTF-8?q?=E7=A7=98=E5=A2=83=E7=BB=93=E7=AE=97=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NapCatRobotClient.API/Program.cs | 4 + .../NapCatRobotClient.API/appsettings.json | 3 +- .../NapCatRobotClient.Core/Consts.cs | 4 + .../RobotAPI/RobotAPI.cs | 22 +++-- .../BackGround/LingTianBackGroundWorker.cs | 60 ++++++++++++ .../BackGround/MijingBackGroundWorker.cs | 55 +++++++++++ .../Group/Dto/TaskDetailInfo.cs | 28 ++++++ .../Group/Service/GroupService.cs | 8 +- .../Group/TextProcess/LingTianProcess.cs | 95 ++++++++++++------- .../Group/TextProcess/MiJingNotifyProcess.cs | 40 ++------ 10 files changed, 242 insertions(+), 77 deletions(-) create mode 100644 NapCatRobotClient/NapCatRobotClient.Service/BackGround/LingTianBackGroundWorker.cs create mode 100644 NapCatRobotClient/NapCatRobotClient.Service/BackGround/MijingBackGroundWorker.cs create mode 100644 NapCatRobotClient/NapCatRobotClient.Service/Group/Dto/TaskDetailInfo.cs diff --git a/NapCatRobotClient/NapCatRobotClient.API/Program.cs b/NapCatRobotClient/NapCatRobotClient.API/Program.cs index 399e1f2..c68dde8 100644 --- a/NapCatRobotClient/NapCatRobotClient.API/Program.cs +++ b/NapCatRobotClient/NapCatRobotClient.API/Program.cs @@ -2,6 +2,7 @@ using Furion; using Microsoft.AspNetCore.HttpOverrides; using NapCatRobotClient.Core.Helper; +using NapCatRobotClient.Service.BackGround; using System.Net; namespace NapCatRobotClient.API @@ -18,6 +19,9 @@ namespace NapCatRobotClient.API builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); + builder.Services.AddHostedService(); + builder.Services.AddHostedService(); + var app = builder.Build(); app.UseInject(string.Empty); diff --git a/NapCatRobotClient/NapCatRobotClient.API/appsettings.json b/NapCatRobotClient/NapCatRobotClient.API/appsettings.json index bb67b33..2332be0 100644 --- a/NapCatRobotClient/NapCatRobotClient.API/appsettings.json +++ b/NapCatRobotClient/NapCatRobotClient.API/appsettings.json @@ -149,6 +149,7 @@ "😲🏹🇿🐦": "惊弓之鸟", "🐓✈️🥚🤛": "鸡飞蛋打", "🐴🌾🍊🏹": "马到成功", - "😋🥂🤹‍♂️😀": "吃喝玩乐" + "😋🥂🤹‍♂️😀": "吃喝玩乐", + "📟🐝🗣🌧": "呼风唤雨" } } diff --git a/NapCatRobotClient/NapCatRobotClient.Core/Consts.cs b/NapCatRobotClient/NapCatRobotClient.Core/Consts.cs index 90d903e..0cd2f7b 100644 --- a/NapCatRobotClient/NapCatRobotClient.Core/Consts.cs +++ b/NapCatRobotClient/NapCatRobotClient.Core/Consts.cs @@ -7,5 +7,9 @@ { public const string GoodsKey = "GoodsInfo"; + public const string MiJingKey = "MiJing:"; + + public const string LingTianKey = "LingTian:"; + } } diff --git a/NapCatRobotClient/NapCatRobotClient.Core/RobotAPI/RobotAPI.cs b/NapCatRobotClient/NapCatRobotClient.Core/RobotAPI/RobotAPI.cs index def5347..01cca0b 100644 --- a/NapCatRobotClient/NapCatRobotClient.Core/RobotAPI/RobotAPI.cs +++ b/NapCatRobotClient/NapCatRobotClient.Core/RobotAPI/RobotAPI.cs @@ -1,4 +1,5 @@ using Flurl.Http; +using Furion.Logging; using NapCatRobotClient.Core.RobotAPI.Dto.Request; namespace NapCatRobotClient.Core.RobotAPI @@ -27,12 +28,21 @@ namespace NapCatRobotClient.Core.RobotAPI private static async Task Post(string parameters, string action) { - string url = App.Configuration["QQConfig:SendApiUrl"] + action; - string response = await url.WithHeader("Authorization", "Bearer " + App.Configuration["QQConfig:AccessToken"]) - .WithHeader("Content-Type", "application/json;charset=utf-8") - .PostStringAsync(parameters) - .ReceiveString(); - return response; + try + { + string url = App.Configuration["QQConfig:SendApiUrl"] + action; + string response = await url.WithHeader("Authorization", "Bearer " + App.Configuration["QQConfig:AccessToken"]) + .WithHeader("Content-Type", "application/json;charset=utf-8") + .AllowAnyHttpStatus() + .PostStringAsync(parameters) + .ReceiveString(); + return response; + } + catch (Exception ex) + { + Log.Error($"RobotAPI 请求异常,请求参数:{parameters},请求地址:{action} {ex.Message},{ex.StackTrace}"); + return default; + } } } } diff --git a/NapCatRobotClient/NapCatRobotClient.Service/BackGround/LingTianBackGroundWorker.cs b/NapCatRobotClient/NapCatRobotClient.Service/BackGround/LingTianBackGroundWorker.cs new file mode 100644 index 0000000..d4f6093 --- /dev/null +++ b/NapCatRobotClient/NapCatRobotClient.Service/BackGround/LingTianBackGroundWorker.cs @@ -0,0 +1,60 @@ +using Furion.Logging; +using Microsoft.Extensions.Hosting; +using NapCatRobotClient.Core.RobotAPI.Dto.Request; + +namespace NapCatRobotClient.Service.BackGround +{ + /// + /// 灵田结算通知 + /// + public class LingTianBackGroundWorker : BackgroundService + { + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + string[] keys = RedisHelper.Client.Keys($"{RedisPrefix.LingTianKey}*"); + + foreach (string key in keys) + { + string json = RedisHelper.Client.Get(key); + if (string.IsNullOrEmpty(json)) continue; + + TaskDetailInfo task = JsonConvert.DeserializeObject(json); + if (task.TriggerTime <= DateTime.Now) + { + GroupSendMessageRequest request = new() + { + GroupId = task.GroupId, + Message = new() + { + new MessageItem() + { + Type = "text", + Data = new() + { + Text = $"【灵田结算通知】\r\n该结算奖励了!" + } + }, + new MessageItem() + { + Type = "at", + Data = new() + { + QQ = task.UserId + } + } + } + }; + _ = RobotAPI.SendGroupText(request); + + RedisHelper.Client.Del(key); + } + } + + await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); + } + + } + } +} diff --git a/NapCatRobotClient/NapCatRobotClient.Service/BackGround/MijingBackGroundWorker.cs b/NapCatRobotClient/NapCatRobotClient.Service/BackGround/MijingBackGroundWorker.cs new file mode 100644 index 0000000..5298019 --- /dev/null +++ b/NapCatRobotClient/NapCatRobotClient.Service/BackGround/MijingBackGroundWorker.cs @@ -0,0 +1,55 @@ +using Furion.Logging; +using Microsoft.Extensions.Hosting; +using NapCatRobotClient.Core.RobotAPI.Dto.Request; + +namespace NapCatRobotClient.Service.BackGround +{ + /// + /// 秘境结算通知 + /// + public class MijingBackGroundWorker : BackgroundService + { + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + string[] keys = RedisHelper.Client.Keys($"{RedisPrefix.MiJingKey}*"); + + foreach (string key in keys) + { + string json = RedisHelper.Client.Get(key); + if (string.IsNullOrEmpty(json)) continue; + + TaskDetailInfo task = JsonConvert.DeserializeObject(json); + if (task.TriggerTime <= DateTime.Now) + { + GroupSendMessageRequest request = new () + { + GroupId = task.GroupId, + Message = + [ + new MessageItem + { + Type = "text", + Data = new() { Text = $"【秘境结算通知】\r\n探索已完成,该结算奖励了!({task.Minutes} 分钟)" } + }, + new MessageItem + { + Type = "at", + Data = new() { QQ = task.UserId } + } + ] + }; + + _ = RobotAPI.SendGroupText(request); + + RedisHelper.Client.Del(key); + } + } + + await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); + } + + } + } +} diff --git a/NapCatRobotClient/NapCatRobotClient.Service/Group/Dto/TaskDetailInfo.cs b/NapCatRobotClient/NapCatRobotClient.Service/Group/Dto/TaskDetailInfo.cs new file mode 100644 index 0000000..db3decc --- /dev/null +++ b/NapCatRobotClient/NapCatRobotClient.Service/Group/Dto/TaskDetailInfo.cs @@ -0,0 +1,28 @@ +namespace NapCatRobotClient.Service.Group.Dto +{ + /// + /// 任务处理信息 + /// + public class TaskDetailInfo + { + /// + /// 群ID + /// + public string GroupId { get; set; } + + /// + /// 用户ID + /// + public string UserId { get; set; } + + /// + /// 结束时间 + /// + public DateTime TriggerTime { get; set; } + + /// + /// 用时 + /// + public double? Minutes { get; set; } + } +} diff --git a/NapCatRobotClient/NapCatRobotClient.Service/Group/Service/GroupService.cs b/NapCatRobotClient/NapCatRobotClient.Service/Group/Service/GroupService.cs index f3a0cd6..4071455 100644 --- a/NapCatRobotClient/NapCatRobotClient.Service/Group/Service/GroupService.cs +++ b/NapCatRobotClient/NapCatRobotClient.Service/Group/Service/GroupService.cs @@ -7,12 +7,12 @@ namespace NapCatRobotClient.Service.Group.Service /// /// 修仙大群 /// - private static List XiuXianGroup = App.GetConfig>("QQConfig:XiuXianGroupId"); + private List XiuXianGroup = App.GetConfig>("QQConfig:XiuXianGroupId"); /// /// 自动做任务群 /// - private static List AutoTaskGroup = App.GetConfig>("QQConfig:AutoTaskGroupId"); + private List AutoTaskGroup = App.GetConfig>("QQConfig:AutoTaskGroupId"); /// /// 处理群消息 @@ -41,8 +41,8 @@ namespace NapCatRobotClient.Service.Group.Service _ = ImmortalElixirProcess.ProcessGroupRequest(groupId, message); } if (AutoTaskGroup.Contains(groupId) && string.IsNullOrWhiteSpace(groupMsg) is false) - { - _= VerifyMsgProcess.ProcessGroupRequest(groupId, message); + { + _ = VerifyMsgProcess.ProcessGroupRequest(groupId, message); } return await Task.FromResult(true); diff --git a/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/LingTianProcess.cs b/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/LingTianProcess.cs index 1aaf683..3548560 100644 --- a/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/LingTianProcess.cs +++ b/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/LingTianProcess.cs @@ -25,11 +25,49 @@ namespace NapCatRobotClient.Service.Group.TextProcess } string groupMsg = JArray.Parse(json["message"].ToString()).FirstOrDefault(o => o["type"].ToString() == "text" - && !string.IsNullOrWhiteSpace(o["data"]["text"].ToString()) && (o["data"]["text"].ToString().Contains("道友本次采集成果") || o["data"]["text"].ToString().Contains("道友成功收获药材")))?["data"]?["text"]?.ToString(); + && !string.IsNullOrWhiteSpace(o["data"]["text"].ToString()) && (o["data"]["text"].ToString().Contains("道友的灵田还不能收取") || o["data"]["text"].ToString().Contains("道友本次采集成果") || o["data"]["text"].ToString().Contains("道友成功收获药材")))?["data"]?["text"]?.ToString(); if (string.IsNullOrWhiteSpace(groupMsg)) return false; List goods = new(); - if (groupMsg.Contains("道友本次采集成果")) + if (groupMsg.Contains("道友的灵田还不能收取")) + { + double ts = ConvertLastTime(groupMsg); + if (ts > 0) + { + ts += 0.01; + var atData = JArray.Parse(json["message"].ToString()).FirstOrDefault(o => o["type"].ToString() == "at"); + if (atData is not null) + { + string at = atData["data"]["qq"].ToString(); + ScheduleNextNotify(groupId, at, ts); + GroupSendMessageRequest request = new() + { + GroupId = groupId, + Message = new() + { + new MessageItem() + { + Type = "text", + Data = new() + { + Text = $"灵田结算预计在 【{DateTime.Now.AddHours(ts):yyyy-MM-dd HH:mm:ss}】 提醒您收取" + } + }, + new MessageItem() + { + Type = "at", + Data = new() + { + QQ = at + } + } + } + }; + await RobotAPI.SendGroupText(request); + } + } + } + else if (groupMsg.Contains("道友本次采集成果")) { goods = await NewCmd(groupMsg); } @@ -91,7 +129,7 @@ namespace NapCatRobotClient.Service.Group.TextProcess var atData = JArray.Parse(json["message"].ToString()).FirstOrDefault(o => o["type"].ToString() == "at"); if (atData is not null) { - ScheduleNextNotify(groupId, atData["data"]["qq"].ToString()); + ScheduleNextNotify(groupId, atData["data"]["qq"].ToString(), 47.01); } } @@ -104,6 +142,17 @@ namespace NapCatRobotClient.Service.Group.TextProcess return await Task.FromResult(true); } + private static double ConvertLastTime(string text) + { + Match match = Regex.Match(text, @"([\d\.]+)\s*小时"); + double hours = 0; + if (match.Success) + { + hours = double.Parse(match.Groups[1].Value); + } + return hours; + } + private static async Task> OldCmd(string message) { List goodsInfos = new(); @@ -134,41 +183,19 @@ namespace NapCatRobotClient.Service.Group.TextProcess return await Task.FromResult(goodsInfos); } - private static void ScheduleNextNotify(string groupId, string userId) + private static void ScheduleNextNotify(string groupId, string userId, double hours) { - TimeSpan delay = TimeSpan.FromHours(47.01); + DateTime now = DateTime.Now.AddHours(hours); + TimeSpan delay = TimeSpan.FromHours(hours + 0.09); - Log.Information($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {userId} 触发下次灵田结算通知 "); + Log.Information($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {userId} 触发下次灵田结算通知 结算时间:{now} "); - _ = Task.Run(async () => + RedisHelper.Client.Set($"{RedisPrefix.LingTianKey}{groupId}:{userId}", new TaskDetailInfo() { - await Task.Delay(delay); - - GroupSendMessageRequest request = new() - { - GroupId = groupId, - Message = new() - { - new MessageItem() - { - Type = "text", - Data = new() - { - Text = $"【灵田结算通知】\r\n该结算奖励了!" - } - }, - new MessageItem() - { - Type = "at", - Data = new() - { - QQ = userId - } - } - } - }; - await RobotAPI.SendGroupText(request); - }); + GroupId = groupId, + UserId = userId, + TriggerTime = now + }, delay); } } } diff --git a/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs b/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs index 9da44b7..d376577 100644 --- a/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs +++ b/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs @@ -1,5 +1,4 @@ using Furion.Logging; -using NapCatRobotClient.Core.RobotAPI.Dto.Request; using System.Globalization; namespace NapCatRobotClient.Service.Group.TextProcess @@ -51,40 +50,17 @@ namespace NapCatRobotClient.Service.Group.TextProcess private static void ScheduleReminder(string groupId, string userId, double minutes) { - DateTime now = DateTime.Now; + DateTime now = DateTime.Now.AddMinutes(minutes); - TimeSpan delay = TimeSpan.FromMinutes(minutes); + Log.Information($@"{now:yyyy-MM-dd HH:mm:ss} {userId} 触发秘境通知 {minutes}分钟 结束时间: {now:yyyy-MM-dd HH:mm:ss}"); - Log.Information($@"{now:yyyy-MM-dd HH:mm:ss} {userId} 触发秘境通知 {minutes}分钟 结束时间: {now.AddMinutes(minutes):yyyy-MM-dd HH:mm:ss}"); - - _ = Task.Run(async () => + RedisHelper.Client.Set($"{RedisPrefix.MiJingKey}{groupId}:{userId}", new TaskDetailInfo() { - await Task.Delay(delay); - GroupSendMessageRequest request = new() - { - GroupId = groupId, - Message = new() - { - new MessageItem() - { - Type = "text", - Data = new() - { - Text = $"【秘境结算通知】\r\n探索已完成,该结算奖励了!({minutes} 分钟)" - } - }, - new MessageItem() - { - Type = "at", - Data = new() - { - QQ = userId - } - } - } - }; - await RobotAPI.SendGroupText(request); - }); + GroupId = groupId, + Minutes = minutes, + UserId = userId, + TriggerTime = now + }, Convert.ToInt32((minutes + 3) * 60)); } private static double ParseMinutes(string content)