Channels.fs 934 Bytes
Newer Older
Øystein Kristoffer Tveit's avatar
Øystein Kristoffer Tveit committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace Bot

module Channels =

  open FSharp.Data
  open System.IO

  let private filepath = __SOURCE_DIRECTORY__ + "/../../data/channels.dat"

  let mutable channels = 
    File.ReadLines(filepath)
      |> Seq.map (fun line -> line.ToString().AsInteger64())
      |> Seq.map (uint64)
      |> set

  let private updateChannels newChannels =
    newChannels
    |> Seq.map (fun i -> i.ToString())
    |> Seq.toList
    |> fun lines -> File.WriteAllLines(filepath, lines)
    channels <- newChannels

  let private removeChannel (channelId:uint64) =
    channels.Remove(channelId)
    |> updateChannels

  let private addChannel (channelId:uint64) =
    channels.Add(channelId)
    |> updateChannels
  
  let toggleChannel (channelId:uint64) =
    match channelId with
    | channelId when channels.Contains(channelId) -> 
      removeChannel channelId
      false

    | channelId -> 
      addChannel channelId
      true