コード例 #1
0
        public static GreenOnionsMessages SendBitmapAfterMove(long qqId, Bitmap nowStepBmp, int?winOrLostMsg)
        {
            using (MemoryStream tempMs = new MemoryStream())
            {
                nowStepBmp.Save(tempMs, ImageFormat.Jpeg);
                using (MemoryStream ms = new MemoryStream(tempMs.ToArray()))
                {
                    GreenOnionsMessages outMsg = new GreenOnionsMessages();
                    outMsg.Add(ms);

                    if (winOrLostMsg != null)
                    {
                        switch (winOrLostMsg)
                        {
                        case -1:     //bot获胜
                            outMsg.Add(BotInfo.TicTacToeBotWinReply.ReplaceGreenOnionsTags());
                            break;

                        case 0:      //平局
                            outMsg.Add(BotInfo.TicTacToeDrawReply.ReplaceGreenOnionsTags());
                            break;

                        case 1:      //玩家获胜
                            outMsg.Add(BotInfo.TicTacToePlayerWinReply.ReplaceGreenOnionsTags());
                            break;
                        }
                        PlayingTicTacToeSessions.Remove(qqId);
                        Cache.PlayingTicTacToeUsers.TryRemove(qqId, out _);
                    }
                    return(outMsg);
                }
            }
        }
コード例 #2
0
        public static GreenOnionsMessages ToOnionsMessages(this IChatMessage[] miraiMessage, long senderId, string senderName)
        {
            GreenOnionsMessages greenOnionsMessages = new GreenOnionsMessages();

            for (int i = 0; i < miraiMessage.Length; i++)
            {
                if (miraiMessage[i] is IAtMessage atMsg)
                {
                    greenOnionsMessages.Add(new GreenOnionsAtMessage(atMsg.Target, atMsg.Display));
                }
                else if (miraiMessage[i] is IPlainMessage plainMsg)
                {
                    greenOnionsMessages.Add(plainMsg.ToString());
                }
                else if (miraiMessage[i] is IImageMessage imageMsg)
                {
                    greenOnionsMessages.Add(new GreenOnionsImageMessage(imageMsg.Url, imageMsg.ImageId));
                }
            }

            greenOnionsMessages.SenderId   = senderId;
            greenOnionsMessages.SenderName = senderName;
            return(greenOnionsMessages);
        }
コード例 #3
0
        public static GreenOnionsMessages ToOnionsMessages(this MessageBody miraiMessage, long senderId, string senderName, long?senderGroup, SoraApi api)
        {
            GreenOnionsMessages greenOnionsMessages = new GreenOnionsMessages();

            for (int i = 0; i < miraiMessage.Count; i++)
            {
                if (miraiMessage[i].Data is AtSegment atMsg)
                {
                    //获取@群名片
                    if (long.TryParse(atMsg.Target, out long atId))
                    {
                        var apiResult = api.GetGroupMemberList(senderGroup.Value).GetAwaiter().GetResult();
                        List <GroupMemberInfo> groupMemberInfos = apiResult.groupMemberList;
                        GroupMemberInfo        targetQQ         = groupMemberInfos.Where(m => m.UserId == atId).FirstOrDefault();
                        string nickName = targetQQ?.Nick;
                        greenOnionsMessages.Add(new GreenOnionsAtMessage(atId, nickName));
                    }
                    else
                    {
                        greenOnionsMessages.Add(new GreenOnionsAtMessage(atId, atMsg.Name));
                    }
                }
                else if (miraiMessage[i].Data is TextSegment textMsg)
                {
                    greenOnionsMessages.Add(textMsg.Content);
                }
                else if (miraiMessage[i].Data is ImageSegment imageMsg)
                {
                    greenOnionsMessages.Add(new GreenOnionsImageMessage(imageMsg.Url, imageMsg.ImgFile));
                }
            }

            greenOnionsMessages.SenderId   = senderId;
            greenOnionsMessages.SenderName = senderName;
            return(greenOnionsMessages);
        }
コード例 #4
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);
            }
        }
コード例 #5
0
        private static async Task SendELFHPicture(long senderId, long?senderGroup, string strHttpRequestUrl, Action <GreenOnionsMessages> SendMessage)
        {
            List <GreenOnionsMessages> outMessages = null;

            if (BotInfo.HPictureSendByForward)
            {
                outMessages = new List <GreenOnionsMessages>();
            }

            string resultValue = "";

            try
            {
                resultValue = await HttpHelper.GetHttpResponseStringAsync(strHttpRequestUrl);

                JArray ja = (JArray)JsonConvert.DeserializeObject(resultValue);
                if (ja.Count == 0)
                {
                    SendMessage(BotInfo.HPictureNoResultReply);  //没有结果
                }

                IEnumerable <ELFHPictureItem> enumImg = ja.Select(i => new ELFHPictureItem(i["id"].ToString(), i["link"].ToString().Replace("pixiv.cat", "pixiv.re"), i["source"].ToString(), string.Join(",", i["jp_tag"].Select(s => s.ToString())), string.Join(",", i["zh_tags"].Select(s => s.ToString())), i["author"].ToString()));

                //包含twimg.com的图墙内无法访问, 暂时不处理
                foreach (ELFHPictureItem imgItem in enumImg)
                {
                    GreenOnionsMessages outMessage = new GreenOnionsMessages();
                    if (BotInfo.HPictureSendUrl)
                    {
                        string addresses;
                        if (BotInfo.HPictureSendTags)
                        {
                            addresses = $"{imgItem.Source}\r\n中文标签:{imgItem.Zh_Tags}\r\n日文标签:{imgItem.Jp_Tag}\r\n作者:{imgItem.Author}";
                        }
                        else
                        {
                            addresses = imgItem.Source;
                        }
                        outMessage.Add(addresses);
                        RecordLimit(senderId, senderGroup, LimitType.Frequency);
                    }

                    GreenOnionsImageMessage imgMsg = CreateOnceELFHPicture(imgItem);

                    #region  -- 撤回时间 --
                    if (BotInfo.RevokeBeautyPicture)
                    {
                        if (senderGroup == null)
                        {
                            outMessage.RevokeTime = BotInfo.HPicturePMRevoke;  //私聊撤回
                        }
                        else
                        {
                            if (BotInfo.HPictureWhiteGroup.Contains(senderGroup.Value))  //白名单群撤回
                            {
                                outMessage.RevokeTime = BotInfo.HPictureWhiteRevoke;
                            }
                            else
                            {
                                outMessage.RevokeTime = BotInfo.HPictureRevoke;  //普通群撤回
                            }
                        }
                    }
                    #endregion  -- 撤回时间 --

                    outMessage.Add(imgMsg);
                    if (BotInfo.HPictureSendByForward)
                    {
                        outMessages.Add(outMessage);
                    }
                    else
                    {
                        SendMessage(outMessage);
                    }
                    RecordLimit(senderId, senderGroup, LimitType.Count);
                }

                if (BotInfo.HPictureSendByForward && outMessages.Count > 0)
                {
                    GreenOnionsForwardMessage[] forwardMessages = outMessages.Select(msg => new GreenOnionsForwardMessage(BotInfo.QQId, BotInfo.BotName, msg)).ToArray();
                    SendMessage(forwardMessages);  //合并转发
                }
            }
            catch (Exception ex)
            {
                SendMessage(BotInfo.HPictureErrorReply + ex.Message);  //发生错误
            }
        }
コード例 #6
0
        private static async Task SendLoliconHPhicture(long senderId, long?senderGroup, string strHttpRequestUrl, string sizeUrlName, Action <GreenOnionsMessages> SendMessage)
        {
            string resultValue  = string.Empty;
            string errorMessage = string.Empty;

            try
            {
                resultValue = await HttpHelper.GetHttpResponseStringAsync(strHttpRequestUrl);
            }
            catch (Exception ex)
            {
                errorMessage = BotInfo.HPictureErrorReply + ex.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                SendMessage(errorMessage);
            }

            JObject jo  = (JObject)JsonConvert.DeserializeObject(resultValue);
            JToken  jt  = jo["data"];
            string  err = jo["error"].ToString();

            if (!string.IsNullOrWhiteSpace(err))//Api错误
            {
                SendMessage(BotInfo.HPictureErrorReply + err);
            }

            if (jt.Count() == 0)                            //没找到对应词条的色图;
            {
                SendMessage(BotInfo.HPictureNoResultReply); //没有结果
            }

            IEnumerable <LoliconHPictureItem> enumImg = jt.Select(i => new LoliconHPictureItem(
                                                                      i["p"].ToString(),
                                                                      i["pid"].ToString(),
                                                                      i["urls"][sizeUrlName].ToString(),
                                                                      i["title"].ToString(),
                                                                      i["author"].ToString(),
                                                                      string.Join(",", (i["tags"] as JArray)),
                                                                      @$ "https://www.pixiv.net/artworks/{i[" pid "]}(p{i[" p "]})")
                                                                  );

            if (enumImg == null)
            {
                LogHelper.WriteErrorLog("Lolicon响应解析失败");
                SendMessage(BotInfo.HPictureErrorReply);  //发生错误
            }

            List <GreenOnionsMessages> outMessages = null;

            if (BotInfo.HPictureSendByForward)
            {
                outMessages = new List <GreenOnionsMessages>();
            }

            RecordLimit(senderId, senderGroup, LimitType.Frequency);
            foreach (LoliconHPictureItem imgItem in enumImg)
            {
                GreenOnionsMessages outMessage = new GreenOnionsMessages();
                if (BotInfo.HPictureSendUrl)
                {
                    string addresses;
                    if (BotInfo.HPictureSendTags)
                    {
                        addresses = $@"https://www.pixiv.net/artworks/{imgItem.ID} (p{imgItem.P})\r\n标题:{imgItem.Title}\r\n作者:{imgItem.Author}\r\n标签:{imgItem.Tags}";
                    }
                    else
                    {
                        addresses = $@"https://www.pixiv.net/artworks/{imgItem.ID} (p{imgItem.P})";
                    }
                    outMessage.Add(addresses);
                }

                GreenOnionsImageMessage imgMsg = CreateOnceLoliconHPicture(imgItem);

                #region  -- 撤回时间 --
                if (senderGroup == null)
                {
                    outMessage.RevokeTime = BotInfo.HPicturePMRevoke;  //私聊撤回
                }
                else
                {
                    if (BotInfo.HPictureWhiteGroup.Contains(senderGroup.Value))  //白名单群撤回
                    {
                        outMessage.RevokeTime = BotInfo.HPictureWhiteRevoke;
                    }
                    else
                    {
                        outMessage.RevokeTime = BotInfo.HPictureRevoke;  //普通群撤回
                    }
                }
                #endregion  -- 撤回时间 --

                outMessage.Add(imgMsg);
                if (BotInfo.HPictureSendByForward)
                {
                    outMessages.Add(outMessage);
                }
                else
                {
                    SendMessage(outMessage);
                }
                RecordLimit(senderId, senderGroup, LimitType.Count);
            }

            if (BotInfo.HPictureSendByForward && outMessages.Count > 0)
            {
                GreenOnionsForwardMessage[] forwardMessages = outMessages.Select(msg => new GreenOnionsForwardMessage(BotInfo.QQId, BotInfo.BotName, msg)).ToArray();
                SendMessage(forwardMessages);  //合并转发
            }
        }