Exemple #1
0
        private static async ValueTask GetDynamic(SoraApi soraApi, long biliUser, List <long> groupId,
                                                  SubscriptionDBHelper dbHelper)
        {
            string        textMessage;
            Dynamic       biliDynamic;
            List <string> imgList = new();

            //获取动态文本
            try
            {
                var cardData = DynamicAPIs.GetLatestDynamic(biliUser);
                switch (cardData.cardType)
                {
                //检查动态类型
                case CardType.PlainText:
                    PlainTextCard plainTextCard = (PlainTextCard)cardData.cardObj;
                    textMessage = plainTextCard.ToString();
                    biliDynamic = plainTextCard;
                    break;

                case CardType.TextAndPic:
                    TextAndPicCard textAndPicCard = (TextAndPicCard)cardData.cardObj;
                    imgList.AddRange(textAndPicCard.ImgList);
                    textMessage = textAndPicCard.ToString();
                    biliDynamic = textAndPicCard;
                    break;

                case CardType.Forward:
                    ForwardCard forwardCard = (ForwardCard)cardData.cardObj;
                    textMessage = forwardCard.ToString();
                    biliDynamic = forwardCard;
                    break;

                case CardType.Video:
                    VideoCard videoCard = (VideoCard)cardData.cardObj;
                    imgList.Add(videoCard.CoverUrl);
                    textMessage = videoCard.ToString();
                    biliDynamic = videoCard;
                    break;

                case CardType.Error:
                    //Log.Error("动态获取", $"ID:{biliUser}的动态获取失败");
                    return;

                default:
                    Log.Debug("动态获取", $"ID:{biliUser}的动态获取成功,动态类型未知");
                    foreach (var gid in groupId)
                    {
                        if (!dbHelper.UpdateDynamic(gid, biliUser, BotUtils.GetNowStampLong()))
                        {
                            Log.Error("数据库", "更新动态记录时发生了数据库错误");
                        }
                    }

                    return;
                }
            }
            catch (Exception e)
            {
                Log.Error("获取动态更新时发生错误", Log.ErrorLogBuilder(e));
                return;
            }

            //获取用户信息
            UserInfo sender = biliDynamic.GetUserInfo();

            Log.Debug("动态获取", $"{sender.UserName}的动态获取成功");
            //检查是否是最新的
            List <long> targetGroups = groupId
                                       .Where(@group => !dbHelper.IsLatestDynamic(@group, sender.Uid,
                                                                                  biliDynamic.UpdateTime))
                                       .ToList();

            //没有群需要发送消息
            if (targetGroups.Count == 0)
            {
                Log.Debug("动态获取", $"{sender.UserName}的动态已是最新");
                return;
            }

            //构建消息
            List <CQCode> msgList    = new();
            StringBuilder msgBuilder = new();

            msgBuilder.Append("获取到了来自 ");
            msgBuilder.Append(sender.UserName);
            msgBuilder.Append(" 的动态:\r\n");
            msgBuilder.Append(textMessage);
            msgList.Add(CQCode.CQText(msgBuilder.ToString()));
            //添加图片
            imgList.ForEach(imgUrl => msgList.Add(CQCode.CQImage(imgUrl)));
            msgBuilder.Clear();
            msgBuilder.Append("\r\n更新时间:");
            msgBuilder.Append(biliDynamic.UpdateTime.ToString("MM-dd HH:mm:ss"));
            msgList.Add(CQCode.CQText(msgBuilder.ToString()));
            //向未发生消息的群发送消息
            foreach (var targetGroup in targetGroups)
            {
                Log.Info("动态获取", $"获取到{sender.UserName}的最新动态,向群{targetGroup}发送动态信息");
                await soraApi.SendGroupMessage(targetGroup, msgList);

                if (!dbHelper.UpdateDynamic(targetGroup, sender.Uid, biliDynamic.UpdateTime))
                {
                    Log.Error("数据库", "更新动态记录时发生了数据库错误");
                }
            }
        }
Exemple #2
0
        private static Task GetDynamic(CQApi cqApi, long biliUser, List <long> groupId, SubscriptionDBHelper dbHelper)
        {
            string  message;
            Dynamic biliDynamic;

            //获取动态文本
            try
            {
                JObject cardData = DynamicAPIs.GetBiliDynamicJson((ulong)biliUser, out CardType cardType);
                switch (cardType)
                {
                //检查动态类型
                case CardType.PlainText:
                    PlainTextCard plainTextCard = new PlainTextCard(cardData)
                    {
                        ContentType = ContentType.CQCode
                    };
                    message     = plainTextCard.ToString();
                    biliDynamic = plainTextCard;
                    break;

                case CardType.TextAndPic:
                    TextAndPicCard textAndPicCard = new TextAndPicCard(cardData)
                    {
                        ContentType = ContentType.CQCode
                    };
                    message     = textAndPicCard.ToString();
                    biliDynamic = textAndPicCard;
                    break;

                default:
                    ConsoleLog.Debug("动态获取", $"ID:{biliUser}的动态获取成功,动态类型未知");
                    return(Task.CompletedTask);
                }
            }
            catch (Exception e)
            {
                ConsoleLog.Error("获取动态更新时发生错误", ConsoleLog.ErrorLogBuilder(e));
                return(Task.CompletedTask);
            }
            //获取用户信息
            UserInfo sender = biliDynamic.GetUserInfo();

            ConsoleLog.Info("动态获取", $"{sender.UserName}的动态获取成功");
            //检查是否是最新的

            List <long> targetGroups = new List <long>();

            foreach (long group in groupId)
            {
                //检查是否已经发送过消息
                if (!dbHelper.IsLatest(group, sender.Uid, biliDynamic.UpdateTime))
                {
                    targetGroups.Add(group);
                }
            }
            //没有群需要发送消息
            if (targetGroups.Count == 0)
            {
                ConsoleLog.Info("动态获取", $"{sender.UserName}的动态已是最新");
                return(Task.CompletedTask);
            }
            //向未发生消息的群发送消息
            string messageToSend = MsgBuilder(sender, message, biliDynamic);

            foreach (long targetGroup in targetGroups)
            {
                ConsoleLog.Info("动态获取", $"向群{targetGroup}发送动态信息");
                cqApi.SendGroupMessage(targetGroup, messageToSend);
                dbHelper.Update(targetGroup, sender.Uid, biliDynamic.UpdateTime);
            }
            return(Task.CompletedTask);
        }
Exemple #3
0
        private static Task GetDynamic(SoraApi soraApi, long biliUser, List <long> groupId, SubscriptionDBHelper dbHelper)
        {
            string        textMessage;
            Dynamic       biliDynamic;
            List <string> imgList = new List <string>();

            //获取动态文本
            try
            {
                JObject cardData = DynamicAPIs.GetBiliDynamicJson((ulong)biliUser, out CardType cardType);
                switch (cardType)
                {
                //检查动态类型
                case CardType.PlainText:
                    PlainTextCard plainTextCard = new PlainTextCard(cardData);
                    textMessage = plainTextCard.ToString();
                    biliDynamic = plainTextCard;
                    break;

                case CardType.TextAndPic:
                    TextAndPicCard textAndPicCard = new TextAndPicCard(cardData);
                    imgList.AddRange(textAndPicCard.ImgList);
                    textMessage = textAndPicCard.ToString();
                    biliDynamic = textAndPicCard;
                    break;

                default:
                    ConsoleLog.Warning("动态获取", $"ID:{biliUser}的动态获取成功,动态类型未知");
                    return(Task.CompletedTask);
                }
            }
            catch (Exception e)
            {
                ConsoleLog.Error("获取动态更新时发生错误", ConsoleLog.ErrorLogBuilder(e));
                return(Task.CompletedTask);
            }
            //获取用户信息
            BiliUserInfo sender = biliDynamic.GetUserInfo();

            ConsoleLog.Debug("动态获取", $"{sender.UserName}的动态获取成功");
            //检查是否是最新的

            List <long> targetGroups = new List <long>();

            foreach (long group in groupId)
            {
                //检查是否已经发送过消息
                if (!dbHelper.IsLatest(group, sender.Uid, biliDynamic.UpdateTime))
                {
                    targetGroups.Add(group);
                }
            }
            //没有群需要发送消息
            if (targetGroups.Count == 0)
            {
                ConsoleLog.Debug("动态获取", $"{sender.UserName}的动态已是最新");
                return(Task.CompletedTask);
            }
            //构建消息
            List <CQCode> msgList = new List <CQCode>();
            StringBuilder sb      = new StringBuilder();

            sb.Append("获取到了来自 ");
            sb.Append(sender.UserName);
            sb.Append(" 的动态:\r\n");
            sb.Append(textMessage);
            msgList.Add(CQCode.CQText(sb.ToString()));
            //添加图片
            imgList.ForEach(imgUrl => msgList.Add(CQCode.CQImage(imgUrl)));
            sb.Clear();
            sb.Append("\r\n更新时间:");
            sb.Append(biliDynamic.UpdateTime);
            msgList.Add(CQCode.CQText(sb.ToString()));
            //向未发生消息的群发送消息
            foreach (long targetGroup in targetGroups)
            {
                ConsoleLog.Info("动态获取", $"获取到{sender.UserName}的最新动态,向群{targetGroup}发送动态信息");
                soraApi.SendGroupMessage(targetGroup, msgList);
                if (!dbHelper.Update(targetGroup, sender.Uid, biliDynamic.UpdateTime))
                {
                    ConsoleLog.Error("数据库", "更新动态记录时发生了数据库错误");
                }
            }
            return(Task.CompletedTask);
        }