Пример #1
0
        public static void StartRssTask(Action <GreenOnionsMessages, long, long> SendMessage)
        {
            if (BotInfo.RssEnabled && BotInfo.IsLogin)
            {
                if (_RssWorker != null && !_RssWorker.IsCompleted && !_RssWorker.IsCanceled && !_RssWorker.IsFaulted)
                {
                    return;
                }
                _source    = new CancellationTokenSource();
                _RssWorker = Task.Run(async() =>
                {
                    LogHelper.WriteInfoLog("启动RSS抓取线程");
                    while (BotInfo.RssEnabled && BotInfo.IsLogin)
                    {
                        if (_source.IsCancellationRequested)
                        {
                            return;
                        }

                        foreach (RssSubscriptionItem item in BotInfo.RssSubscription)  //若干条订阅地址
                        {
                            //如果在调试模式并且转发的QQ和群组均不在管理员和调试群组集合中时不去请求
                            if (BotInfo.DebugMode && ((BotInfo.DebugReplyAdminOnly && item.ForwardQQs.Intersect(BotInfo.AdminQQ).Count() == 0) || (BotInfo.OnlyReplyDebugGroup && item.ForwardGroups.Intersect(BotInfo.DebugGroups).Count() == 0)))
                            {
                                LogHelper.WriteWarningLog("没有为订阅源设置转发目标或当前处于调试模式, 不进行转发");
                                continue;
                            }

                            try
                            {
                                if (item.ForwardGroups.Length == 0 && item.ForwardQQs.Length == 0)
                                {
                                    continue;
                                }
                                if (!Cache.LastOneSendRssTime.ContainsKey(item.Url))  //如果不存在上次发送的日期记录
                                {
                                    LogHelper.WriteInfoLog($"首次抓取到{item.Url}内容, 只保存不发送, 防止内容太多刷屏");
                                    Cache.LastOneSendRssTime.TryAdd(item.Url, DateTime.Now);  //添加现在作为起始日期(避免把所有历史信息全都抓过来发送)
                                    Cache.LastOneSendRssTime = Cache.LastOneSendRssTime;
                                    JsonHelper.SaveCacheFile();
                                    continue;
                                }
                                foreach (var rss in ReadRss(item.Url))  //每条订阅地址可能获取到若干条更新
                                {
                                    if (rss.pubDate > Cache.LastOneSendRssTime[item.Url])
                                    {
                                        LogHelper.WriteInfoLog($"更新时间晚于记录时间, 需要推送消息");

                                        string titleMsg     = $"{rss.title}更新啦:\r\n{rss.description}";
                                        string translateMsg = null;
                                        if (item.Translate)
                                        {
                                            LogHelper.WriteInfoLog($"本条RSS订阅启用了翻译");
                                            string translatedText;
                                            if (item.TranslateFromTo)
                                            {
                                                translatedText = await(BotInfo.TranslateEngineType == TranslateEngine.Google ? GoogleTranslateHelper.TranslateFromTo(rss.description, item.TranslateFrom, item.TranslateTo) : YouDaoTranslateHelper.TranslateFromTo(rss.description, item.TranslateFrom, item.TranslateTo));
                                            }
                                            else
                                            {
                                                translatedText = await(BotInfo.TranslateEngineType == TranslateEngine.Google ? GoogleTranslateHelper.TranslateToChinese(rss.description) : YouDaoTranslateHelper.TranslateToChinese(rss.description));
                                            }
                                            translateMsg = $"\r\n以下为翻译内容:\r\n{ translatedText }";
                                            LogHelper.WriteInfoLog($"翻译成功");
                                        }

                                        LogHelper.WriteInfoLog($"需要转发的组:{item.ForwardGroups.Length}个");
                                        if (item.ForwardGroups.Length > 0)
                                        {
                                            GreenOnionsMessages groupResultMsg = new GreenOnionsMessages();

                                            if (item.AtAll)
                                            {
                                                groupResultMsg.Add(new GreenOnionsAtMessage(-1, "全体成员"));
                                                groupResultMsg.Add("\r\n");
                                            }

                                            groupResultMsg.Add(titleMsg);
                                            if (translateMsg != null)
                                            {
                                                groupResultMsg.Add(translateMsg);
                                            }

                                            for (int i = 0; i < rss.imgsSrc.Length; i++)
                                            {
                                                groupResultMsg.Add(new GreenOnionsImageMessage(rss.imgsSrc[i]));
                                            }

                                            groupResultMsg.Add($"\r\n更新时间:{rss.pubDate}");
                                            groupResultMsg.Add($"\r\n原文地址:{rss.link}");

                                            LogHelper.WriteInfoLog($"组合群消息完成");

                                            if (item.SendByForward)
                                            {
                                                LogHelper.WriteInfoLog($"发送模式为合并转发");
                                                GreenOnionsForwardMessage greenOnionsForwardMessage = new GreenOnionsForwardMessage(BotInfo.QQId, BotInfo.BotName, groupResultMsg);

                                                for (int i = 0; i < item.ForwardGroups.Length; i++)
                                                {
                                                    if (_source.IsCancellationRequested)
                                                    {
                                                        return;
                                                    }
                                                    SendMessage(greenOnionsForwardMessage, -1, item.ForwardGroups[i]);
                                                }
                                            }
                                            else
                                            {
                                                LogHelper.WriteInfoLog($"发送模式为直接发送");
                                                for (int i = 0; i < item.ForwardGroups.Length; i++)
                                                {
                                                    if (_source.IsCancellationRequested)
                                                    {
                                                        return;
                                                    }
                                                    SendMessage(groupResultMsg, -1, item.ForwardGroups[i]);
                                                }
                                            }
                                            LogHelper.WriteInfoLog($"全部群消息发送完毕");
                                        }
                                        LogHelper.WriteInfoLog($"需要转发的好友:{item.ForwardQQs.Length}个");
                                        if (item.ForwardQQs.Length > 0)
                                        {
                                            GreenOnionsMessages friendResultMsg = new GreenOnionsMessages();

                                            friendResultMsg.Add(titleMsg);
                                            if (translateMsg != null)
                                            {
                                                friendResultMsg.Add(translateMsg);
                                            }

                                            for (int i = 0; i < rss.imgsSrc.Length; i++)
                                            {
                                                friendResultMsg.Add(new GreenOnionsImageMessage(rss.imgsSrc[i]));
                                            }

                                            friendResultMsg.Add($"\r\n更新时间:{rss.pubDate}");
                                            friendResultMsg.Add($"\r\n原文地址:{rss.link}");

                                            LogHelper.WriteInfoLog($"组合好友消息完成");

                                            if (item.SendByForward)
                                            {
                                                LogHelper.WriteInfoLog($"发送模式为合并转发");
                                                GreenOnionsForwardMessage greenOnionsForwardMessage = new GreenOnionsForwardMessage(BotInfo.QQId, BotInfo.BotName, friendResultMsg);

                                                for (int i = 0; i < item.ForwardQQs.Length; i++)
                                                {
                                                    if (_source.IsCancellationRequested)
                                                    {
                                                        return;
                                                    }
                                                    SendMessage(friendResultMsg, item.ForwardQQs[i], -1);
                                                }
                                            }
                                            else
                                            {
                                                LogHelper.WriteInfoLog($"发送模式为直接发送");
                                                for (int i = 0; i < item.ForwardQQs.Length; i++)
                                                {
                                                    if (_source.IsCancellationRequested)
                                                    {
                                                        return;
                                                    }
                                                    SendMessage(friendResultMsg, item.ForwardQQs[i], -1);
                                                }
                                            }
                                            LogHelper.WriteInfoLog($"全部好友消息发送完毕");
                                        }

                                        if (Cache.LastOneSendRssTime.ContainsKey(item.Url))
                                        {
                                            Cache.LastOneSendRssTime[item.Url] = rss.pubDate;
                                        }
                                        else
                                        {
                                            Cache.LastOneSendRssTime.TryAdd(item.Url, rss.pubDate);  //群和好友均推送完毕后记录此地址的最后更新时间
                                        }
                                        Cache.LastOneSendRssTime = Cache.LastOneSendRssTime;
                                        JsonHelper.SaveCacheFile();

                                        LogHelper.WriteInfoLog($"记录{item.Url}最后更新时间完毕");

                                        //if (rss.iframseSrc.Length > 0)  //视频或内嵌网页没想好怎么处理
                                        //{

                                        //}
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteErrorLogWithUserMessage("获取RSS错误", ex, $"请求地址为:{item.Url}");
                            }
                        }
                        await Task.Delay((int)Math.Round(BotInfo.ReadRssInterval * 1000 * 60));
                    }
                }, _source.Token);
            }
        }
Пример #2
0
        public static async void HandleGroupMesage(MiraiHttpSession session, IMessageBase[] Chain, IGroupMemberInfo sender, QuoteMessage quoteMessage)
        {
            Regex regexSearchOn           = new Regex(BotInfo.SearchModeOnCmd.ReplaceGreenOnionsTags());
            Regex regexSearchOff          = new Regex(BotInfo.SearchModeOffCmd.ReplaceGreenOnionsTags());
            Regex regexTranslateToChinese = new Regex(BotInfo.TranslateToChineseCMD.ReplaceGreenOnionsTags());
            Regex regexTranslateTo        = new Regex(BotInfo.TranslateToCMD.ReplaceGreenOnionsTags());
            Regex regexHPicture           = new Regex(BotInfo.HPictureCmd.ReplaceGreenOnionsTags());
            Regex regexShabHPicture       = new Regex(BotInfo.ShabHPictureCmd.ReplaceGreenOnionsTags());
            Regex regexSelectPhone        = new Regex($"({BotInfo.BotName}查询手机号[::])");


            string firstMessage = Chain[1].ToString();

            #region -- 连续搜图 --
            if (regexSearchOn.IsMatch(firstMessage))
            {
                if (Cache.SearchingPictures.ContainsKey(sender.Id))
                {
                    Cache.SearchingPictures[sender.Id] = DateTime.Now.AddMinutes(1);
                    await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.SearchModeAlreadyOnReply.ReplaceGreenOnionsTags()) }, quoteMessage.Id);
                }
                else
                {
                    Cache.SearchingPictures.Add(sender.Id, DateTime.Now.AddMinutes(1));
                    await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.SearchModeOnReply.ReplaceGreenOnionsTags()) }, quoteMessage.Id);

                    Cache.CheckSearchPictureTime(callback =>
                    {
                        Cache.SearchingPictures.Remove(sender.Id);
                        session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.SearchModeTimeOutReply.ReplaceGreenOnionsTags()) }, quoteMessage.Id);
                    });
                }
            }
            else if (regexSearchOff.IsMatch(firstMessage))
            {
                if (Cache.SearchingPictures.ContainsKey(sender.Id))
                {
                    Cache.SearchingPictures.Remove(sender.Id);
                    await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.SearchModeOffReply.ReplaceGreenOnionsTags()) }, quoteMessage.Id);
                }
                else
                {
                    await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.SearchModeAlreadyOffReply.ReplaceGreenOnionsTags()) }, quoteMessage.Id);
                }
            }
            #endregion -- 连续搜图 --
            #region -- 翻译 --
            else if (regexTranslateToChinese.IsMatch(firstMessage))
            {
                foreach (Match match in regexTranslateToChinese.Matches(firstMessage))
                {
                    try
                    {
                        await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(await GoogleTranslateHelper.TranslateToChinese(firstMessage.Substring(match.Value.Length))) }, quoteMessage.Id);
                    }
                    catch (Exception ex)
                    {
                        await session.SendGroupMessageAsync(sender.Group.Id, new PlainMessage("翻译失败," + ex.Message));
                    }
                    break;
                }
            }
            else if (regexTranslateTo.IsMatch(firstMessage))
            {
                foreach (Match match in regexTranslateTo.Matches(firstMessage))
                {
                    if (match.Groups.Count > 1)
                    {
                        try
                        {
                            await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(await GoogleTranslateHelper.TranslateTo(firstMessage.Substring(match.Value.Length), match.Groups[1].Value)) }, quoteMessage.Id);
                        }
                        catch (Exception ex)
                        {
                            await session.SendGroupMessageAsync(sender.Group.Id, new PlainMessage("翻译失败," + ex.Message));
                        }
                    }
                    break;
                }
            }
            #endregion -- 翻译 --
            #region -- 色图 --
            #region -- Lolicon图库 --
            else if (BotInfo.EnabledLoliconDataBase && (regexHPicture.IsMatch(firstMessage) || BotInfo.HPictureUserCmd.Contains(firstMessage)))
            {
                #region -- 异步流色图(已弃用) --
                //await foreach (var item in HPictureHandlerAsync.SendHPictures(session, firstMessage, BotInfo.HPictureAllowR18 && (!BotInfo.HPictureR18WhiteOnly || BotInfo.HPictureWhiteGroup.Contains(sender.Group.Id))))
                //{
                //    int messageID = await session.SendGroupMessageAsync(sender.Group.Id, new[] { item }, quoteMessage.Id);

                //    if (item is PlainMessage)  //地址发出去后记录次数
                //    {
                //        if (BotInfo.HPictureLimitType == LimitType.Frequency)
                //        {
                //            Cache.RecordLimit(sender.Id);
                //        }
                //        Cache.RecordCD(sender.Id, sender.Group.Id);
                //    }
                //    else if (item is ImageMessage)
                //    {
                //        if (BotInfo.HPictureLimitType == LimitType.Count)
                //        {
                //            Cache.RecordLimit(sender.Id);
                //        }
                //        HPictureHandler.RevokeHPicture(session, messageID, BotInfo.HPictureWhiteGroup.Contains(sender.Group.Id) ? BotInfo.HPictureWhiteRevoke : BotInfo.HPictureRevoke);
                //    }
                //}
                #endregion -- 异步流色图(已弃用) --

                if (sender.Permission == GroupPermission.Member || !BotInfo.HPictureManageNoLimit)
                {
                    if (Cache.CheckGroupLimit(sender.Id, sender.Group.Id))
                    {
                        await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.HPictureOutOfLimitReply) }, quoteMessage.Id);

                        return;
                    }
                    if (Cache.CheckGroupCD(sender.Id, sender.Group.Id))
                    {
                        await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.HPictureCDUnreadyReply) }, quoteMessage.Id);

                        return;
                    }
                }

                HPictureHandler.SendHPictures(session, firstMessage, BotInfo.HPictureAllowR18 && (!BotInfo.HPictureR18WhiteOnly || BotInfo.HPictureWhiteGroup.Contains(sender.Group.Id)), BotInfo.HPictureEndCmd, stream => session.UploadPictureAsync(UploadTarget.Group, stream), msg => session.SendGroupMessageAsync(sender.Group.Id, msg, quoteMessage.Id), limitType =>
                {
                    if (limitType == LimitType.Frequency)
                    {
                        Cache.RecordGroupCD(sender.Id, sender.Group.Id);
                        if (BotInfo.HPictureLimitType == LimitType.Frequency)
                        {
                            Cache.RecordLimit(sender.Id);
                        }
                    }
                    else if (limitType == LimitType.Count && BotInfo.HPictureLimitType == LimitType.Count)
                    {
                        Cache.RecordLimit(sender.Id);
                    }
                }, BotInfo.HPictureWhiteGroup.Contains(sender.Group.Id) ? BotInfo.HPictureWhiteRevoke : BotInfo.HPictureRevoke);
            }
            #endregion  -- Lolicon图库 --
            #region -- Shab图库 --
            else if (BotInfo.EnabledShabDataBase && (regexShabHPicture.IsMatch(firstMessage) || BotInfo.HPictureUserCmd.Contains(firstMessage)))
            {
                if (sender.Permission == GroupPermission.Member || !BotInfo.HPictureManageNoLimit)
                {
                    if (Cache.CheckGroupLimit(sender.Id, sender.Group.Id))
                    {
                        await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.HPictureOutOfLimitReply) }, quoteMessage.Id);

                        return;
                    }
                    if (Cache.CheckGroupCD(sender.Id, sender.Group.Id))
                    {
                        await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(BotInfo.HPictureCDUnreadyReply) }, quoteMessage.Id);

                        return;
                    }
                }

                HPictureHandler.SendHPictures(session, firstMessage, BotInfo.HPictureAllowR18 && (!BotInfo.HPictureR18WhiteOnly || BotInfo.HPictureWhiteGroup.Contains(sender.Group.Id)), BotInfo.ShabHPictureEndCmd, stream => session.UploadPictureAsync(UploadTarget.Group, stream), msg => session.SendGroupMessageAsync(sender.Group.Id, msg, quoteMessage.Id), limitType =>
                {
                    if (limitType == LimitType.Frequency)
                    {
                        Cache.RecordGroupCD(sender.Id, sender.Group.Id);
                        if (BotInfo.HPictureLimitType == LimitType.Frequency)
                        {
                            Cache.RecordLimit(sender.Id);
                        }
                    }
                    else if (limitType == LimitType.Count && BotInfo.HPictureLimitType == LimitType.Count)
                    {
                        Cache.RecordLimit(sender.Id);
                    }
                }, BotInfo.HPictureWhiteGroup.Contains(sender.Group.Id) ? BotInfo.HPictureWhiteRevoke : BotInfo.HPictureRevoke);
            }
            #endregion -- Shab图库 --
            #endregion -- 色图 --
            else if (firstMessage == $"{BotInfo.BotName}帮助")
            {
                List <string> lstEnabledFeatures = new List <string>();
                if (BotInfo.SearchEnabled)
                {
                    lstEnabledFeatures.Add("搜图");
                }
                if (BotInfo.TranslateEnabled)
                {
                    lstEnabledFeatures.Add("翻译");
                }
                if (BotInfo.HPictureEnabled)
                {
                    lstEnabledFeatures.Add("GHS");
                }
                if (BotInfo.QQId == 3246934384)
                {
                    lstEnabledFeatures.Add("查手机号");
                }
                string strHelpResult = $"现在您可以让我{string.Join(",", lstEnabledFeatures)}。\r\n如果您觉得{BotInfo.BotName}好用,请到{BotInfo.BotName}的项目地址 https://github.com/Alex1911-Jiang/GreenOnions 给{BotInfo.BotName}一颗星星。";
                await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(strHelpResult) }, quoteMessage.Id);
            }
            #region -- 查询手机号(夹带私货) --
            else if (regexSelectPhone.IsMatch(firstMessage))
            {
                if (BotInfo.QQId == 3246934384)
                {
                    foreach (Match match in regexSelectPhone.Matches(firstMessage))
                    {
                        if (match.Groups.Count > 1)
                        {
                            string qqNumber = firstMessage.Substring(match.Groups[1].Length);
                            long   lQQNumber;
                            if (long.TryParse(qqNumber, out lQQNumber))
                            {
                                try
                                {
                                    string result = AssemblyHelper.CallStaticMethod <string>("GreenOnions.QQPhone", "GreenOnions.QQPhone.QQAndPhone", "GetPhoneByQQ", lQQNumber);
                                    await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage(result) }, quoteMessage.Id);
                                }
                                catch (Exception ex)
                                {
                                    await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage("查询失败" + ex.Message) }, quoteMessage.Id);
                                }
                            }
                            else
                            {
                                await session.SendGroupMessageAsync(sender.Group.Id, new[] { new PlainMessage("请输入正确的QQ号码(不支持以邮箱查询)") }, quoteMessage.Id);
                            }
                        }
                    }
                }
            }
            #endregion -- 查询手机号(夹带私货) --
        }
Пример #3
0
        public static async void HandleFriendMesage(MiraiHttpSession session, IMessageBase[] Chain, long qqId)
        {
            Regex regexSearchOn           = new Regex(BotInfo.SearchModeOnCmd.ReplaceGreenOnionsTags());
            Regex regexSearchOff          = new Regex(BotInfo.SearchModeOffCmd.ReplaceGreenOnionsTags());
            Regex regexTranslateToChinese = new Regex(BotInfo.TranslateToChineseCMD.ReplaceGreenOnionsTags());
            Regex regexTranslateTo        = new Regex(BotInfo.TranslateToCMD.ReplaceGreenOnionsTags());
            Regex regexHPicture           = new Regex(BotInfo.HPictureCmd.ReplaceGreenOnionsTags());
            Regex regexShabHPicture       = new Regex(BotInfo.ShabHPictureCmd.ReplaceGreenOnionsTags());
            //Regex regexSelectPhone = new Regex($"({BotInfo.BotName}查询手机号[::])");


            string firstMessage = Chain[1].ToString();

            #region -- 连续搜图 --
            if (regexSearchOn.IsMatch(firstMessage))
            {
                if (Cache.SearchingPictures.ContainsKey(qqId))
                {
                    Cache.SearchingPictures[qqId] = DateTime.Now.AddMinutes(1);
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.SearchModeAlreadyOnReply.ReplaceGreenOnionsTags()));
                }
                else
                {
                    Cache.SearchingPictures.Add(qqId, DateTime.Now.AddMinutes(1));
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.SearchModeOnReply.ReplaceGreenOnionsTags()));

                    Cache.CheckSearchPictureTime(callback =>
                    {
                        Cache.SearchingPictures.Remove(qqId);
                        session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.SearchModeTimeOutReply.ReplaceGreenOnionsTags()));
                    });
                }
            }
            else if (regexSearchOff.IsMatch(firstMessage))
            {
                if (Cache.SearchingPictures.ContainsKey(qqId))
                {
                    Cache.SearchingPictures.Remove(qqId);
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.SearchModeOffReply.ReplaceGreenOnionsTags()));
                }
                else
                {
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.SearchModeAlreadyOffReply.ReplaceGreenOnionsTags()));
                }
            }
            #endregion -- 连续搜图 --
            #region -- 翻译 --
            else if (regexTranslateToChinese.IsMatch(firstMessage))
            {
                foreach (Match match in regexTranslateToChinese.Matches(firstMessage))
                {
                    try
                    {
                        await session.SendFriendMessageAsync(qqId, new PlainMessage(await GoogleTranslateHelper.TranslateToChinese(firstMessage.Substring(match.Value.Length))));
                    }
                    catch (Exception ex)
                    {
                        await session.SendFriendMessageAsync(qqId, new PlainMessage("翻译失败," + ex.Message));
                    }
                    break;
                }
            }
            else if (regexTranslateTo.IsMatch(firstMessage))
            {
                foreach (Match match in regexTranslateTo.Matches(firstMessage))
                {
                    if (match.Groups.Count > 1)
                    {
                        try
                        {
                            await session.SendFriendMessageAsync(qqId, new PlainMessage(await GoogleTranslateHelper.TranslateTo(firstMessage.Substring(match.Value.Length), match.Groups[1].Value)));
                        }
                        catch (Exception ex)
                        {
                            await session.SendFriendMessageAsync(qqId, new PlainMessage("翻译失败," + ex.Message));
                        }
                    }
                    break;
                }
            }
            #endregion -- 翻译 --
            #region -- 色图 --
            #region  -- Lolicon图库 --
            else if (regexHPicture.IsMatch(firstMessage) || BotInfo.HPictureUserCmd.Contains(firstMessage))
            {
                if (Cache.CheckPMLimit(qqId))
                {
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.HPictureOutOfLimitReply));

                    return;
                }
                if (Cache.CheckPMCD(qqId))
                {
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.HPictureCDUnreadyReply));

                    return;
                }
                HPictureHandler.SendHPictures(session, firstMessage, BotInfo.HPictureAllowR18, BotInfo.HPictureEndCmd, stream => session.UploadPictureAsync(UploadTarget.Friend, stream), msg => session.SendFriendMessageAsync(qqId, msg), limitType =>
                {
                    if (limitType == LimitType.Frequency)
                    {
                        Cache.RecordFriendCD(qqId);
                        if (BotInfo.HPictureLimitType == LimitType.Frequency)
                        {
                            Cache.RecordLimit(qqId);
                        }
                    }
                    else if (limitType == LimitType.Count && BotInfo.HPictureLimitType == LimitType.Count)
                    {
                        Cache.RecordLimit(qqId);
                    }
                }, BotInfo.HPicturePMRevoke);
            }
            #endregion  -- Lolicon图库 --
            #region  -- Shab图库 --
            else if (regexShabHPicture.IsMatch(firstMessage) || BotInfo.HPictureUserCmd.Contains(firstMessage))
            {
                if (Cache.CheckPMLimit(qqId))
                {
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.HPictureOutOfLimitReply));

                    return;
                }
                if (Cache.CheckPMCD(qqId))
                {
                    await session.SendFriendMessageAsync(qqId, new PlainMessage(BotInfo.HPictureCDUnreadyReply));

                    return;
                }
                HPictureHandler.SendHPictures(session, firstMessage, BotInfo.HPictureAllowR18, BotInfo.ShabHPictureEndCmd, stream => session.UploadPictureAsync(UploadTarget.Friend, stream), msg => session.SendFriendMessageAsync(qqId, msg), limitType =>
                {
                    if (limitType == LimitType.Frequency)
                    {
                        Cache.RecordFriendCD(qqId);
                        if (BotInfo.HPictureLimitType == LimitType.Frequency)
                        {
                            Cache.RecordLimit(qqId);
                        }
                    }
                    else if (limitType == LimitType.Count && BotInfo.HPictureLimitType == LimitType.Count)
                    {
                        Cache.RecordLimit(qqId);
                    }
                }, BotInfo.HPicturePMRevoke);
            }
            #endregion  -- Shab图库 --
            #endregion -- 色图 --
            else if (firstMessage == $"{BotInfo.BotName}帮助")
            {
                List <string> lstEnabledFeatures = new List <string>();
                if (BotInfo.SearchEnabled)
                {
                    lstEnabledFeatures.Add("搜图");
                }
                if (BotInfo.TranslateEnabled)
                {
                    lstEnabledFeatures.Add("翻译");
                }
                if (BotInfo.HPictureEnabled)
                {
                    lstEnabledFeatures.Add("GHS");
                }
                //if (BotInfo.QQId == 3246934384)
                //{
                //    lstEnabledFeatures.Add("查手机号");
                //}
                string strHelpResult = $"现在您可以让我{string.Join(",", lstEnabledFeatures)}。\r\n如果您觉得{BotInfo.BotName}好用,请到{BotInfo.BotName}的项目地址 https://github.com/Alex1911-Jiang/GreenOnions 给{BotInfo.BotName}一颗星星。";
                await session.SendFriendMessageAsync(qqId, new PlainMessage(strHelpResult));
            }
            //#region -- 查询手机号(夹带私货) --
            //else if (regexSelectPhone.IsMatch(firstMessage))
            //{
            //    if (BotInfo.QQId == 3246934384)
            //    {
            //        foreach (Match match in regexSelectPhone.Matches(firstMessage))
            //        {
            //            if (match.Groups.Count > 1)
            //            {
            //                string qqNumber = firstMessage.Substring(match.Groups[1].Length);
            //                long lQQNumber;
            //                if (long.TryParse(qqNumber, out lQQNumber))
            //                {
            //                    try
            //                    {
            //                        string result = AssemblyHelper.CallStaticMethod<string>("GreenOnions.QQPhone", "GreenOnions.QQPhone.QQAndPhone", "GetPhoneByQQ", lQQNumber);
            //                        await session.SendFriendMessageAsync(qqId, new PlainMessage(result));
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        await session.SendFriendMessageAsync(qqId, new PlainMessage("查询失败" + ex.Message));
            //                    }
            //                }
            //                else
            //                {
            //                    await session.SendFriendMessageAsync(qqId, new PlainMessage("请输入正确的QQ号码(不支持以邮箱查询)"));
            //                }
            //            }
            //        }
            //    }
            //}
            //#endregion -- 查询手机号(夹带私货) --
        }
Пример #4
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="inMsg">传入的消息体</param>
        /// <param name="senderGroup">消息来自的群号(私聊时为空)</param>
        /// <param name="SendMessage">回发消息方法</param>
        /// <returns></returns>
        public static async Task <bool> HandleMesage(GreenOnionsMessages inMsg, long?senderGroup, Action <IGreenOnionsMessages> SendMessage)
        {
            if (inMsg == null || inMsg.Count == 0)
            {
                return(false);
            }
            GreenOnionsBaseMessage firstMessage = inMsg.First();

            if (firstMessage is GreenOnionsAtMessage atMsg && senderGroup != null && atMsg.AtId == BotInfo.QQId)  //@自己
            {
                for (int i = 1; i < inMsg.Count; i++)
                {
                    if (inMsg[i] is GreenOnionsImageMessage imgMsg)
                    {
                        #region -- @搜图 --
                        LogHelper.WriteInfoLog($"群消息为@搜图");
                        if (BotInfo.SearchEnabled)
                        {
                            SearchPictureHandler.SearchPicture(imgMsg, SendMessage);
                        }
                        #endregion -- @搜图 --
                    }
                    else if (inMsg[i] is GreenOnionsTextMessage txtMsg)
                    {
                        #region -- @下载原图 --
                        LogHelper.WriteInfoLog($"群消息为@下载原图");
                        if (BotInfo.OriginPictureEnabled)
                        {
                            if (string.IsNullOrWhiteSpace(txtMsg.Text))
                            {
                                continue;
                            }
                            _ = SearchPictureHandler.SendPixivOriginPictureWithIdAndP(txtMsg.Text).ContinueWith(callback => SendMessage(callback.Result));
                        }
                        #endregion -- @下载原图 --
                    }
                }
            }

            if (Cache.SearchingPicturesUsers.Keys.Contains(inMsg.SenderId))  //连续搜图
            {
                var imgMsgs = inMsg.OfType <GreenOnionsImageMessage>();
                if (inMsg.Count == imgMsgs.Count())
                {
                    SearchPictureHandler.UpdateSearchTime(inMsg.SenderId);  //刷新搜图超时时间到1分钟
                    foreach (GreenOnionsImageMessage imgMsg in imgMsgs)
                    {
                        SearchPictureHandler.SearchPicture(imgMsg, SendMessage);
                    }
                    return(true);
                }
            }
            else if (Cache.PlayingTicTacToeUsers.ContainsKey(inMsg.SenderId))  //井字棋
            {
                if (inMsg.Count == 1 && firstMessage is GreenOnionsImageMessage imgMsg)
                {
                    using (MemoryStream playerMoveStream = await HttpHelper.DownloadImageAsMemoryStream(ImageHelper.ReplaceGroupUrl(imgMsg.Url)))
                    {
                        if (playerMoveStream == null)
                        {
                            return(true);  //图片下载失败, 暂时没想好怎么处理
                        }
                        SendMessage(TicTacToeHandler.PlayerMoveByBitmap(inMsg.SenderId, playerMoveStream));
                    }
                    return(true);
                }
            }

            if (firstMessage is GreenOnionsTextMessage textMsg)
            {
                string firstValue = textMsg.ToString();

                #region -- 井字棋 --

                if (BotInfo.TicTacToeEnabled)
                {
                    if (regexTicTacToeStart.IsMatch(firstValue))
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发开始井字棋");
                        TicTacToeHandler.StartTicTacToeSession(inMsg.SenderId, SendMessage);
                        return(true);
                    }
                    else if (regexTicTacToeStop.IsMatch(firstValue))
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发结束井字棋");
                        TicTacToeHandler.StopTicTacToeSession(inMsg.SenderId, SendMessage);
                        return(true);
                    }
                    else if ((BotInfo.TicTacToeMoveMode & (int)TicTacToeMoveMode.Nomenclature) != 0 && Cache.PlayingTicTacToeUsers.ContainsKey(inMsg.SenderId) && firstValue.Length == 2)
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发井字棋移动");
                        TicTacToeHandler.PlayerMoveByNomenclature(firstValue, inMsg.SenderId, SendMessage);
                        return(true);
                    }
                }

                #endregion -- 井字棋 --

                #region -- 伪造消息 --
                if (BotInfo.ForgeMessageEnabled && regexForgeMessage.IsMatch(firstValue))
                {
                    LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发伪造消息");
                    ForgeMessageHandler.SendForgeMessage(inMsg, inMsg.SenderId, SendMessage);
                    return(true);
                }
                #endregion -- 伪造消息 --

                #region -- 连续搜图 --
                if (BotInfo.SearchEnabled)
                {
                    if (regexSearchOn.IsMatch(firstValue))
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发开始连续搜图");
                        SearchPictureHandler.SearchOn(inMsg.SenderId, SendMessage);
                        return(true);
                    }
                    if (regexSearchOff.IsMatch(firstValue))
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发结束连续搜图");
                        SearchPictureHandler.SearchOff(inMsg.SenderId, SendMessage);
                        return(true);
                    }
                }
                #endregion -- 连续搜图 --

                #region -- 翻译 --
                if (BotInfo.TranslateEnabled)
                {
                    if (regexTranslateToChinese.IsMatch(firstValue))  //翻译为中文
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发自动识别语言并翻译为中文");
                        TranslateHandler.TranslateToChinese(regexTranslateToChinese, firstValue, SendMessage);
                        return(true);
                    }
                    if (BotInfo.TranslateEngineType == TranslateEngine.Google && regexTranslateTo.IsMatch(firstValue))  //翻译为指定语言(仅限谷歌)
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发自动识别语言并翻译为指定语言");
                        TranslateHandler.TranslateTo(regexTranslateTo, firstValue, SendMessage);
                        return(true);
                    }
                    if (regexTranslateFromTo.IsMatch(firstValue))  //从指定语言翻译为指定语言
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息触发从指定语言翻译为指定语言");
                        TranslateHandler.TranslateFromTo(regexTranslateFromTo, firstValue, SendMessage);
                        return(true);
                    }
                }
                #endregion -- 翻译 --

                #region -- 色图 --
                if (BotInfo.HPictureEnabled)
                {
                    if (regexHPicture.IsMatch(firstValue) || BotInfo.HPictureUserCmd.Contains(firstValue))
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息命中色图命令");
                        if (senderGroup != null)  //群消息
                        {
                            if (!BotInfo.HPictureWhiteOnly || BotInfo.HPictureWhiteGroup.Contains(senderGroup.Value))
                            {
                                LogHelper.WriteInfoLog($"{inMsg.SenderId}有权限使用群色图");

                                if (Cache.CheckGroupLimit(inMsg.SenderId, senderGroup.Value))
                                {
                                    LogHelper.WriteInfoLog($"{inMsg.SenderId}群色图次数耗尽");
                                    SendMessage(new GreenOnionsMessages(BotInfo.HPictureOutOfLimitReply));  //次数用尽
                                    return(true);
                                }
                                if (Cache.CheckGroupCD(inMsg.SenderId, senderGroup.Value))
                                {
                                    LogHelper.WriteInfoLog($"{inMsg.SenderId}群色图冷却中");
                                    SendMessage(new GreenOnionsMessages(BotInfo.HPictureCDUnreadyReply));  //冷却中
                                    return(true);
                                }

                                if (BotInfo.EnabledHPictureSource.Count > 0)
                                {
                                    if (BotInfo.HPictureUserCmd.Contains(firstValue))
                                    {
                                        _ = HPictureHandler.SendOnlyOneHPictures(inMsg.SenderId, senderGroup, SendMessage);
                                    }
                                    else
                                    {
                                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息进入群色图处理事件");
                                        _ = HPictureHandler.SendHPictures(inMsg.SenderId, senderGroup, regexHPicture.Match(firstValue), SendMessage);
                                    }
                                }
                            }
                        }
                        else  //私聊消息
                        {
                            if (BotInfo.HPictureAllowPM)
                            {
                                LogHelper.WriteInfoLog($"{inMsg.SenderId}有权限使用私聊色图");
                                if (Cache.CheckPMLimit(inMsg.SenderId))
                                {
                                    LogHelper.WriteInfoLog($"{inMsg.SenderId}私聊色图次数耗尽");
                                    SendMessage(new GreenOnionsMessages(BotInfo.HPictureOutOfLimitReply));  //次数用尽
                                    return(true);
                                }
                                if (Cache.CheckPMCD(inMsg.SenderId))
                                {
                                    LogHelper.WriteInfoLog($"{inMsg.SenderId}私聊色图冷却中");
                                    SendMessage(new GreenOnionsMessages(BotInfo.HPictureCDUnreadyReply));  //冷却中
                                    return(true);
                                }

                                if (BotInfo.EnabledHPictureSource.Count > 0)
                                {
                                    if (BotInfo.HPictureUserCmd.Contains(firstValue))
                                    {
                                        _ = HPictureHandler.SendOnlyOneHPictures(inMsg.SenderId, senderGroup, SendMessage);
                                    }
                                    else
                                    {
                                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息进入私聊色图处理事件");
                                        _ = HPictureHandler.SendHPictures(inMsg.SenderId, null, regexHPicture.Match(firstValue), SendMessage);
                                    }
                                }
                                else
                                {
                                    LogHelper.WriteInfoLog($"没有启用任何图库");
                                }
                            }
                        }
                        return(true);
                    }
                }
                #endregion -- 色图 --

                #region -- 下载Pixiv原图 --
                if (BotInfo.OriginPictureEnabled)
                {
                    if (regexDownloadPixivOriginPicture.IsMatch(firstValue))
                    {
                        LogHelper.WriteInfoLog($"{inMsg.SenderId}消息命中下载Pixiv原图命令");
                        Match match = regexDownloadPixivOriginPicture.Matches(firstValue).FirstOrDefault();
                        if (match.Groups.Count > 1)
                        {
                            string strId = firstValue.Substring(match.Groups[0].Length);
                            LogHelper.WriteInfoLog($"{inMsg.SenderId}下载id={strId}的原图");
                            _ = SearchPictureHandler.SendPixivOriginPictureWithIdAndP(strId).ContinueWith(callback => SendMessage(callback.Result));
                        }
                        return(true);
                    }
                }
                #endregion -- 下载Pixiv原图 --

                #region -- 帮助 --
                if (regexHelp.IsMatch(firstValue))
                {
                    LogHelper.WriteInfoLog($"{inMsg.SenderId}消息命中帮助命令");
                    SendMessage(HelpHandler.Helps(regexHelp, firstValue, senderGroup));
                    return(true);
                }
                #endregion -- 帮助 --

                #region -- 查询手机号(夹带私货) --
                if (regexSelectPhone.IsMatch(firstValue))
                {
                    if (BotInfo.QQId == 3246934384 || BotInfo.QQId == 3095752458)
                    {
                        string qqNumber = firstValue.Substring(regexSelectPhone.Matches(firstValue).First().Length);
                        long   lQQNumber;
                        if (long.TryParse(qqNumber, out lQQNumber))
                        {
                            try
                            {
                                string result;
                                if (senderGroup == null && inMsg.SenderId == lQQNumber) // 私聊
                                {
                                    result = AssemblyHelper.CallStaticMethod <string>("GreenOnions.QQPhone", "GreenOnions.QQPhone.QQAndPhone", "GetSelfPhoneByQQ", lQQNumber);
                                }
                                //result = QQPhone.QQAndPhone.GetSelfPhoneByQQ(lQQNumber);
                                else  //群
                                {
                                    result = AssemblyHelper.CallStaticMethod <string>("GreenOnions.QQPhone", "GreenOnions.QQPhone.QQAndPhone", "GetPhoneByQQ", lQQNumber);
                                }
                                //result = QQPhone.QQAndPhone.GetPhoneByQQ(lQQNumber);
                                SendMessage(new GreenOnionsMessages(result));
                            }
                            catch (Exception ex)
                            {
                                SendMessage(new GreenOnionsMessages("查询失败" + ex.Message));
                            }
                        }
                        else
                        {
                            SendMessage(new GreenOnionsMessages("请输入正确的QQ号码(不支持以邮箱查询)"));
                        }
                    }
                    return(true);
                }
                #endregion -- 查询手机号(夹带私货) --

                #region -- 自动翻译 --
                if (BotInfo.AutoTranslateGroupMemoriesQQ.Contains(inMsg.SenderId))
                {
                    string tranStr = await GoogleTranslateHelper.TranslateToChinese(string.Join('\n', inMsg.OfType <GreenOnionsTextMessage>().Select(m => m.Text)));

                    try
                    {
                        SendMessage(new GreenOnionsMessages(tranStr));
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteErrorLogWithUserMessage("自动翻译失败", ex);
                    }
                    return(true);
                }
                ;
                #endregion -- 自动翻译 --

                LogHelper.WriteInfoLog($"{inMsg.SenderId}消息没有命中任何逻辑命令");
            }

            if (PluginManager.Message(inMsg, senderGroup, SendMessage))
            {
                return(true);
            }

            if (BotInfo.PmAutoSearch && senderGroup is null && BotInfo.SearchEnabled)  //私聊自动搜图
            {
                for (int i = 0; i < inMsg.Count; i++)
                {
                    if (inMsg[i] is GreenOnionsImageMessage imgMsg)
                    {
                        SearchPictureHandler.SearchPicture(imgMsg, SendMessage);
                    }
                }
            }

            #region -- 复读 --
            if (senderGroup != null && (BotInfo.SuccessiveRepeatEnabled || BotInfo.RandomRepeatEnabled))
            {
                if (inMsg.Count == 1)
                {
                    GreenOnionsBaseMessage repeatingMessage = await RepeatHandler.Repeating(inMsg.First(), senderGroup.Value);

                    if (repeatingMessage != null)
                    {
                        GreenOnionsMessages repeatMessage = new GreenOnionsMessages(repeatingMessage);
                        repeatMessage.Reply = false;
                        SendMessage(repeatMessage);
                        return(true);
                    }
                }
            }
            #endregion -- 复读 --

            return(false);
        }