aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdelyn Breedlove <[email protected]>2019-01-19 15:58:31 -0700
committerAdelyn Breedlove <[email protected]>2019-01-19 15:58:31 -0700
commitb678d7312518bb0ff74d7db364bcbbcded5c1609 (patch)
tree39094fc179c33fc67269ffc3fea218384c558cfe /lib
parentupdate docs export (diff)
downloaddisml-b678d7312518bb0ff74d7db364bcbbcded5c1609.tar.xz
disml-b678d7312518bb0ff74d7db364bcbbcded5c1609.zip
more updates
Diffstat (limited to 'lib')
-rw-r--r--lib/client_options.mli1
-rw-r--r--lib/dispatch.mli19
-rw-r--r--lib/event.ml72
-rw-r--r--lib/models/channel.ml2
-rw-r--r--lib/models/channel.mli24
-rw-r--r--lib/models/embed.ml18
-rw-r--r--lib/models/message.ml20
-rw-r--r--lib/models/message.mli7
8 files changed, 124 insertions, 39 deletions
diff --git a/lib/client_options.mli b/lib/client_options.mli
index ef94691..ee46aae 100644
--- a/lib/client_options.mli
+++ b/lib/client_options.mli
@@ -1 +1,2 @@
+(** Token that is set when using {!Client.start} *)
val token : string ref \ No newline at end of file
diff --git a/lib/dispatch.mli b/lib/dispatch.mli
index 51d94b4..0442c19 100644
--- a/lib/dispatch.mli
+++ b/lib/dispatch.mli
@@ -1,3 +1,22 @@
+(** Used to store dispatch callbacks. Each event can only have one callback registered at a time.
+ These should be accessed through their re-export in {!Client}.
+ {3 Examples}
+ [Client.ready := (fun _ -> print_endline "Shard is Ready!")]
+
+ [Client.guild_create := (fun guild -> print_endline guild.name)]
+
+ {[
+ open Core
+ open Disml
+
+ let check_command (msg : Message.t) =
+ if String.is_prefix ~prefix:"!ping" msg.content then
+ Message.reply msg "Pong!" >>> ignore
+
+ Client.message_create := check_command
+ ]}
+*)
+
(** Dispatched when connecting to the gateway, most users will have no use for this. *)
val hello : (Yojson.Safe.json -> unit) ref
diff --git a/lib/event.ml b/lib/event.ml
index 614c485..7f843d7 100644
--- a/lib/event.ml
+++ b/lib/event.ml
@@ -78,41 +78,41 @@ let event_of_yojson ~contents t = match t with
| s -> raise @@ Invalid_event s
let dispatch ev = match ev with
-| HELLO d -> !Dispatch.hello d
-| READY d -> !Dispatch.ready d
-| RESUMED d -> !Dispatch.resumed d
-| INVALID_SESSION d -> !Dispatch.invalid_session d
-| CHANNEL_CREATE d -> !Dispatch.channel_create d
-| CHANNEL_UPDATE d -> !Dispatch.channel_update d
-| CHANNEL_DELETE d -> !Dispatch.channel_delete d
-| CHANNEL_PINS_UPDATE d -> !Dispatch.channel_pins_update d
-| GUILD_CREATE d -> !Dispatch.guild_create d
-| GUILD_UPDATE d -> !Dispatch.guild_update d
-| GUILD_DELETE d -> !Dispatch.guild_delete d
-| GUILD_BAN_ADD d -> !Dispatch.member_ban d
-| GUILD_BAN_REMOVE d -> !Dispatch.member_unban d
-| GUILD_EMOJIS_UPDATE d -> !Dispatch.guild_emojis_update d
-| GUILD_INTEGRATIONS_UPDATE d -> !Dispatch.integrations_update d
-| GUILD_MEMBER_ADD d -> !Dispatch.member_join d
-| GUILD_MEMBER_REMOVE d -> !Dispatch.member_leave d
-| GUILD_MEMBER_UPDATE d -> !Dispatch.member_update d
-| GUILD_MEMBERS_CHUNK d -> !Dispatch.members_chunk d
-| GUILD_ROLE_CREATE d -> !Dispatch.role_create d
-| GUILD_ROLE_UPDATE d -> !Dispatch.role_update d
-| GUILD_ROLE_DELETE d -> !Dispatch.role_delete d
-| MESSAGE_CREATE d -> !Dispatch.message_create d
-| MESSAGE_UPDATE d -> !Dispatch.message_update d
-| MESSAGE_DELETE (d,e) -> !Dispatch.message_delete d e
-| MESSAGE_BULK_DELETE d -> !Dispatch.message_bulk_delete d
-| MESSAGE_REACTION_ADD d -> !Dispatch.reaction_add d
-| MESSAGE_REACTION_REMOVE d -> !Dispatch.reaction_remove d
-| MESSAGE_REACTION_REMOVE_ALL d -> !Dispatch.reaction_bulk_remove d
-| PRESENCE_UPDATE d -> !Dispatch.presence_update d
-| TYPING_START d -> !Dispatch.typing_start d
-| USER_UPDATE d -> !Dispatch.user_update d
-| VOICE_STATE_UPDATE d -> !Dispatch.voice_state_update d
-| VOICE_SERVER_UPDATE d -> !Dispatch.voice_server_update d
-| WEBHOOKS_UPDATE d -> !Dispatch.webhooks_update d
+ | HELLO d -> !Dispatch.hello d
+ | READY d -> !Dispatch.ready d
+ | RESUMED d -> !Dispatch.resumed d
+ | INVALID_SESSION d -> !Dispatch.invalid_session d
+ | CHANNEL_CREATE d -> !Dispatch.channel_create d
+ | CHANNEL_UPDATE d -> !Dispatch.channel_update d
+ | CHANNEL_DELETE d -> !Dispatch.channel_delete d
+ | CHANNEL_PINS_UPDATE d -> !Dispatch.channel_pins_update d
+ | GUILD_CREATE d -> !Dispatch.guild_create d
+ | GUILD_UPDATE d -> !Dispatch.guild_update d
+ | GUILD_DELETE d -> !Dispatch.guild_delete d
+ | GUILD_BAN_ADD d -> !Dispatch.member_ban d
+ | GUILD_BAN_REMOVE d -> !Dispatch.member_unban d
+ | GUILD_EMOJIS_UPDATE d -> !Dispatch.guild_emojis_update d
+ | GUILD_INTEGRATIONS_UPDATE d -> !Dispatch.integrations_update d
+ | GUILD_MEMBER_ADD d -> !Dispatch.member_join d
+ | GUILD_MEMBER_REMOVE d -> !Dispatch.member_leave d
+ | GUILD_MEMBER_UPDATE d -> !Dispatch.member_update d
+ | GUILD_MEMBERS_CHUNK d -> !Dispatch.members_chunk d
+ | GUILD_ROLE_CREATE d -> !Dispatch.role_create d
+ | GUILD_ROLE_UPDATE d -> !Dispatch.role_update d
+ | GUILD_ROLE_DELETE d -> !Dispatch.role_delete d
+ | MESSAGE_CREATE d -> !Dispatch.message_create d
+ | MESSAGE_UPDATE d -> !Dispatch.message_update d
+ | MESSAGE_DELETE (d,e) -> !Dispatch.message_delete d e
+ | MESSAGE_BULK_DELETE d -> !Dispatch.message_bulk_delete d
+ | MESSAGE_REACTION_ADD d -> !Dispatch.reaction_add d
+ | MESSAGE_REACTION_REMOVE d -> !Dispatch.reaction_remove d
+ | MESSAGE_REACTION_REMOVE_ALL d -> !Dispatch.reaction_bulk_remove d
+ | PRESENCE_UPDATE d -> !Dispatch.presence_update d
+ | TYPING_START d -> !Dispatch.typing_start d
+ | USER_UPDATE d -> !Dispatch.user_update d
+ | VOICE_STATE_UPDATE d -> !Dispatch.voice_state_update d
+ | VOICE_SERVER_UPDATE d -> !Dispatch.voice_server_update d
+ | WEBHOOKS_UPDATE d -> !Dispatch.webhooks_update d
let handle_event ~ev contents =
(* Printf.printf "Dispatching %s\n%!" ev; *)
@@ -120,4 +120,4 @@ let handle_event ~ev contents =
try
event_of_yojson ~contents ev
|> dispatch
- with Invalid_event ev -> Printf.printf "Unknown event: %s%!" ev \ No newline at end of file
+ with Invalid_event ev -> Printf.printf "Unknown event: %s\n%!" ev \ No newline at end of file
diff --git a/lib/models/channel.ml b/lib/models/channel.ml
index c3524ef..972f888 100644
--- a/lib/models/channel.ml
+++ b/lib/models/channel.ml
@@ -11,7 +11,7 @@ let say ~content ch =
let send_message ?embed ?content ?file ?(tts=false) ch =
let embed = match embed with
- | Some e -> e
+ | Some e -> Embed.to_yojson e
| None -> `Null in
let content = match content with
| Some c -> `String c
diff --git a/lib/models/channel.mli b/lib/models/channel.mli
index 6ca814e..001bb05 100644
--- a/lib/models/channel.mli
+++ b/lib/models/channel.mli
@@ -1,9 +1,31 @@
open Async
include module type of Channel_t
+exception Invalid_message
+exception No_message_found
+
+(** Simple version of send_message that only takes [~content] *)
val say : content:string -> t -> Message_t.t Deferred.Or_error.t
+
+(** Advanced message sending.
+
+ Raises {!Channel.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 with title = Some "Hello World!" } in
+ Channel.send_message ~embed msg.channel >>> ignore
+
+ Client.message_create := check_command
+ ]}
+*)
val send_message :
- ?embed:Yojson.Safe.json ->
+ ?embed:Embed.t ->
?content:string ->
?file:string ->
?tts:bool ->
diff --git a/lib/models/embed.ml b/lib/models/embed.ml
index 4faaba7..7dbbeaa 100644
--- a/lib/models/embed.ml
+++ b/lib/models/embed.ml
@@ -51,4 +51,20 @@ type t = {
provider: provider option [@default None];
author: author option [@default None];
fields: field list [@default []];
-} [@@deriving sexp, yojson { strict = false }] \ No newline at end of file
+} [@@deriving sexp, yojson { strict = false }]
+
+let default = {
+ title = None;
+ kind = None;
+ description = None;
+ url = None;
+ timestamp = None;
+ colour = None;
+ footer = None;
+ image = None;
+ thumbnail = None;
+ video = None;
+ provider = None;
+ author = None;
+ fields = [];
+} \ No newline at end of file
diff --git a/lib/models/message.ml b/lib/models/message.ml
index 5bc06b7..f772c48 100644
--- a/lib/models/message.ml
+++ b/lib/models/message.ml
@@ -39,6 +39,26 @@ let reply msg cont =
Http.create_message msg.channel_id rep
>>| Result.map ~f:Message_t.of_yojson_exn
+let reply_with ?embed ?content ?file ?(tts=false) msg =
+ 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 Channel.Invalid_message
+ | _ -> () in
+ Http.create_message (msg.channel_id) (`Assoc [
+ ("embed", embed);
+ ("content", content);
+ ("file", file);
+ ("tts", `Bool tts);
+ ]) >>| Result.map ~f:Message_t.of_yojson_exn
+
let set_content msg cont =
to_yojson { msg with content = cont; }
|> Http.edit_message msg.channel_id msg.id
diff --git a/lib/models/message.mli b/lib/models/message.mli
index 40c87d4..09e62a4 100644
--- a/lib/models/message.mli
+++ b/lib/models/message.mli
@@ -9,5 +9,12 @@ val delete : t -> unit Deferred.Or_error.t
val pin : t -> unit Deferred.Or_error.t
val unpin : t -> unit Deferred.Or_error.t
val reply : t -> string -> t Deferred.Or_error.t
+val reply_with :
+ ?embed:Embed.t ->
+ ?content:string ->
+ ?file:string ->
+ ?tts:bool ->
+ t ->
+ Message_t.t Deferred.Or_error.t
val set_content : t -> string -> t Deferred.Or_error.t
val set_embed : t -> Embed.t -> t Deferred.Or_error.t \ No newline at end of file