Current section
Files
Jump to
Current section
Files
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/3]).
-spec ws_frame_to_packet(binary()) -> {ok, shimmer@types@packet:packet()} |
{error, shimmer@internal@error:shimmer_error()}.
ws_frame_to_packet(Frame) ->
shimmer@types@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 => 17})
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(binary(), integer(), nerf@websocket:connection()) -> nil.
gateway_identify(Token, Intents, Conn) ->
Identify_payload = begin
_pipe = <<"{\"op\":2,\"d\":{\"token\":\""/utf8>>,
_pipe@1 = gleam@string:append(_pipe, Token),
_pipe@2 = gleam@string:append(_pipe@1, <<"\",\"intents\":"/utf8>>),
_pipe@3 = gleam@string:append(_pipe@2, gleam@int:to_string(Intents)),
gleam@string:append(
_pipe@3,
<<",\"properties\":{\"$os\":\"macos\",\"$browser\":\"shimmer\",\"$device\":\"shimmer\"}}}"/utf8>>
)
end,
nerf@websocket:send(Conn, Identify_payload).