Files
NapCatRobot/NapCatRobotClient/NapCatRobotClient.Service/Group/TextProcess/MiJingNotifyProcess.cs
T

125 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Furion.Logging;
using System.Globalization;
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 userId = json["user_id"]?.ToString();
string robotId = App.Configuration["QQConfig:XiaoXiaoRobotQQ"];
if (userId != robotId)
return false;
if (json["message"] is not JArray messageArray || messageArray.Count == 0)
return false;
string[] keywords = { "进入秘境", "道友已踏入", "道友已破界", "道友已遁入", "道友已降临" };
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)
return true;
JToken atData = messageArray.FirstOrDefault(o => o["type"]?.ToString() == "at");
string targetQQ = null;
if (atData != null)
{
targetQQ = atData["data"]?["qq"]?.ToString();
}
else
{
string markdown = messageArray
.FirstOrDefault(o => o["type"]?.ToString() == "markdown")
?["data"]?["content"]?.ToString();
if (!string.IsNullOrWhiteSpace(markdown))
{
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}\n{ex.StackTrace}",
true);
return false;
}
}
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;
}
}
}