From 158199c1da9c167a3f4d284591ae27e7f2f4cf86 Mon Sep 17 00:00:00 2001 From: Hellobaka <863450594@qq.com> Date: Mon, 11 Nov 2024 09:40:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrderFunctions/Devour.cs | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/me.cqp.luohuaming.iKun.Code/OrderFunctions/Devour.cs b/me.cqp.luohuaming.iKun.Code/OrderFunctions/Devour.cs index d30c0eb..e3fabfc 100644 --- a/me.cqp.luohuaming.iKun.Code/OrderFunctions/Devour.cs +++ b/me.cqp.luohuaming.iKun.Code/OrderFunctions/Devour.cs @@ -16,34 +16,44 @@ public class Devour : IOrderModel public bool Judge(string destStr) => destStr.Replace("#", "#").StartsWith(GetOrderStr()); + /// + /// 执行吞噬操作的方法。 + /// + /// 事件参数,包含消息来源和内容。 + /// 执行结果,包括是否成功、是否发送消息以及发送的消息内容。 public FunctionResult Execute(CQGroupMessageEventArgs e) { + // 初始化执行结果 FunctionResult result = new FunctionResult { Result = true, SendFlag = true, }; + + // 创建发送文本对象并设置发送的目标群 SendText sendText = new SendText { SendID = e.FromGroup, }; result.SendObject.Add(sendText); + + // 尝试从消息中解析出目标QQ号 long target = -1; if (!e.Message.CQCodes.Any(x => x.Function == Sdk.Cqp.Enum.CQFunction.At) || !e.Message.CQCodes.First(x => x.Function == Sdk.Cqp.Enum.CQFunction.At).Items.TryGetValue("qq", out string targetValue) || !long.TryParse(targetValue, out target)) { - // 非CQ码,尝试解析昵称、卡片与QQ + // 如果没有@,则尝试解析昵称、卡片或QQ号 string raw = e.Message.Text.Substring(GetOrderStr().Length).Trim(); if (string.IsNullOrEmpty(raw)) { - // 无有效指令 + // 没有有效指令 sendText.MsgToSend.Add(string.Format(AppConfig.ReplyParamInvalid, $"或无法找到目标,示例:{GetOrderStr()} [QQ|At|昵称|卡片]")); return result; } if (!long.TryParse(raw, out target)) { - // 获取昵称与卡片列表 + // 获取群成员信息,尝试匹配昵称或卡片 if (!MainSave.GroupMemberInfos.TryGetValue(e.FromGroup, out var infos)) { infos = e.FromGroup.GetGroupMemberList(); @@ -56,17 +66,22 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) } } } + + // 检查目标QQ号是否有效 if (target < QQ.MinValue) { sendText.MsgToSend.Add(string.Format(AppConfig.ReplyParamInvalid, $",示例:{GetOrderStr()} [QQ|At|昵称|卡片]")); return result; } + + // 检查是否吞噬自己 if (target == e.FromQQ) { sendText.MsgToSend.Add(AppConfig.ReplyDevourSelf); return result; } + // 获取玩家和目标玩家的信息 var player = Player.GetPlayer(e.FromQQ); if (player == null) { @@ -79,6 +94,8 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) sendText.MsgToSend.Add(AppConfig.ReplyNoTargetPlayer); return result; } + + // 获取玩家和目标玩家的Kun信息 var kun = Kun.GetKunByQQ(player.QQ); if (kun == null) { @@ -86,6 +103,8 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) return result; } kun.Initialize(); + + // 检查Kun是否在自动模式下 if (AutoPlay.CheckKunAutoPlay(kun)) { sendText.MsgToSend.Add(string.Format(AppConfig.ReplyAutoPlaying, kun)); @@ -96,6 +115,8 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) sendText.MsgToSend.Add(string.Format(AppConfig.ReplyWorking, kun)); return result; } + + // 获取目标玩家的Kun信息 var targetKun = Kun.GetKunByQQ(targetPlayer.QQ); if (targetKun == null) { @@ -103,18 +124,22 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) return result; } targetKun.Initialize(); + + // 检查目标Kun是否在自动模式下 if (AutoPlay.CheckKunAutoPlay(targetKun)) { sendText.MsgToSend.Add(string.Format(AppConfig.ReplyAutoPlaying, targetKun)); return result; } + // 检查冷却时间 if (DateTime.Now - player.DevourAt < TimeSpan.FromMinutes(AppConfig.ValueDevourCD)) { sendText.MsgToSend.Add(string.Format(AppConfig.ReplyDevourInCD, (player.DevourAt.AddMinutes(30)).ToString("G"))); return result; } + // 检查是否在同一个群 bool sameGroup = CommonHelper.CheckSameGroup(target, e.FromGroup); long notSameGroupId = 0; if (!sameGroup) @@ -125,6 +150,8 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) notSameGroupId = record.Group; } } + + // 构建目标玩家信息 string playerInfo = ""; if (AppConfig.EnableAt) { @@ -139,14 +166,19 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) playerInfo = e.CQApi.GetGroupMemberInfo(notSameGroupId, target)?.Card ?? target.ToString(); } + // 执行吞噬操作 var r = kun.Devour(targetKun); if (r.Success is false) { sendText.MsgToSend.Add("吞噬方法过程发生异常,查看日志获取更多信息"); return result; } + + // 更新玩家的吞噬时间 player.DevourAt = DateTime.Now; player.Update(); + + // 处理吞噬结果 if (r.Dead) { sendText.MsgToSend.Add(string.Format(AppConfig.ReplyDevourFailAndDead, kun.ToString(), playerInfo, targetKun.ToString(), r.TargetDecrement.ToShortNumber(), r.TargetCurrentWeight.ToShortNumber())); @@ -169,6 +201,7 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) sendText.MsgToSend.Add(send); } + // 如果不在同一个群且启用了跨群广播 if (!sameGroup && AppConfig.EnableNotSameGroupDevourBoardcast) { if (r.Escaped) @@ -180,6 +213,7 @@ public FunctionResult Execute(CQGroupMessageEventArgs e) e.CQApi.SendGroupMessage(notSameGroupId, string.Format(AppConfig.ReplyDevouredNotSameGroup, CQApi.CQCode_At(target))); } } + return result; }