Packages

A Gleam library for interacting with the Discord API

Current section

Files

Jump to
shimmer src shimmer@ws@ws_utils.erl
Raw

src/shimmer@ws@ws_utils.erl

-module(shimmer@ws@ws_utils).
-compile(no_auto_import).
-export([ws_frame_to_packet/1, open_gateway/0, gateway_heartbeat/2, gateway_heartbeat_null/1, gateway_identify/2]).
-spec ws_frame_to_packet(binary()) -> {ok, shimmer@ws@packet:packet()} |
{error, shimmer@internal@error:shimmer_error()}.
ws_frame_to_packet(Frame) ->
shimmer@ws@packet:from_json_string(Frame).
-spec open_gateway() -> {ok, nerf@websocket:connection()} | {error, nil}.
open_gateway() ->
{ok, Conn@1} = case nerf@websocket:connect(
<<"gateway.discord.gg"/utf8>>,
<<"/?v=9&encoding=json"/utf8>>,
443,
[]
) of
{ok, Conn} -> {ok, Conn};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"shimmer/ws/ws_utils"/utf8>>,
function => <<"open_gateway"/utf8>>,
line => 14})
end,
{ok, Conn@1}.
-spec gateway_heartbeat(integer(), nerf@websocket:connection()) -> nil.
gateway_heartbeat(Sequence, Conn) ->
Payload = gleam@string:append(
<<"{\"op\": 1, \"d\": "/utf8>>,
gleam@string:append(gleam@int:to_string(Sequence), <<"}"/utf8>>)
),
nerf@websocket:send(Conn, Payload).
-spec gateway_heartbeat_null(nerf@websocket:connection()) -> nil.
gateway_heartbeat_null(Conn) ->
Payload = <<"{\"op\": 1, \"d\": null}"/utf8>>,
nerf@websocket:send(Conn, Payload).
-spec gateway_identify(
shimmer@ws@packet:identify_packet_data(),
nerf@websocket:connection()
) -> nil.
gateway_identify(Payload, Conn) ->
Identify_packet = {identify_packet, 2, none, none, Payload},
Payload_string = shimmer@ws@packet:json_string(Identify_packet),
nerf@websocket:send(Conn, Payload_string).