Packages

A Gleam library for building Discord webhook payloads safely.

Current section

Files

Jump to
kitazith src kitazith@webhook@message.erl
Raw

src/kitazith@webhook@message.erl

-module(kitazith@webhook@message).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/kitazith/webhook/message.gleam").
-export([decode/1]).
-export_type([webhook_message/0, message_attachment/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type webhook_message() :: {webhook_message,
kitazith@snowflake:snowflake(),
kitazith@snowflake:snowflake(),
kitazith@timestamp:timestamp(),
gleam@option:option(kitazith@timestamp:timestamp()),
gleam@option:option(kitazith@snowflake:snowflake()),
gleam@option:option(list(kitazith@flag:message_flag())),
list(message_attachment())}.
-type message_attachment() :: {message_attachment,
kitazith@snowflake:snowflake(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
integer(),
binary(),
binary(),
gleam@option:option(integer()),
gleam@option:option(integer()),
gleam@option:option(boolean()),
gleam@option:option(float()),
gleam@option:option(binary()),
gleam@option:option(integer())}.
-file("src/kitazith/webhook/message.gleam", 185).
-spec snowflake_decoder() -> gleam@dynamic@decode:decoder(kitazith@snowflake:snowflake()).
snowflake_decoder() ->
_pipe = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:map(_pipe, fun kitazith@snowflake:new/1).
-file("src/kitazith/webhook/message.gleam", 112).
-spec message_attachment_decoder() -> gleam@dynamic@decode:decoder(message_attachment()).
message_attachment_decoder() ->
Snowflake_decoder = snowflake_decoder(),
begin
gleam@dynamic@decode:field(
<<"id"/utf8>>,
Snowflake_decoder,
fun(Id) ->
gleam@dynamic@decode:field(
<<"filename"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Filename) ->
gleam@dynamic@decode:optional_field(
<<"title"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Title) ->
gleam@dynamic@decode:optional_field(
<<"description"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Description) ->
gleam@dynamic@decode:optional_field(
<<"content_type"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Content_type) ->
gleam@dynamic@decode:field(
<<"size"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(Size) ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:field(
<<"proxy_url"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Proxy_url
) ->
gleam@dynamic@decode:optional_field(
<<"height"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_int/1}
),
fun(
Height
) ->
gleam@dynamic@decode:optional_field(
<<"width"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_int/1}
),
fun(
Width
) ->
gleam@dynamic@decode:optional_field(
<<"ephemeral"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_bool/1}
),
fun(
Ephemeral
) ->
gleam@dynamic@decode:optional_field(
<<"duration_secs"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_float/1}
),
fun(
Duration_secs
) ->
gleam@dynamic@decode:optional_field(
<<"waveform"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(
Waveform
) ->
gleam@dynamic@decode:optional_field(
<<"flags"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_int/1}
),
fun(
Flags
) ->
gleam@dynamic@decode:success(
{message_attachment,
Id,
Filename,
Title,
Description,
Content_type,
Size,
Url,
Proxy_url,
Height,
Width,
Ephemeral,
Duration_secs,
Waveform,
Flags}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end.
-file("src/kitazith/webhook/message.gleam", 190).
-spec message_timestamp_decoder() -> gleam@dynamic@decode:decoder(kitazith@timestamp:timestamp()).
message_timestamp_decoder() ->
_pipe = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:then(
_pipe,
fun(Value) -> case kitazith@timestamp:from_rfc3339(Value) of
{ok, Parsed} ->
gleam@dynamic@decode:success(Parsed);
{error, nil} ->
gleam@dynamic@decode:failure(
kitazith@timestamp:from_unix_seconds(0),
<<"RFC3339 timestamp"/utf8>>
)
end end
).
-file("src/kitazith/webhook/message.gleam", 66).
-spec webhook_message_decoder() -> gleam@dynamic@decode:decoder(webhook_message()).
webhook_message_decoder() ->
Snowflake_decoder = snowflake_decoder(),
Timestamp_decoder = message_timestamp_decoder(),
begin
gleam@dynamic@decode:field(
<<"id"/utf8>>,
Snowflake_decoder,
fun(Id) ->
gleam@dynamic@decode:field(
<<"channel_id"/utf8>>,
Snowflake_decoder,
fun(Channel_id) ->
gleam@dynamic@decode:field(
<<"timestamp"/utf8>>,
Timestamp_decoder,
fun(Message_timestamp) ->
gleam@dynamic@decode:optional_field(
<<"edited_timestamp"/utf8>>,
none,
gleam@dynamic@decode:optional(
Timestamp_decoder
),
fun(Edited_timestamp) ->
gleam@dynamic@decode:optional_field(
<<"webhook_id"/utf8>>,
none,
gleam@dynamic@decode:optional(
Snowflake_decoder
),
fun(Webhook_id) ->
gleam@dynamic@decode:optional_field(
<<"flags"/utf8>>,
none,
gleam@dynamic@decode:optional(
begin
_pipe = {decoder,
fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:map(
_pipe,
fun kitazith@flag:from_int/1
)
end
),
fun(Flags) ->
gleam@dynamic@decode:optional_field(
<<"attachments"/utf8>>,
[],
gleam@dynamic@decode:list(
message_attachment_decoder(
)
),
fun(Attachments) ->
gleam@dynamic@decode:success(
{webhook_message,
Id,
Channel_id,
Message_timestamp,
Edited_timestamp,
Webhook_id,
Flags,
Attachments}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end.
-file("src/kitazith/webhook/message.gleam", 62).
?DOC(
" Decode a message object returned from\n"
" [`execute webhook` with the Query String Param `wait` set to `true`](https://docs.discord.com/developers/resources/webhook#execute-webhook).\n"
).
-spec decode(binary()) -> {ok, webhook_message()} |
{error, gleam@json:decode_error()}.
decode(Body) ->
gleam@json:parse(Body, webhook_message_decoder()).