diff options
| author | Adelyn Breelove <[email protected]> | 2019-01-11 12:44:22 -0700 |
|---|---|---|
| committer | Adelyn Breelove <[email protected]> | 2019-01-11 12:44:22 -0700 |
| commit | cba1fa3ab1b085600f3a73dae62682d21d112afb (patch) | |
| tree | 89ef10a59d8a355356561e564f23777b4a8f66bf /lib | |
| parent | Fix some deserializing bits (diff) | |
| download | disml-cba1fa3ab1b085600f3a73dae62682d21d112afb.tar.xz disml-cba1fa3ab1b085600f3a73dae62682d21d112afb.zip | |
More deserialization fixes
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/dispatch.ml | 8 | ||||
| -rw-r--r-- | lib/event.ml | 32 | ||||
| -rw-r--r-- | lib/models/embed.ml | 14 | ||||
| -rw-r--r-- | lib/models/emoji.ml | 5 | ||||
| -rw-r--r-- | lib/models/member_t.ml | 11 | ||||
| -rw-r--r-- | lib/models/message_t.ml | 30 | ||||
| -rw-r--r-- | lib/models/reaction_t.ml | 8 | ||||
| -rw-r--r-- | lib/models/snowflake.ml | 4 |
8 files changed, 82 insertions, 30 deletions
diff --git a/lib/dispatch.ml b/lib/dispatch.ml index c52e81c..4a9b791 100644 --- a/lib/dispatch.ml +++ b/lib/dispatch.ml @@ -1,7 +1,9 @@ module Make(H : sig val handle_event : Event.t -> unit end) : S.Dispatch = struct let dispatch ~ev contents = - Printf.printf "Dispatching %s\n%!" ev; + (* Printf.printf "Dispatching %s\n%!" ev; *) (* print_endline (Yojson.Safe.prettify contents); *) - Event.event_of_yojson ~contents ev - |> H.handle_event + try + Event.event_of_yojson ~contents ev + |> H.handle_event + with Event.Invalid_event ev -> Printf.printf "Unknown event: %s%!" ev end
\ No newline at end of file diff --git a/lib/event.ml b/lib/event.ml index 824cd58..d6a6372 100644 --- a/lib/event.ml +++ b/lib/event.ml @@ -19,19 +19,19 @@ type t = | GUILD_EMOJIS_UPDATE of Yojson.Safe.json | GUILD_INTEGRATIONS_UPDATE of Yojson.Safe.json | GUILD_MEMBER_ADD of Member_t.t -| GUILD_MEMBER_REMOVE of Member_t.t -| GUILD_MEMBER_UPDATE of Member_t.t +| GUILD_MEMBER_REMOVE of Member_t.member_wrapper +| GUILD_MEMBER_UPDATE of Member_t.member_update | GUILD_MEMBERS_CHUNK of Member_t.t list | GUILD_ROLE_CREATE of Role_t.t (* * Guild_t.t *) | GUILD_ROLE_UPDATE of Role_t.t (* * Guild_t.t *) | GUILD_ROLE_DELETE of Role_t.t (* * Guild_t.t *) | MESSAGE_CREATE of Message_t.t -| MESSAGE_UPDATE of Message_t.t -| MESSAGE_DELETE of Message_t.t -| MESSAGE_BULK_DELETE of Message_t.t list -| MESSAGE_REACTION_ADD of (* Message_t.t * *) Reaction_t.t -| MESSAGE_REACTION_REMOVE of (* Message_t.t * *) Reaction_t.t -| MESSAGE_REACTION_REMOVE_ALL of (* Message_t.t * *) Reaction_t.t list +| MESSAGE_UPDATE of Message_t.message_update +| MESSAGE_DELETE of Snowflake.t * Snowflake.t +| MESSAGE_BULK_DELETE of Snowflake.t list +| MESSAGE_REACTION_ADD of Reaction_t.reaction_event +| MESSAGE_REACTION_REMOVE of Reaction_t.reaction_event +| MESSAGE_REACTION_REMOVE_ALL of Reaction_t.t list | PRESENCE_UPDATE of Presence.t | TYPING_START of Yojson.Safe.json | USER_UPDATE of Yojson.Safe.json @@ -56,18 +56,18 @@ let event_of_yojson ~contents t = match t with | "GUILD_EMOJIS_UPDATE" -> GUILD_EMOJIS_UPDATE contents | "GUILD_INTEGRATIONS_UPDATE" -> GUILD_INTEGRATIONS_UPDATE contents | "GUILD_MEMBER_ADD" -> GUILD_MEMBER_ADD (Member_t.of_yojson_exn contents) - | "GUILD_MEMBER_REMOVE" -> GUILD_MEMBER_REMOVE (let Member_t.{guild_id;member} = Member_t.member_update_of_yojson_exn contents in Member_t.wrap ~guild_id member) - | "GUILD_MEMBER_UPDATE" -> GUILD_MEMBER_UPDATE (let Member_t.{guild_id;member} = Member_t.member_update_of_yojson_exn contents in Member_t.wrap ~guild_id member) + | "GUILD_MEMBER_REMOVE" -> GUILD_MEMBER_REMOVE (Member_t.member_wrapper_of_yojson_exn contents) + | "GUILD_MEMBER_UPDATE" -> GUILD_MEMBER_UPDATE (Member_t.member_update_of_yojson_exn contents) | "GUILD_MEMBERS_CHUNK" -> GUILD_MEMBERS_CHUNK (Yojson.Safe.Util.to_list contents |> List.map ~f:Member_t.of_yojson_exn) | "GUILD_ROLE_CREATE" -> GUILD_ROLE_CREATE (let Role_t.{guild_id;role} = Role_t.role_update_of_yojson_exn contents in Role_t.wrap ~guild_id role) | "GUILD_ROLE_UPDATE" -> GUILD_ROLE_UPDATE (let Role_t.{guild_id;role} = Role_t.role_update_of_yojson_exn contents in Role_t.wrap ~guild_id role) | "GUILD_ROLE_DELETE" -> GUILD_ROLE_DELETE (let Role_t.{guild_id;role} = Role_t.role_update_of_yojson_exn contents in Role_t.wrap ~guild_id role) | "MESSAGE_CREATE" -> MESSAGE_CREATE (Message_t.of_yojson_exn contents) - | "MESSAGE_UPDATE" -> MESSAGE_UPDATE (Message_t.of_yojson_exn contents) - | "MESSAGE_DELETE" -> MESSAGE_DELETE (Message_t.of_yojson_exn contents) - | "MESSAGE_BULK_DELETE" -> MESSAGE_BULK_DELETE (Yojson.Safe.Util.to_list contents |> List.map ~f:Message_t.of_yojson_exn) - | "MESSAGE_REACTION_ADD" -> MESSAGE_REACTION_ADD (Reaction_t.of_yojson_exn contents) - | "MESSAGE_REACTION_REMOVE" -> MESSAGE_REACTION_REMOVE (Reaction_t.of_yojson_exn contents) + | "MESSAGE_UPDATE" -> MESSAGE_UPDATE (Message_t.message_update_of_yojson_exn contents) + | "MESSAGE_DELETE" -> MESSAGE_DELETE (Yojson.Safe.Util.(member "id" contents |> Snowflake.of_yojson_exn), Yojson.Safe.Util.(member "channel_id" contents |> Snowflake.of_yojson_exn)) + | "MESSAGE_BULK_DELETE" -> MESSAGE_BULK_DELETE (Yojson.Safe.Util.to_list contents |> List.map ~f:Snowflake.of_yojson_exn) + | "MESSAGE_REACTION_ADD" -> MESSAGE_REACTION_ADD (Reaction_t.reaction_event_of_yojson_exn contents) + | "MESSAGE_REACTION_REMOVE" -> MESSAGE_REACTION_REMOVE (Reaction_t.reaction_event_of_yojson_exn contents) | "MESSAGE_REACTION_REMOVE_ALL" -> MESSAGE_REACTION_REMOVE_ALL (Yojson.Safe.Util.to_list contents |> List.map ~f:Reaction_t.of_yojson_exn) | "PRESENCE_UPDATE" -> PRESENCE_UPDATE (Presence.of_yojson_exn contents) | "TYPING_START" -> TYPING_START contents @@ -75,4 +75,4 @@ let event_of_yojson ~contents t = match t with | "VOICE_STATE_UPDATE" -> VOICE_STATE_UPDATE contents | "VOICE_SERVER_UPDATE" -> VOICE_SERVER_UPDATE contents | "WEBHOOKS_UPDATE" -> WEBHOOKS_UPDATE contents - | s -> raise (Invalid_event s)
\ No newline at end of file + | s -> raise @@ Invalid_event s
\ No newline at end of file diff --git a/lib/models/embed.ml b/lib/models/embed.ml index 789e959..627d617 100644 --- a/lib/models/embed.ml +++ b/lib/models/embed.ml @@ -22,15 +22,22 @@ type provider = { url: string option [@default None]; } [@@deriving yojson] +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 yojson] + type field = { name: string; value: string; - inline: bool option [@default None]; + inline: bool [@default true]; } [@@deriving yojson] type t = { title: string option [@default None]; - kind: string option [@default None]; + kind: string option [@default None][@key "type"]; description: string option [@default None]; url: string option [@default None]; timestamp: string option [@default None]; @@ -40,5 +47,6 @@ type t = { thumbnail: image option [@default None]; video: video option [@default None]; provider: provider option [@default None]; + author: author option [@default None]; fields: field list [@default []]; -} [@@deriving yojson]
\ No newline at end of file +} [@@deriving yojson { strict = false }]
\ No newline at end of file diff --git a/lib/models/emoji.ml b/lib/models/emoji.ml index dccdb54..62e5bd7 100644 --- a/lib/models/emoji.ml +++ b/lib/models/emoji.ml @@ -1,3 +1,8 @@ +type partial_emoji = { + id: Snowflake.t option [@default None]; + name: string; +} [@@deriving yojson { strict = false }] + type t = { id: Snowflake.t option [@default None]; name: string; diff --git a/lib/models/member_t.ml b/lib/models/member_t.ml index 4132a20..ca39092 100644 --- a/lib/models/member_t.ml +++ b/lib/models/member_t.ml @@ -15,9 +15,16 @@ type member = { user: User_t.t; } [@@deriving yojson { strict = false}] +type member_wrapper = { + guild_id: Snowflake.t; + user: User_t.t; +} [@@deriving yojson { strict = false }] + type member_update = { - guild_id: Snowflake.t [@key "id"]; - member: member; + guild_id: Snowflake.t; + roles: Snowflake.t list [@default []]; + user: User_t.t; + nick: string option [@default None]; } [@@deriving yojson { strict = false}] type t = { diff --git a/lib/models/message_t.ml b/lib/models/message_t.ml index b8aa204..f793223 100644 --- a/lib/models/message_t.ml +++ b/lib/models/message_t.ml @@ -1,3 +1,25 @@ +type message_update = { + id: Snowflake.t; + author: User_t.t option [@default None]; + channel_id: Snowflake.t; + member: Member_t.partial_member option [@default None]; + guild_id: Snowflake.t option [@default None]; + content: string option [@default None]; + timestamp: string option [@default None]; + editedimestamp: string option [@default None]; + tts: bool option [@default None]; + mention_everyone: bool option [@default None]; + mentions: Snowflake.t list [@default []]; + role_mentions: Snowflake.t list [@default []]; + attachments: Attachment.t list [@default []]; + embeds: Embed.t list [@default []]; + reactions: Snowflake.t list [@default []]; + nonce: Snowflake.t option [@default None]; + pinned: bool option [@default None]; + webhook_id: Snowflake.t option [@default None]; + kind: int option [@default None][@key "type"]; +} [@@deriving yojson { strict = false}] + type t = { id: Snowflake.t; author: User_t.t; @@ -9,10 +31,10 @@ type t = { editedimestamp: string option [@default None]; tts: bool; mention_everyone: bool; - mentions: Snowflake.t list; - role_mentions: Snowflake.t list [@default []]; - attachments: Attachment.t list; - embeds: Embed.t list; + (* mentions: Snowflake.t list [@default []]; *) + (* role_mentions: Snowflake.t list [@default []]; *) + attachments: Attachment.t list [@default []]; + embeds: Embed.t list [@default []]; reactions: Snowflake.t list [@default []]; nonce: Snowflake.t option [@default None]; pinned: bool; diff --git a/lib/models/reaction_t.ml b/lib/models/reaction_t.ml index f60561e..2e9aad5 100644 --- a/lib/models/reaction_t.ml +++ b/lib/models/reaction_t.ml @@ -1,3 +1,11 @@ +type reaction_event = { + user_id: Snowflake.t; + channel_id: Snowflake.t; + message_id: Snowflake.t; + guild_id: Snowflake.t option [@default None]; + emoji: Emoji.partial_emoji; +} [@@deriving yojson] + type t = { count: int; emoji: Emoji.t; diff --git a/lib/models/snowflake.ml b/lib/models/snowflake.ml index 9e630e1..21846fc 100644 --- a/lib/models/snowflake.ml +++ b/lib/models/snowflake.ml @@ -2,10 +2,10 @@ open Core type t = int -let of_yojson_exn d = Yojson.Safe.Util.to_string d |> Int.of_string |> Ok +let of_yojson_exn d = Yojson.Safe.Util.to_string d |> Int.of_string let of_yojson d = - try of_yojson_exn d + try of_yojson_exn d |> Ok with Yojson.Safe.Util.Type_error (why,_) -> Error why let to_yojson s : Yojson.Safe.json = `String (Int.to_string s) |