private void SonarrWebhooksService_OnDownload(object sender, OnDownloadSonarrEvent e)
 {
     if (Episodes.TryGetValue(e.Series.Id, out var ep))
     {
         ep.Add(e);
     }
     else
     {
         Episodes[e.Series.Id] = new Episodes(e);
     }
 }
        void loop(object param)
        {
            if (!(param is CancellationToken token))
            {
                return;
            }
            while (!token.IsCancellationRequested)
            {
                try
                {
                    Lock.WaitOne();
                    var rem = new List <int>();
                    foreach (var keypair in Episodes)
                    {
                        var value = keypair.Value;
                        var diff  = DateTime.Now - value.Last;
                        if (diff.TotalMinutes >= 15)
                        {
                            var embed    = value.ToEmbed();
                            var toRemove = new List <ITextChannel>();
                            foreach (var txt in Channels)
                            {
                                var chnl = txt.Channel;
                                if (chnl is NullTextChannel)
                                {
                                    toRemove.Add(chnl);
                                    continue;
                                }
                                var send = ShouldSendInChannel(keypair.Value.Series.Id, txt).Result;
                                if (send == false)
                                {
                                    continue;
                                }
                                try
                                {
                                    chnl.SendMessageAsync(embed: embed);
                                } catch (Exception ex)
                                {
                                    toRemove.Add(chnl);
                                    Program.LogMsg("Sonarr", ex);
                                }
                            }
                            rem.Add(keypair.Key);
                        }
                    }
                    foreach (var x in rem)
                    {
                        Episodes.Remove(x);
                    }
                } finally
                {
                    Lock.Release();
                }
                try
                {
#if DEBUG
                    Task.Delay(Time.Minute * 2, token)
#else
                    Task.Delay(Time.Minute * 15, token)
#endif
                    .Wait();
                }
                catch (Exception ex)
                {
                    Program.LogMsg("SonarrLoop", ex);
                    return;
                }
            }
            Program.LogMsg("Exited loop", LogSeverity.Debug, "SonarWebhooks");
        }