aboutsummaryrefslogtreecommitdiff
path: root/lib/models/channel.ml
diff options
context:
space:
mode:
authorAdelyn Breedlove <[email protected]>2018-12-21 16:22:49 +0000
committerAdelyn Breedlove <[email protected]>2018-12-21 16:22:49 +0000
commitc23f6b323a7231cd2caee6520dc709799508c87e (patch)
tree4c238d53d2cbd9362f04044351582dd5f5e56d79 /lib/models/channel.ml
parentMerge branch 'dev' into 'master' (diff)
parentUpdate README (diff)
downloaddisml-c23f6b323a7231cd2caee6520dc709799508c87e.tar.xz
disml-c23f6b323a7231cd2caee6520dc709799508c87e.zip
Merge branch 'dev' into 'master'
Merge work from dev See merge request Mishio595/disml!11
Diffstat (limited to 'lib/models/channel.ml')
-rw-r--r--lib/models/channel.ml71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/models/channel.ml b/lib/models/channel.ml
index 36b7d4b..15202b1 100644
--- a/lib/models/channel.ml
+++ b/lib/models/channel.ml
@@ -1,2 +1,73 @@
+exception Invalid_message
+exception No_message_found
+
+let get_id (ch:Channel_t.t) = match ch with
+| `Group g -> g.id
+| `Private p -> p.id
+| `GuildText t -> t.id
+| `GuildVoice v -> v.id
+| `Category c -> c.id
+
module Make(Http : S.Http) = struct
+ open Async
+ open Core
+
+ type t = Channel_t.t
+
+ let say ~content ch =
+ Http.create_message (get_id ch) (`Assoc [("content", `String content)])
+ >>| Result.map ~f:Message_j.t_of_string
+
+ let send_message ?embed ?content ?file ?(tts=false) ch =
+ let embed = match embed with
+ | Some e -> e
+ | None -> `Null in
+ let content = match content with
+ | Some c -> `String c
+ | None -> `Null in
+ let file = match file with
+ | Some f -> `String f
+ | None -> `Null in
+ let () = match embed, content with
+ | `Null, `Null -> raise Invalid_message
+ | _ -> () in
+ Http.create_message (get_id ch) (`Assoc [
+ ("embed", embed);
+ ("content", content);
+ ("file", file);
+ ("tts", `Bool tts);
+ ]) >>| Result.map ~f:Message_j.t_of_string
+
+ let delete ch =
+ Http.delete_channel (get_id ch) >>| Result.map ~f:ignore
+
+ let get_message ~id ch =
+ Http.get_message (get_id ch) id >>| Result.map ~f:Message_j.t_of_string
+
+ let get_messages ?(mode=`Around) ?id ?(limit=50) ch =
+ let kind = match mode with
+ | `Around -> "around", limit
+ | `Before -> "before", limit
+ | `After -> "after", limit
+ in
+ let id = match id with
+ | Some id -> id
+ | None -> raise No_message_found in
+ Http.get_messages (get_id ch) id kind >>| Result.map ~f:(fun l ->
+ Yojson.Safe.(from_string l
+ |> Util.to_list)
+ |> List.map ~f:(fun i ->
+ Yojson.Safe.to_string i
+ |> Message_j.t_of_string))
+
+ let broadcast_typing ch =
+ Http.broadcast_typing (get_id ch) >>| Result.map ~f:ignore
+
+ let get_pins ch =
+ Http.get_pinned_messages (get_id ch) >>| Result.map ~f:(fun l ->
+ Yojson.Safe.(from_string l
+ |> Util.to_list)
+ |> List.map ~f:(fun i ->
+ Yojson.Safe.to_string i
+ |> Message_j.t_of_string))
end \ No newline at end of file