From d92fe94da312c0a69dbe9bd7c2e525c594f20e40 Mon Sep 17 00:00:00 2001 From: Mishio595 Date: Sat, 17 Nov 2018 19:49:45 -0700 Subject: Rewrite from Lwt to Async --- bin/bot.ml | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'bin') diff --git a/bin/bot.ml b/bin/bot.ml index bf9f36e..34f24ea 100644 --- a/bin/bot.ml +++ b/bin/bot.ml @@ -1,20 +1,13 @@ -open Lwt.Infix +open Async open Disml -let main sharder = - Lwt_engine.on_timer 60.0 true begin - fun _ev -> Sharder.set_status_with sharder @@ begin - fun shard -> - `String ("Current seq: " ^ string_of_int shard.seq) - end - >|= (fun _ -> print_endline "Status set!") - |> ignore; - end +let main () = + let token = match Sys.getenv "DISCORD_TOKEN" with + | Some s -> s + | None -> failwith "No token" + in + Sharder.start token + |> ignore let _ = - Sharder.start @@ Sys.getenv "DISCORD_TOKEN" - >>= (fun sharder -> - main sharder - |> ignore; - sharder.promise) - |> Lwt_main.run \ No newline at end of file + Scheduler.go_main ~main:(main) () \ No newline at end of file -- cgit v1.2.3 From a7340eba42bcef93a4e6f9ece7c86043f9a01747 Mon Sep 17 00:00:00 2001 From: Mishio595 Date: Sun, 18 Nov 2018 12:04:19 -0700 Subject: Event dispatch and client abstraction --- bin/bot.ml | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/bot.ml b/bin/bot.ml index 34f24ea..83e7063 100644 --- a/bin/bot.ml +++ b/bin/bot.ml @@ -1,4 +1,5 @@ open Async +open Core open Disml let main () = @@ -6,8 +7,24 @@ let main () = | Some s -> s | None -> failwith "No token" in - Sharder.start token - |> ignore + let client = Client.make token in + Client.on "MESSAGE_CREATE" client (fun msg -> + let content = Yojson.Basic.Util.(member "content" msg |> to_string) in + let channel = Yojson.Basic.Util.(member "channel_id" msg |> to_string) in + if String.is_prefix ~prefix:"!?ping" content then + Http.create_message channel @@ `Assoc [ + ("content", `String "Pong!"); + ("tts", `Bool false); + ] + >>> fun _ -> print_endline "Message sent!"; + ); + Client.start client + >>> fun client -> + Clock.every + (Time.Span.create ~sec:60 ()) + (fun () -> + Client.set_status_with client (fun shard -> `String ("Current seq: " ^ (Int.to_string shard.seq))) + |> ignore) let _ = - Scheduler.go_main ~main:(main) () \ No newline at end of file + Scheduler.go_main ~main () \ No newline at end of file -- cgit v1.2.3