aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/commands.ml19
-rw-r--r--lib/client.ml16
-rw-r--r--lib/client.mli35
-rw-r--r--lib/gateway/sharder.ml54
-rw-r--r--lib/gateway/sharder.mli16
5 files changed, 74 insertions, 66 deletions
diff --git a/bin/commands.ml b/bin/commands.ml
index ab3a76e..131925a 100644
--- a/bin/commands.ml
+++ b/bin/commands.ml
@@ -54,9 +54,9 @@ let embed message _args =
(* Set the status of all shards to a given string. *)
let status message args =
- let status = List.fold ~init:"" ~f:(fun a v -> a ^ " " ^ v) args in
+ let name = List.fold ~init:"" ~f:(fun a v -> a ^ " " ^ v) args in
Ivar.read client >>> fun client ->
- Client.set_status ~status:(`String status) client
+ Client.set_status ~name client
>>> fun _ ->
Message.reply message "Updated status" >>> ignore
@@ -87,14 +87,19 @@ let cache message _args =
let pre = U.length cache.presences in
let user = Option.(value ~default:"None" (cache.user >>| User.tag)) in
let embed = Embed.(default
- |> description (Printf.sprintf "Guilds: %d\nText Channels: %d\nVoice Channels: %d\nCategories: %d\nGroups: %d\nPrivate Channels: %d\nUsers: %d\nPresences: %d\nCurrent User: %s" gc tc vc cs gr pr uc pre user)) in
+ |> description (Printf.sprintf
+ "Guilds: %d\nText Channels: %d\n\
+ Voice Channels: %d\nCategories: %d\n\
+ Groups: %d\nPrivate Channels: %d\n\
+ Users: %d\nPresences: %d\n\
+ Current User: %s"
+ gc tc vc cs gr pr uc pre user)) in
Message.reply_with ~embed message >>> ignore
-(* Issue a shutdown to all shards. It is expected that they will restart if `?restart` is not false. *)
+(* Issue a shutdown to all shards, then exits the process. *)
let shutdown _message _args =
- let module Sharder = Gateway.Sharder in
- Ivar.read client >>> fun client ->
- Sharder.shutdown_all client.sharder >>> ignore
+ Ivar.read client >>= Client.shutdown_all ~restart:false >>> fun _ ->
+ exit 0
(* Request guild members to be sent over the gateway for the guild the command is run in. This will cause multiple GUILD_MEMBERS_CHUNK events. *)
let request_members (message:Message.t) _args =
diff --git a/lib/client.ml b/lib/client.ml
index d1778e7..9269807 100644
--- a/lib/client.ml
+++ b/lib/client.ml
@@ -1,8 +1,8 @@
open Async
include Dispatch
-type t = {
- sharder: Sharder.t;
+type t =
+{ sharder: Sharder.t
}
let start ?count ?compress ?(large=250) token =
@@ -11,12 +11,12 @@ let start ?count ?compress ?(large=250) token =
>>| fun sharder ->
{ sharder; }
-let set_status ~status client =
- Sharder.set_status ~status client.sharder
-
-let set_status_with ~f client =
- Sharder.set_status_with ~f client.sharder
+let set_status ?status ?kind ?name ?since client =
+ Sharder.set_status ?status ?kind ?name ?since client.sharder
let request_guild_members ~guild ?query ?limit client =
let `Guild_id guild = guild in
- Sharder.request_guild_members ~guild ?query ?limit client.sharder \ No newline at end of file
+ Sharder.request_guild_members ~guild ?query ?limit client.sharder
+
+let shutdown_all ?restart client =
+ Sharder.shutdown_all ?restart client.sharder \ No newline at end of file
diff --git a/lib/client.mli b/lib/client.mli
index c5b5501..8a0002b 100644
--- a/lib/client.mli
+++ b/lib/client.mli
@@ -3,8 +3,8 @@ open Async
include module type of Dispatch
(** Type of the Client, it isn't recommended to access the fields directly. *)
-type t = {
- sharder: Sharder.t;
+type t =
+{ sharder: Sharder.t
}
(** Start the Client. This begins shard connections to Discord and event handlers should be registered prior to calling this.
@@ -26,13 +26,32 @@ type t = {
@param string The token used for authentication.
@return A deferred client object.
*)
-val start : ?count:int -> ?compress:bool -> ?large:int -> string -> t Deferred.t
+val start :
+ ?count:int ->
+ ?compress:bool ->
+ ?large:int ->
+ string ->
+ t Deferred.t
(** Same as {!Sharder.set_status} where [client.sharder] is passed. *)
-val set_status : status:Yojson.Safe.t -> t -> Sharder.Shard.shard list Deferred.t
-
-(** Same as {!Sharder.set_status_with} where [client.sharder] is passed. *)
-val set_status_with : f:(Sharder.Shard.shard -> Yojson.Safe.t) -> t -> Sharder.Shard.shard list Deferred.t
+val set_status :
+ ?status:string ->
+ ?kind:int ->
+ ?name:string ->
+ ?since:int ->
+ t ->
+ Sharder.Shard.shard list Deferred.t
(** Same as {!Sharder.request_guild_members} where [client.sharder] is passed. *)
-val request_guild_members : guild:Guild_id.t -> ?query:string -> ?limit:int -> t -> Sharder.Shard.shard list Deferred.t \ No newline at end of file
+val request_guild_members :
+ guild:Guild_id.t ->
+ ?query:string ->
+ ?limit:int ->
+ t ->
+ Sharder.Shard.shard list Deferred.t
+
+(** Same as {!Sharder.shutdown_all} where [client.sharder] is passed. *)
+val shutdown_all :
+ ?restart:bool ->
+ t ->
+ unit list Deferred.t \ No newline at end of file
diff --git a/lib/gateway/sharder.ml b/lib/gateway/sharder.ml
index 9fcb10d..4317f80 100644
--- a/lib/gateway/sharder.ml
+++ b/lib/gateway/sharder.ml
@@ -107,40 +107,29 @@ module Shard = struct
; session = session
}
- let set_status ~(status:Yojson.Safe.t) shard =
- let payload = match status with
- | `Assoc ["name", `String name; "type", `Int t]
- | `Assoc ["type", `Int t; "name", `String name] ->
- `Assoc [
- "status", `String "online";
- "afk", `Bool false;
- "since", `Null;
- "game", `Assoc [
- "name", `String name;
- "type", `Int t;
- ]
- ]
- | `String name ->
- `Assoc [
- "status", `String "online";
- "afk", `Bool false;
- "since", `Null;
- "game", `Assoc [
- "name", `String name;
- "type", `Int 0
- ]
+ let set_status ?(status="online") ?(kind=1) ?name ?since shard =
+ let since = Option.(since >>| (fun v -> `Int v) |> value ~default:`Null) in
+ let game = match name with
+ | Some name -> `Assoc [ "name", `String name; "type", `Int kind ]
+ | None -> `Null
+ in
+ let payload = `Assoc
+ [ "status", `String status
+ ; "afk", `Bool false
+ ; "since", since
+ ; "game", game
]
- | _ -> raise Invalid_Payload
in
Ivar.read shard.ready >>= fun _ ->
push_frame ~payload ~ev:STATUS_UPDATE shard
let request_guild_members ?(query="") ?(limit=0) ~guild shard =
- let payload = `Assoc [
- "guild_id", `String (Int.to_string guild);
- "query", `String query;
- "limit", `Int limit;
- ] in
+ let payload = `Assoc
+ [ "guild_id", `String (Int.to_string guild)
+ ; "query", `String query
+ ; "limit", `Int limit
+ ]
+ in
Ivar.read shard.ready >>= fun _ ->
push_frame ~payload ~ev:REQUEST_GUILD_MEMBERS shard
@@ -372,14 +361,9 @@ let start ?count ?compress ?large_threshold () =
>>| fun shards ->
{ shards }
-let set_status ~status sharder =
- Deferred.all @@ List.map ~f:(fun t ->
- Shard.set_status ~status t.state
- ) sharder.shards
-
-let set_status_with ~f sharder =
+let set_status ?status ?kind ?name ?since sharder =
Deferred.all @@ List.map ~f:(fun t ->
- Shard.set_status ~status:(f t.state) t.state
+ Shard.set_status ?status ?kind ?name ?since t.state
) sharder.shards
let request_guild_members ?query ?limit ~guild sharder =
diff --git a/lib/gateway/sharder.mli b/lib/gateway/sharder.mli
index a5f18e6..1851265 100644
--- a/lib/gateway/sharder.mli
+++ b/lib/gateway/sharder.mli
@@ -48,7 +48,10 @@ module Shard : sig
(** Set the status of the shard. *)
val set_status :
- status:Yojson.Safe.t ->
+ ?status:string ->
+ ?kind:int ->
+ ?name:string ->
+ ?since:int ->
shard ->
shard Deferred.t
@@ -78,13 +81,10 @@ end
(** Calls {!Shard.set_status} for each shard registered with the sharder. *)
val set_status :
- status:Yojson.Safe.t ->
- t ->
- Shard.shard list Deferred.t
-
-(** Like {!set_status} but takes a function with a {{!Shard.shard}shard} as its parameter and {{!Yojson.Safe.t}json} for its return. *)
-val set_status_with :
- f:(Shard.shard -> Yojson.Safe.t) ->
+ ?status:string ->
+ ?kind:int ->
+ ?name:string ->
+ ?since:int ->
t ->
Shard.shard list Deferred.t