89 lines
3.4 KiB
C#
89 lines
3.4 KiB
C#
using Furion.Logging;
|
||
using System.Globalization;
|
||
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;
|
||
|
||
namespace NapCatRobotClient.Service.Group.TextProcess
|
||
{
|
||
/// <summary>
|
||
/// 秘境通知
|
||
/// </summary>
|
||
public class MiJingNotifyProcess
|
||
{
|
||
public static async Task<bool> ProcessGroupRequest(string groupId, string message)
|
||
{
|
||
try
|
||
{
|
||
JObject json = JObject.Parse(message);
|
||
string xx = json["user_id"]?.ToString();
|
||
if (xx != App.Configuration["QQConfig:XiaoXiaoRobotQQ"])
|
||
{
|
||
return false;
|
||
}
|
||
|
||
var messageArray = JArray.Parse(json["message"].ToString());
|
||
|
||
List<string> keywords = new() { "进入秘境", "道友已踏入", "道友已破界", "道友已遁入", "道友已降临" };
|
||
|
||
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;
|
||
|
||
var minutes = ParseMinutes(groupMsg);
|
||
if (minutes > 0)
|
||
{
|
||
var atData = messageArray.FirstOrDefault(o => o["type"].ToString() == "at");
|
||
if (atData is not null)
|
||
{
|
||
ScheduleReminder(groupId, atData["data"]["qq"].ToString(), minutes);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 秘境结束通知 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}", true);
|
||
}
|
||
return await Task.FromResult(true);
|
||
}
|
||
|
||
private static void ScheduleReminder(string groupId, string userId, double minutes)
|
||
{
|
||
DateTime now = DateTime.Now.AddMinutes(minutes);
|
||
|
||
Log.Information($@"{now:yyyy-MM-dd HH:mm:ss} {userId} 触发秘境通知 {minutes}分钟 结束时间: {now:yyyy-MM-dd HH:mm:ss}");
|
||
|
||
RedisHelper.Client.Set($"{RedisPrefix.MiJingKey}{groupId}:{userId}", new TaskDetailInfo()
|
||
{
|
||
GroupId = groupId,
|
||
Minutes = minutes,
|
||
UserId = userId,
|
||
TriggerTime = now
|
||
}, Convert.ToInt32((minutes + 3) * 60));
|
||
}
|
||
|
||
private static double ParseMinutes(string content)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(content)) return 0;
|
||
|
||
var match = Regex.Match(content, @"(花费时间|探索耗时|妖时流转|探索时长|勘历天时)[::]\s*(\d+(?:\.\d+)?)", RegexOptions.Compiled);
|
||
if (!match.Success)
|
||
{
|
||
match = Regex.Match(content, @"(\d+(?:\.\d+)?)", RegexOptions.Compiled);
|
||
}
|
||
|
||
if (match.Success)
|
||
{
|
||
if (double.TryParse(match.Groups[2].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double val))
|
||
{
|
||
return (double)val;
|
||
}
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
}
|
||
}
|