From 7c9b809078b5cd53e3d54c0004c683da2ec679af Mon Sep 17 00:00:00 2001 From: Adelyn Breedlove Date: Mon, 11 Feb 2019 17:23:59 +0000 Subject: Add a cache --- lib/models/channel/channel.ml | 54 ++++++++++++++++++++++++++++++++++++- lib/models/channel/channel.mli | 47 ++++++++++++++++++++++++++++++-- lib/models/channel/message/embed.ml | 12 ++++----- 3 files changed, 104 insertions(+), 9 deletions(-) (limited to 'lib/models/channel') diff --git a/lib/models/channel/channel.ml b/lib/models/channel/channel.ml index 6ccc66d..47cf500 100644 --- a/lib/models/channel/channel.ml +++ b/lib/models/channel/channel.ml @@ -1,3 +1,55 @@ +open Core include Channel_t -include Impl.Channel(Channel_t) \ No newline at end of file +exception Invalid_message +exception No_message_found + +let send_message ?embed ?content ?file ?(tts=false) ch = + let embed = match embed with + | Some e -> Embed.to_yojson 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); + ]) + +let say content ch = + send_message ~content ch + +let delete ch = + Http.delete_channel (get_id ch) + +let get_message ~id ch = + Http.get_message (get_id ch) id + +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 + +let broadcast_typing ch = + Http.broadcast_typing (get_id ch) + +let get_pins ch = + Http.get_pinned_messages (get_id ch) + +let bulk_delete msgs ch = + let msgs = `List (List.map ~f:(fun id -> `Int id) msgs) in + Http.bulk_delete (get_id ch) msgs \ No newline at end of file diff --git a/lib/models/channel/channel.mli b/lib/models/channel/channel.mli index 3eece7d..9e981ae 100644 --- a/lib/models/channel/channel.mli +++ b/lib/models/channel/channel.mli @@ -1,3 +1,46 @@ +open Async include module type of Channel_t -include S.ChannelImpl with - type t := Channel_t.t \ No newline at end of file + +exception Invalid_message +exception No_message_found + +(** Advanced message sending. + + Raises {!Invalid_message} if one of content or embed is not set. + + {3 Examples} + {[ + open Core + open Disml + + let check_command (msg : Message.t) = + if String.is_prefix ~prefix:"!hello" msg.content then + let embed = Embed.(default |> title "Hello World!") in + Channel_id.send_message ~embed msg.channel_id >>> ignore + + Client.message_create := check_command + ]} +*) +val send_message : + ?embed:Embed.t -> + ?content:string -> + ?file:string -> + ?tts:bool -> + t -> + Message_t.t Deferred.Or_error.t + +(** [say str ch] is equivalent to [send_message ~content:str ch]. *) +val say : string -> t -> Message_t.t Deferred.Or_error.t + +val delete : t -> Channel_t.t Deferred.Or_error.t +val get_message : id:Snowflake.t -> t -> Message_t.t Deferred.Or_error.t +val get_messages : + ?mode:[ `Before | `After | `Around ] -> + ?id:Snowflake.t -> + ?limit:int -> + t -> + Message_t.t list Deferred.Or_error.t +val broadcast_typing : t -> unit Deferred.Or_error.t +val get_pins : t -> Message_t.t list Deferred.Or_error.t +val bulk_delete : Snowflake.t list -> t -> unit Deferred.Or_error.t +(* TODO more things related to guild channels *) \ No newline at end of file diff --git a/lib/models/channel/message/embed.ml b/lib/models/channel/message/embed.ml index f66aa3f..0dd7343 100644 --- a/lib/models/channel/message/embed.ml +++ b/lib/models/channel/message/embed.ml @@ -4,38 +4,38 @@ type footer = { text: string; icon_url: string option [@default None]; proxy_icon_url: string option [@default None]; -} [@@deriving sexp, yojson { exn = true }] +} [@@deriving sexp, yojson { strict = false; exn = true }] type image = { url: string option [@default None]; proxy_url: string option [@default None]; height: int option [@default None]; width: int option [@default None]; -} [@@deriving sexp, yojson { exn = true }] +} [@@deriving sexp, yojson { strict = false; exn = true }] type video = { url: string option [@default None]; height: int option [@default None]; width: int option [@default None]; -} [@@deriving sexp, yojson { exn = true }] +} [@@deriving sexp, yojson { strict = false; exn = true }] type provider = { name: string option [@default None]; url: string option [@default None]; -} [@@deriving sexp, yojson { exn = true }] +} [@@deriving sexp, yojson { strict = false; exn = true }] type author = { name: string option [@default None]; url: string option [@default None]; icon_url: string option [@default None]; proxy_icon_url: string option [@default None]; -} [@@deriving sexp, yojson { exn = true }] +} [@@deriving sexp, yojson { strict = false; exn = true }] type field = { name: string; value: string; inline: bool [@default false]; -} [@@deriving sexp, yojson { exn = true }] +} [@@deriving sexp, yojson { strict = false; exn = true }] type t = { title: string option [@default None]; -- cgit v1.2.3