From c42603da68df3610d063d7b232b53bb45e0183e1 Mon Sep 17 00:00:00 2001 From: Mishio595 Date: Sun, 14 Oct 2018 08:29:52 -0600 Subject: Initial commit --- .gitignore | 2 ++ dune | 6 ++++ dune-project | 1 + lib/animus.ml | 1 + lib/endpoints.ml | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/gateway.ml | 0 lib/http.ml | 43 ++++++++++++++++++++++++++ 7 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 dune create mode 100644 dune-project create mode 100644 lib/animus.ml create mode 100644 lib/endpoints.ml create mode 100644 lib/gateway.ml create mode 100644 lib/http.ml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..297e7e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.merlin +_build/ \ No newline at end of file diff --git a/dune b/dune new file mode 100644 index 0000000..62061f5 --- /dev/null +++ b/dune @@ -0,0 +1,6 @@ +(library + (name animus) + (libraries lwt lwt.unix cohttp cohttp.lwt yojson) +) + +(include_subdirs unqualified) \ No newline at end of file diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..31a7429 --- /dev/null +++ b/dune-project @@ -0,0 +1 @@ +(lang dune 1.3) diff --git a/lib/animus.ml b/lib/animus.ml new file mode 100644 index 0000000..baf4112 --- /dev/null +++ b/lib/animus.ml @@ -0,0 +1 @@ +Lwt_main.run (Lwt_io.printf "Hello, World!") \ No newline at end of file diff --git a/lib/endpoints.ml b/lib/endpoints.ml new file mode 100644 index 0000000..08f479d --- /dev/null +++ b/lib/endpoints.ml @@ -0,0 +1,94 @@ +let gateway = "/gateway" +let gateway_bot = "/gateway/bot" + +let channel channel_id = "/channels/"^channel_id +let channel_messages channel_id = "/channels/"^channel_id^"/messages" + +let channel_message channel_id msg_id = + "/channels/"^channel_id^"/messages/"^msg_id + +let channel_reaction_me channel_id msg_id emoji = + "/channels/"^channel_id^"/messages/"^msg_id^"/reactions/"^emoji^"/@me" + +let channel_reaction channel_id msg_id emoji user_id = + "/channels/"^channel_id^"/messages/"^msg_id^"/reactions/"^emoji^"/"^user_id + +let channel_reactions_get channel_id msg_id emoji = + "/channels/"^channel_id^"/messages/"^msg_id^"/reactions/"^emoji + +let channel_reactions_delete channel_id msg_id = + "/channels/"^channel_id^"/messages/"^msg_id^"/reactions" + +let channel_bulk_delete channel_id = "/channels/"^channel_id + +let channel_permission channel_id overwrite_id = + "/channels/"^channel_id^"/permissions/"^overwrite_id + +let channel_permissions channel_id = "/channels"^channel_id^"/permissions" +let channels = "/channels" +let channel_call_ring channel_id = "/channels/"^channel_id^"/call/ring" +let channel_invites channel_id = "/channels/"^channel_id^"/invites" +let channel_typing channel_id = "/channels/"^channel_id^"/typing" +let channel_pins channel_id = "/channels/"^channel_id^"/pins" +let channel_pin channel_id msg_id = "/channels/"^channel_id^"/pins/"^msg_id + +let guilds = "/guilds" +let guild guild_id = "/guilds/"^guild_id +let guild_channels guild_id = "/guilds/"^guild_id^"/channels" +let guild_members guild_id = "/guilds/"^guild_id^"/members" +let guild_member guild_id user_id = "/guilds/"^guild_id^"/members/"^user_id + +let guild_member_role guild_id user_id role_id = + "/guilds/"^guild_id^"/members/"^user_id^"/roles/"^role_id + +let guild_bans guild_id = "/guilds/"^guild_id^"/bans" +let guild_ban guild_id user_id = "/guilds/"^guild_id^"/bans"^user_id +let guild_roles guild_id = "/guilds/"^guild_id^"/roles" +let guild_role guild_id role_id = "/guilds/"^guild_id^"/roles"^role_id +let guild_prune guild_id = "/guilds/"^guild_id^"/prune" +let guild_voice_regions guild_id = "/guilds/"^guild_id^"/regions" +let guild_invites guild_id = "/guilds/"^guild_id^"/invites" +let guild_integrations guild_id = "/guilds/"^guild_id^"/integrations" + +let guild_integration guild_id integration_id = + "/guilds/"^guild_id^"/integrations/"^integration_id + +let guild_integration_sync guild_id integration_id = + "/guilds/"^guild_id^"/integrations/"^integration_id^"/sync" + +let guild_embed guild_id = "/guilds/"^guild_id^"/embed" +let guild_emojis guild_id = "/guilds/"^guild_id^"/emojis" +let guild_emoji guild_id emoji_id = "/guilds/"^guild_id^"/emojis/"^emoji_id + +let webhooks_guild guild_id = "/guilds/"^guild_id^"/webhooks" +let webhooks_channel channel_id = "/channels/"^channel_id^"/webhooks" +let webhook webhook_id = "/webhooks/"^webhook_id +let webhook_token webhook_id token = "/webhooks/"^webhook_id^"/"^token + +let webhook_git webhook_id token = + "/webhooks/"^webhook_id^"/"^token^"/github" + +let webhook_slack webhook_id token = + "/webhooks/"^webhook_id^"/"^token^"/slack" + +let user user_id = "/users/"^user_id + +let me = "/users/@me" +let me_guilds = "/users/@me/guilds" +let me_guild guild_id = "/users/@me/guilds/"^guild_id +let me_channels = "/users/@me/channels" +let me_connections = "/users/@me/connections" + +let invite code = "/invites/"^code +let regions = "/voice/regions" + +let application_information = "/oauth2/applications/@me" + +let group_recipient group_id user_id = "/channels/"^group_id^"/recipients/"^user_id +let guild_me_nick guild_id = "/guilds/"^guild_id^"/members/@me/nick" + +let cdn_avatar id avatar image_format = "/avatars/"^id^"/"^avatar^"."^image_format +let cdn_embed_avatar image_name = "/embed/avatars/"^image_name^".png" +let cdn_emoji id image_format = "/emojis/"^id^"."^image_format +let cdn_icon id icon image_format = "/icons/"^id^"/"^icon^"."^image_format +let cdn_avatar id splash image_format = "/splashes/"^id^"/"^splash^"."^image_format \ No newline at end of file diff --git a/lib/gateway.ml b/lib/gateway.ml new file mode 100644 index 0000000..e69de29 diff --git a/lib/http.ml b/lib/http.ml new file mode 100644 index 0000000..ba73fd9 --- /dev/null +++ b/lib/http.ml @@ -0,0 +1,43 @@ +open Lwt.Infix +open Cohttp +open Cohttp_lwt_unix + +module Base = struct + exception Invalid_Method + + let base_url = "https://discordapp.com/api/v7" + let cdn_url = "https://cdn.discordapp.com" + + let process_url path = + Uri.of_string (base_url ^ path) + + let process_request_body body = + body + |> Yojson.Basic.to_string + |> Cohttp_lwt.Body.of_string + + let process_request_headers () = + let token = try + Sys.getenv "DISCORD_TOKEN" + with Not_found -> failwith "Please provide a token" in + let h = Header.init_with "User-Agent" "Animus v0.1.0" in + let h = Header.add h "Authorization" ("Bot " ^ token) in + Header.add h "Content-Type" "application/json" + + (* TODO Finish processor *) + let process_response (resp, body) = + body |> Cohttp_lwt.Body.to_string >|= Yojson.Basic.from_string + + let request ?(body=`Null) m path = + let uri = process_url path in + let headers = process_request_headers () in + let body = process_request_body body in + (match m with + | `DELETE -> Client.delete ~headers ~body uri + | `GET -> Client.get ~headers uri + | `PATCH -> Client.patch ~headers ~body uri + | `POST -> Client.post ~headers ~body uri + | `PUT -> Client.put ~headers ~body uri + | _ -> raise Invalid_Method) + >|= process_response +end \ No newline at end of file -- cgit v1.2.3