diff options
| author | Mishio595 <[email protected]> | 2018-11-24 09:51:03 -0700 |
|---|---|---|
| committer | Mishio595 <[email protected]> | 2018-11-24 09:51:03 -0700 |
| commit | d342c4cf9fe907d2107cd815f9988f8ad147218b (patch) | |
| tree | febb926d6f3e1956346db2d3ee952391b193deb9 /bin/bot.ml | |
| parent | Add opam build file (diff) | |
| download | disml-d342c4cf9fe907d2107cd815f9988f8ad147218b.tar.xz disml-d342c4cf9fe907d2107cd815f9988f8ad147218b.zip | |
Major structural changes
Diffstat (limited to 'bin/bot.ml')
| -rw-r--r-- | bin/bot.ml | 60 |
1 files changed, 38 insertions, 22 deletions
@@ -2,33 +2,49 @@ open Async open Core open Disml -let hook_events client = - Client.on "MESSAGE_CREATE" client (fun msg -> - let msg_time = Time.(to_span_since_epoch @@ now ()) in - 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 resp -> - let message_id = Yojson.Basic.Util.(member "id" resp |> to_string) in - let rtt = Time.(to_span_since_epoch @@ sub (now ()) msg_time) in - Http.edit_message channel message_id @@ `Assoc [ - ("content", `String ("Pong! `" ^ (Float.to_string @@ Time.Span.to_ms rtt) ^ " ms`")); - ] - >>> fun _ -> print_endline "Message Edited!" - ); - Client.on "GUILD_CREATE" client (fun guild -> print_endline Yojson.Basic.Util.(member "name" guild |> to_string)) +let rec ev_loop read = + Pipe.read read >>= fun frame -> + match frame with + | `Eof -> return () + | `Ok (t, data) -> begin + match t with + | "MESSAGE_CREATE" -> begin + let msg = Model.to_message data in + let msg_time = Time.(to_span_since_epoch @@ now ()) in + let content = msg.content in + let channel = msg.channel in + if String.is_prefix ~prefix:"!?ping" content then begin + Http.create_message channel @@ `Assoc [ + ("content", `String "Pong!"); + ("tts", `Bool false); + ] + >>> fun resp -> + let message_id = Yojson.Basic.Util.(member "id" resp |> to_string) in + let rtt = Time.(to_span_since_epoch @@ sub (now ()) msg_time) in + Http.edit_message channel message_id @@ `Assoc [ + ("content", `String ("Pong! `" ^ (Float.to_string @@ Time.Span.to_ms rtt) ^ " ms`")); + ] + >>> fun _ -> print_endline "Message Edited!" + end; + return () + end + | "GUILD_CREATE" -> begin + let guild = Model.to_guild data in + print_endline guild.name; + return () + end + | _ -> return () + end + >>= fun _ -> ev_loop read let main () = let token = match Sys.getenv "DISCORD_TOKEN" with | Some s -> s | None -> failwith "No token" in - let client = Client.make token in - hook_events client; + let (r,w) = Pipe.create () in + let client = Client.make ~handler:w token in + ev_loop r >>> ignore; Client.start client >>> fun client -> Clock.every @@ -38,4 +54,4 @@ let main () = |> ignore) let _ = - Scheduler.go_main ~main ()
\ No newline at end of file + Scheduler.go_main ~main () |