-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
82 lines (61 loc) · 1.96 KB
/
Program.fs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
open System
open System.Reactive.Subjects
open System.Threading
open FSharp.Control.Reactive
open Mirai.Net.Sessions
open Mirai.Net.Data.Messages.Receivers
open Serilog
open Serilog.Events
open FScribe
open FScribe.Env
open FScribe.Util
Log.Logger <-
LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Debug)
.WriteTo.Console()
.CreateLogger()
let asmName =
System
.Reflection
.Assembly
.GetExecutingAssembly()
.GetName()
logInfo "%s %s" asmName.Name (asmName.Version |> string)
let initialStatus =
Storage.loadStatus "records.json"
let bot = new MiraiBot()
bot.QQ <- GetEnv "MIRAI_API_QQ"
bot.Address <- GetEnv "MIRAI_API_ADDRESS"
bot.VerifyKey <- GetEnv "MIRAI_API_KEY"
logInfo "Connecting to %s." (bot.Address |> string)
bot.LaunchAsync().Wait()
logInfo "Login to %s." bot.QQ
let latestStatus =
new BehaviorSubject<Storage.ScribeStatus>(initialStatus)
bot.MessageReceived
|> Observable.choose tryParse<GroupMessageReceiver>
|> Observable.scanInit initialStatus (Scribe.handle bot)
|> Observable.subscribeObserver latestStatus
|> ignore
logInfo "Scribe is now observing."
let saveCurrentStatus () =
if latestStatus.Value = initialStatus then
logWarning "No changes to save!"
Storage.saveStatus "records.json" latestStatus.Value
let exitEvent = new ManualResetEvent(false)
AppDomain.CurrentDomain.UnhandledException.AddHandler (fun _ e ->
let ex = e.ExceptionObject :?> Exception
Log.Error(ex, "Unhandled exception.")
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex)))
AppDomain.CurrentDomain.ProcessExit.AddHandler (fun _ _ ->
logInfo "ProcessExiting..."
saveCurrentStatus ()
exitEvent.Set() |> ignore)
Console.CancelKeyPress.AddHandler (fun _ args ->
logInfo "Received Ctrl+C. Scribe is now exiting."
args.Cancel <- true
saveCurrentStatus ()
exitEvent.Set() |> ignore)
exitEvent.WaitOne() |> ignore
Log.CloseAndFlush()
exit 0