Example #1
0
 public void TryRequestGlobalResources()
 {
     Task.Run(async() =>
     {
         await _globalLock.WaitAsync();
         try
         {
             await _twitchBadgeProvider.TryRequestResources(null);
             await _bttvDataProvider.TryRequestResources(null);
             await _ffzDataProvider.TryRequestResources(null);
             //_logger.LogInformation("Finished caching global emotes/badges.");
         }
         catch (Exception ex)
         {
             _logger.LogError(ex, $"An exception occurred while trying to request global Twitch resources.");
         }
         finally
         {
             _globalLock.Release();
         }
     });
 }
        public void TryRequestChannelResources(TwitchChannel channel, Action <Dictionary <string, IChatResourceData> > channelResourceDataCached)
        {
            Task.Run(async() =>
            {
                await _channelLock.WaitAsync();
                try
                {
                    if (!_channelDataCached.Contains(channel.Id))
                    {
                        var roomId = channel.Roomstate.RoomId;
                        await _twitchBadgeProvider.TryRequestResources(roomId);
                        await _twitchCheermoteProvider.TryRequestResources(roomId);
                        await _bttvDataProvider.TryRequestResources(channel.Id);
                        await _ffzDataProvider.TryRequestResources(channel.Id);

                        var ret = new Dictionary <string, IChatResourceData>();
                        _twitchBadgeProvider.Resources.ToList().ForEach(x =>
                        {
                            var parts = x.Key.Split(new[]
                            {
                                '_'
                            }, 2);
                            ret[$"{x.Value.Type}_{(parts.Length > 1 ? parts[1] : parts[0])}"] = x.Value;
                        });

                        _twitchCheermoteProvider.Resources.ToList().ForEach(x =>
                        {
                            var parts = x.Key.Split(new[]
                            {
                                '_'
                            }, 2);
                            foreach (var tier in x.Value.Tiers)
                            {
                                ret[$"{tier.Type}_{(parts.Length > 1 ? parts[1] : parts[0])}{tier.MinBits}"] = tier;
                            }
                        });

                        _bttvDataProvider.Resources.ToList().ForEach(x =>
                        {
                            var parts = x.Key.Split(new[]
                            {
                                '_'
                            }, 2);
                            ret[$"{x.Value.Type}_{(parts.Length > 1 ? parts[1] : parts[0])}"] = x.Value;
                        });

                        _ffzDataProvider.Resources.ToList().ForEach(x =>
                        {
                            var parts = x.Key.Split(new[]
                            {
                                '_'
                            }, 2);
                            ret[$"{x.Value.Type}_{(parts.Length > 1 ? parts[1] : parts[0])}"] = x.Value;
                        });

                        channelResourceDataCached?.Invoke(ret);
                        _channelDataCached.Add(channel.Id);
                        //_logger.LogInformation($"Finished caching emotes for channel {channel.Id}.");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"An exception occurred while trying to request Twitch channel resources for {channel.Id}.");
                }
                finally
                {
                    _channelLock.Release();
                }
            });
        }