/// <summary> /// 自动获取B站动态 /// </summary> /// <param name="cqApi">CQApi</param> public static async void BiliUpdateCheck(CQApi cqApi) { //读取配置文件 Config config = new Config(cqApi.GetLoginQQ().Id); Module moduleEnable = config.LoadedConfig.ModuleSwitch; List <GroupSubscription> Subscriptions = config.LoadedConfig.SubscriptionConfig.GroupsConfig; //数据库 SubscriptionDBHelper dbHelper = new SubscriptionDBHelper(cqApi); //检查模块是否启用 if (!moduleEnable.Bili_Subscription || !moduleEnable.PCR_Subscription) { return; } foreach (GroupSubscription subscription in Subscriptions) { //PCR动态订阅 if (subscription.PCR_Subscription) { await GetDynamic(cqApi, 353840826, subscription.GroupId, dbHelper); } //臭DD的订阅 foreach (long biliUser in subscription.SubscriptionId) { await GetDynamic(cqApi, biliUser, subscription.GroupId, dbHelper); } } }
/// <summary> /// 自动获取B站动态 /// </summary> /// <param name="connectEventArgs">连接事件参数</param> public static async void BiliUpdateCheck(ConnectEventArgs connectEventArgs) { //读取配置文件 Config config = new Config(connectEventArgs.LoginUid); config.LoadUserConfig(out UserConfig loadedConfig); ModuleSwitch moduleEnable = loadedConfig.ModuleSwitch; List <GroupSubscription> Subscriptions = loadedConfig.SubscriptionConfig.GroupsConfig; //数据库 SubscriptionDBHelper dbHelper = new SubscriptionDBHelper(connectEventArgs.LoginUid); //检查模块是否启用 if (!moduleEnable.Bili_Subscription) { return; } foreach (GroupSubscription subscription in Subscriptions) { //PCR动态订阅 if (subscription.PCR_Subscription) { await GetDynamic(connectEventArgs.SoraApi, 353840826, subscription.GroupId, dbHelper); } //臭DD的订阅 foreach (long biliUser in subscription.SubscriptionId) { await GetDynamic(connectEventArgs.SoraApi, biliUser, subscription.GroupId, dbHelper); } } }
private static async ValueTask GetLiveStatus(SoraApi soraApi, long biliUser, List <long> groupId, SubscriptionDBHelper dbHelper) { LiveInfo live; UserSpaceInfo biliUserInfo; //获取数据 try { biliUserInfo = UserApis.GetLiveRoomInfo(biliUser); live = LiveAPIs.GetLiveRoomInfo(biliUserInfo.LiveRoomInfo.ShortId); } catch (Exception e) { Log.Error("获取直播状态时发生错误", Log.ErrorLogBuilder(e)); return; } //需要更新数据的群 Dictionary <long, LiveStatusType> updateDict = groupId .Where(gid => dbHelper.GetLastLiveStatus(gid, biliUser) != live.LiveStatus) .ToDictionary(gid => gid, _ => live.LiveStatus); //更新数据库 foreach (var status in updateDict) { if (!dbHelper.UpdateLiveStatus(status.Key, biliUser, live.LiveStatus)) { Log.Error("Database", "更新直播订阅数据失败"); } } //需要消息提示的群 var targetGroup = updateDict.Where(group => group.Value == LiveStatusType.Online) .Select(group => group.Key) .ToList(); if (targetGroup.Count == 0) { return; } //构建提示消息 MessageBody message = $"{biliUserInfo.Name} 正在直播!\r\n{biliUserInfo.LiveRoomInfo.Title}" + CQCodes.CQImage(biliUserInfo.LiveRoomInfo.CoverUrl) + $"直播间地址:{biliUserInfo.LiveRoomInfo.LiveUrl}"; foreach (var gid in targetGroup) { Log.Info("直播订阅", $"获取到{biliUserInfo.Name}正在直播,向群[{gid}]发送动态信息"); await soraApi.SendGroupMessage(gid, message); } }
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); }
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("数据库", "更新动态记录时发生了数据库错误"); } } }
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); }