diff options
| author | Adelyn Breelove <[email protected]> | 2018-12-21 09:15:32 -0700 |
|---|---|---|
| committer | Adelyn Breelove <[email protected]> | 2018-12-21 09:15:32 -0700 |
| commit | 4abf2d536e04597650125a15a92a8835be607773 (patch) | |
| tree | 7c170b3b44592fdf882a8ed37d414d619ccfc7cb /lib | |
| parent | Working with Discord's channel bullshit (diff) | |
| download | disml-4abf2d536e04597650125a15a92a8835be607773.tar.xz disml-4abf2d536e04597650125a15a92a8835be607773.zip | |
Implement most channel methods
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/http.ml | 4 | ||||
| -rw-r--r-- | lib/models/channel.ml | 65 | ||||
| -rw-r--r-- | lib/s.ml | 21 |
3 files changed, 85 insertions, 5 deletions
diff --git a/lib/http.ml b/lib/http.ml index e2e5471..f04a49f 100644 --- a/lib/http.ml +++ b/lib/http.ml @@ -82,8 +82,8 @@ module Make(T : S.Token) = struct let delete_channel channel_id = Base.request `DELETE (Endpoints.channel channel_id) - let get_messages channel_id = - Base.request `GET (Endpoints.channel_messages channel_id) + let get_messages channel_id limit (kind, id) = + Base.request `GET (Printf.sprintf "%s?%s=%d&limit=%d" (Endpoints.channel_messages channel_id) kind id limit) let get_message channel_id message_id = Base.request `GET (Endpoints.channel_message channel_id message_id) diff --git a/lib/models/channel.ml b/lib/models/channel.ml index d52be05..15202b1 100644 --- a/lib/models/channel.ml +++ b/lib/models/channel.ml @@ -1,4 +1,7 @@ -let id (ch:Channel_t.t) = match ch with +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 @@ -6,7 +9,65 @@ let id (ch:Channel_t.t) = match ch with | `Category c -> c.id module Make(Http : S.Http) = struct - (* open Channel_t *) + 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 @@ -18,6 +18,25 @@ end module type Channel = sig type t = Channel_t.t + val say : content:string -> Channel_t.t -> Message_t.t Deferred.Or_error.t + val send_message : + ?embed:Yojson.Safe.json -> + ?content:string -> + ?file:string -> + ?tts:bool -> + Channel_t.t -> + Message_t.t Deferred.Or_error.t + val delete : Channel_t.t -> unit Deferred.Or_error.t + val get_message : id:Snowflake_t.t -> Channel_t.t -> Message_t.t Deferred.Or_error.t + val get_messages : + ?mode:[ `Before | `After | `Around ] -> + ?id:Snowflake_t.t -> + ?limit:int -> + Channel_t.t -> + Message_t.t list Deferred.Or_error.t + val broadcast_typing : Channel_t.t -> unit Deferred.Or_error.t + val get_pins : Channel_t.t -> Message_t.t list Deferred.Or_error.t + (* TODO more things related to guild channels *) end module type Embed = sig @@ -164,7 +183,7 @@ module type Http = sig val modify_channel : int -> Yojson.Safe.json -> string Core.Or_error.t Conduit_async.io val delete_channel : int -> string Core.Or_error.t Conduit_async.io - val get_messages : int -> string Core.Or_error.t Conduit_async.io + val get_messages : int -> int -> string * int -> string Core.Or_error.t Conduit_async.io val get_message : int -> int -> string Core.Or_error.t Conduit_async.io val create_message : int -> Yojson.Safe.json -> string Core.Or_error.t Conduit_async.io |