Ejemplo n.º 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("数据库", "更新动态记录时发生了数据库错误");
                }
            }
        }