diff options
| author | Adelyn Breelove <[email protected]> | 2018-12-20 16:03:27 -0700 |
|---|---|---|
| committer | Adelyn Breelove <[email protected]> | 2018-12-20 16:03:27 -0700 |
| commit | 9e3c97eb4f5d8a808844cb2e448371ce1cc150e1 (patch) | |
| tree | 2337d229ea595c25dbff88e1b100b91690b21ad4 | |
| parent | Fix member abstraction and helper methods (diff) | |
| download | disml-9e3c97eb4f5d8a808844cb2e448371ce1cc150e1.tar.xz disml-9e3c97eb4f5d8a808844cb2e448371ce1cc150e1.zip | |
Working with Discord's channel bullshit
| -rw-r--r-- | lib/channel.atd | 64 | ||||
| -rw-r--r-- | lib/event.ml | 16 | ||||
| -rw-r--r-- | lib/guild.atd | 8 | ||||
| -rw-r--r-- | lib/models/channel.ml | 9 | ||||
| -rw-r--r-- | lib/models/guild.ml | 8 | ||||
| -rw-r--r-- | lib/s.ml | 2 |
6 files changed, 93 insertions, 14 deletions
diff --git a/lib/channel.atd b/lib/channel.atd index 6ab58cf..4094ff1 100644 --- a/lib/channel.atd +++ b/lib/channel.atd @@ -1,7 +1,64 @@ type snowflake <ocaml from="Snowflake" t="t"> = abstract type user <ocaml from="User" t="t"> = abstract -type t = { +type group = { + id: snowflake; + ?last_message_id: snowflake option; + ?last_pin_timestamp: string option; + ?icon: string option; + ?name: string option; + owner_id: snowflake; + recipients: user list; +} + +type dm = { + id: snowflake; + ?last_message_id: snowflake option; + ?last_pin_timestamp: string option; +} + +type guild_text = { + id: snowflake; + ?last_message_id: snowflake option; + ?last_pin_timestamp: string option; + ?category_id <json name="parent_id">: snowflake option; + guild_id: snowflake; + name: string; + position: int; + ?topic: string option; + nsfw: bool; + ?slow_mode_timeout <json name="rate_limit_per_user">: int option; +} + +type guild_voice = { + id: snowflake; + ?category_id <json name="parent_id">: snowflake option; + guild_id: snowflake; + name: string; + position: int; + ?topic: string option; + nsfw: bool; + ?user_limit: int option; + ?bitrate: int option; +} + +type category = { + id: snowflake; + ?category_id <json name="parent_id">: snowflake option; + position: int; + name: string; + nsfw: bool; +} + +type t = [ + Group of group + | Private of dm + | GuildText of guild_text + | GuildVoice of guild_voice + | Category of category +] + +type channel_wrapper = { id: snowflake; kind <json name="type">: int; ?guild_id: snowflake option; @@ -9,11 +66,14 @@ type t = { ?name: string option; ?topic: string option; ?nsfw: bool option; + ?last_message_id: snowflake option; ?bitrate: int option; ?user_limit: int option; + ?rate_limit_per_user: int option; ?recipients: user list option; ?icon: string option; ?owner_id: snowflake option; ?application_id: snowflake option; - ?parent_id: snowflake option; + ?category_id <json name="parent_id">: snowflake option; + ?last_pin_timestamp: string option; }
\ No newline at end of file diff --git a/lib/event.ml b/lib/event.ml index 6796ef5..8e04fee 100644 --- a/lib/event.ml +++ b/lib/event.ml @@ -49,14 +49,24 @@ let wrap_member ~guild_id member = let {nick;roles;joined_at;deaf;mute;user} = member in {nick;roles;joined_at;deaf;mute;user;guild_id} +let wrap_channel s : Channel_t.t = + let module J = Yojson.Safe in + match J.(from_string s |> Util.member "kind" |> Util.to_int) with + | 0 -> `GuildText (Channel_j.guild_text_of_string s) + | 1 -> `Private (Channel_j.dm_of_string s) + | 2 -> `GuildVoice (Channel_j.guild_voice_of_string s) + | 3 -> `Group (Channel_j.group_of_string s) + | 4 -> `Category (Channel_j.category_of_string s) + | _ -> raise (Invalid_event s) + let event_of_string ~contents t = match t with | "HELLO" -> HELLO (Yojson.Safe.from_string contents) | "READY" -> READY (Yojson.Safe.from_string contents) | "RESUMED" -> RESUMED (Yojson.Safe.from_string contents) | "INVALID_SESSION" -> INVALID_SESSION (Yojson.Safe.from_string contents) - | "CHANNEL_CREATE" -> CHANNEL_CREATE (Channel_j.t_of_string contents) - | "CHANNEL_UPDATE" -> CHANNEL_UPDATE (Channel_j.t_of_string contents) - | "CHANNEL_DELETE" -> CHANNEL_DELETE (Channel_j.t_of_string contents) + | "CHANNEL_CREATE" -> CHANNEL_CREATE (wrap_channel contents) + | "CHANNEL_UPDATE" -> CHANNEL_UPDATE (wrap_channel contents) + | "CHANNEL_DELETE" -> CHANNEL_DELETE (wrap_channel contents) | "CHANNEL_PINS_UPDATE" -> CHANNEL_PINS_UPDATE (Yojson.Safe.from_string contents) | "GUILD_CREATE" -> GUILD_CREATE (Guild_j.t_of_string contents) | "GUILD_UPDATE" -> GUILD_UPDATE (Guild_j.t_of_string contents) diff --git a/lib/guild.atd b/lib/guild.atd index aec3f86..c290b5f 100644 --- a/lib/guild.atd +++ b/lib/guild.atd @@ -1,8 +1,8 @@ type snowflake <ocaml from="Snowflake" t="t"> = abstract type user <ocaml from="User" t="t"> = abstract -type member <ocaml from="Member" t="t"> = abstract +type member <ocaml from="Member" t="member"> = abstract type role <ocaml from="Role" t="role"> = abstract -type channel <ocaml from="Channel" t="t"> = abstract +type channel <ocaml from="Channel" t="channel_wrapper"> = abstract type emoji <ocaml from="Emoji" t="t"> = abstract @@ -28,8 +28,8 @@ type t = { ?widget_enabled: bool option; ?widget_channel: channel option; ?system_channel: channel option; - ?large: bool option; - ?unavailable: bool option; + ~large: bool; + ~unavailable: bool; ?member_count: int option; ~members: member list; ~channels: channel list; diff --git a/lib/models/channel.ml b/lib/models/channel.ml index 328cc94..d52be05 100644 --- a/lib/models/channel.ml +++ b/lib/models/channel.ml @@ -1,3 +1,12 @@ +let 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 Channel_t *) + type t = Channel_t.t end
\ No newline at end of file diff --git a/lib/models/guild.ml b/lib/models/guild.ml index a10cd6c..733fc9f 100644 --- a/lib/models/guild.ml +++ b/lib/models/guild.ml @@ -47,7 +47,7 @@ module Make(Http : S.Http) = struct ("type", `Int kind); ]) >>| Result.map ~f:Channel_j.t_of_string - let delete guild = + let delete guild = Http.delete_guild guild.id >>| Result.map ~f:ignore let get_ban ~id guild = @@ -63,8 +63,8 @@ module Make(Http : S.Http) = struct let get_channel ~id guild = match List.find ~f:(fun c -> c.id = id) guild.channels with - | Some c -> Deferred.Or_error.return c - | None -> Http.get_channel id >>| Result.map ~f:Channel_j.t_of_string + | Some c -> Channel_j.(string_of_channel_wrapper c |> t_of_string) |> Deferred.Or_error.return + | None -> Http.get_channel id >>| Result.map ~f:Event.wrap_channel let get_emoji ~id guild = Http.get_emoji guild.id id >>| Result.map ~f:Emoji_j.t_of_string @@ -76,7 +76,7 @@ module Make(Http : S.Http) = struct let get_member ~id guild = match List.find ~f:(fun m -> m.user.id = id) guild.members with | Some m -> Deferred.Or_error.return m - | None -> Http.get_member guild.id id >>| Result.map ~f:Member_j.t_of_string + | None -> Http.get_member guild.id id >>| Result.map ~f:Member_j.member_of_string let get_prune_count ~days guild = Http.guild_prune_count guild.id days >>| Result.map ~f:(fun prune -> @@ -52,7 +52,7 @@ module type Guild = sig val get_channel : id:Snowflake_t.t -> Guild_t.t -> Channel_t.t Deferred.Or_error.t val get_emoji : id:Snowflake_t.t -> Guild_t.t -> Emoji_t.t Deferred.Or_error.t val get_invites : Guild_t.t -> string Deferred.Or_error.t - val get_member : id:Snowflake_t.t -> Guild_t.t -> Member_t.t Deferred.Or_error.t + val get_member : id:Snowflake_t.t -> Guild_t.t -> Member_t.member Deferred.Or_error.t val get_prune_count : days:int -> Guild_t.t -> int Deferred.Or_error.t val get_role : id:Snowflake_t.t -> Guild_t.t -> Role_t.t option val get_webhooks : Guild_t.t -> string Deferred.Or_error.t |