public async IAsyncEnumerable <ReleaseMessage> CheckSubscriptions() { IAsyncEnumerable <DiscordChannel> channels = subscriptionService.GetAllChannels().Where(channel => !channel.Ignore); await foreach (DiscordChannel channel in channels) { IEnumerable <(IEnumerable <string> newReleases, SubscribedArtist currentArtist)> results = await Task.WhenAll(channel.SubscribedArtists.Select(async savedArtist => { SubscribedArtist currentArtist = await musicInfoService.RetrieveSubscribedArtist(savedArtist.Id); IEnumerable <string> savedReleases = savedArtist.Albums; IEnumerable <string> currentReleases = currentArtist.Albums; return(currentReleases.Except(savedReleases), currentArtist); })); IEnumerable <(IEnumerable <string> newReleases, SubscribedArtist currentArtist)> artistsWithNewReleases = results.Where(artist => artist.newReleases.Any()); DiscordChannel updatedChannel = artistsWithNewReleases .Aggregate(channel, (currentChannel, artist) => currentChannel.WithUpdatedSubscribedArtist(artist.currentArtist)); await subscriptionService.UpdateChannel(updatedChannel); foreach ((IEnumerable <string> newReleases, SubscribedArtist currentArtist) in artistsWithNewReleases) { yield return(new ReleaseMessage( releases: await Task.WhenAll(newReleases.Select(newRelease => musicInfoService.GetReleaseById(newRelease))), artistName: currentArtist.Name, guildId: Convert.ToUInt64(updatedChannel.GuildId), channnelId: Convert.ToUInt64(updatedChannel.Id))); } } }
public async Task <SubscribedArtist?> SelectArtistToSubscribeTo(ulong channelId, int selection) { DiscordChannel?channel = await subscriptionService.GetChannel(channelId); if (channel?.Ignore ?? false) { return(null); } string?artistId = channel?.CurrentArtistOptions?[selection]; if (artistId is null) { return(null); } SubscribedArtist newArtist = await musicInfoService.RetrieveSubscribedArtist(artistId); if (channel is null) { return(null); } DiscordChannel updatedChannel = channel.WithSubscribedArtistAdded(newArtist); DiscordChannel channelWithoutCurrent = updatedChannel.WithNewCurrentArtistOptions(new Dictionary <int, string>()); await subscriptionService.UpdateChannel(channelWithoutCurrent); return(newArtist); }