Exemplo n.º 1
0
        /// <summary>
        /// Returns true when all channels are joined
        /// </summary>
        /// <returns></returns>
        public async Task <bool> JoinAllChannels()
        {
            try
            {
                if (client == null || !client.IsConnected)
                {
                    client = new TwitchClient();
                    client.Initialize(_credentials);
                    client.OnJoinedChannel   += onJoinedChannel;
                    client.OnMessageReceived += onMessageReceived;
                    client.Connect();
                }
                else
                {
                    Console.WriteLine($"Refreshing IRC Channels");
                }

                var streams        = (await _channelHandler.GetStreams()).ToList();
                var joinedChannels = client.JoinedChannels.ToList();
                foreach (var stream in streams)
                {
                    //join if not in already
                    if (!joinedChannels.Select(j => j.Channel).Contains(stream.Value.Channel.Name))
                    {
                        Console.WriteLine("Joined " + stream.Value.Channel.Name);
                        client.JoinChannel(stream.Value.Channel.Name);
                    }
                }


                //Leave channels that we didnt get back (no longer live)
                foreach (var joinedChannel in joinedChannels)
                {
                    if (!streams.Select(s => s.Value.Channel.Name).Contains(joinedChannel.Channel))
                    {
                        Console.WriteLine($"Leaving {joinedChannel.Channel}");
                        client.LeaveChannel(joinedChannel);
                    }
                }

                Console.WriteLine($"Currently in {streams.Count} channels.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }

            return(true);
        }