Current section
Files
Jump to
Current section
Files
src/glyph@clients@bot.erl
-module(glyph@clients@bot).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/3, set_intents/2, initialize/1, send/3, on_message_create/2]).
-export_type([bot_error/0]).
-type bot_error() :: {bot_error, binary()}.
-spec new(binary(), binary(), binary()) -> glyph@models@discord:bot_client().
new(Token, Client_url, Client_version) ->
Rest_client = glyph@internal@network@rest:new(
bot,
Token,
{user_agent, Client_url, Client_version}
),
{bot_client,
bot,
Token,
Client_url,
Client_version,
0,
{event_handler, fun(_, _) -> {ok, nil} end},
Rest_client}.
-spec intent_to_bits(glyph@models@discord:gateway_intent()) -> integer().
intent_to_bits(Intent) ->
case Intent of
guilds ->
erlang:'bsl'(1, 0);
guild_members ->
erlang:'bsl'(1, 1);
guild_moderation ->
erlang:'bsl'(1, 2);
guild_emojis_and_stickers ->
erlang:'bsl'(1, 3);
guild_integrations ->
erlang:'bsl'(1, 4);
guild_webhooks ->
erlang:'bsl'(1, 5);
guild_invites ->
erlang:'bsl'(1, 6);
guild_voice_states ->
erlang:'bsl'(1, 7);
guild_presences ->
erlang:'bsl'(1, 8);
guild_messages ->
erlang:'bsl'(1, 9);
guild_message_reactions ->
erlang:'bsl'(1, 10);
guild_message_typing ->
erlang:'bsl'(1, 11);
direct_messages ->
erlang:'bsl'(1, 12);
direct_message_reactions ->
erlang:'bsl'(1, 13);
direct_message_typing ->
erlang:'bsl'(1, 14);
message_content ->
erlang:'bsl'(1, 15);
guild_scheduled_events ->
erlang:'bsl'(1, 16);
auto_moderation_configuration ->
erlang:'bsl'(1, 20);
auto_moderation_execution ->
erlang:'bsl'(1, 21)
end.
-spec set_intents(
glyph@models@discord:bot_client(),
list(glyph@models@discord:gateway_intent())
) -> glyph@models@discord:bot_client().
set_intents(B, Intents) ->
Intent_bits = gleam@list:fold(
Intents,
erlang:element(6, B),
fun(N, I) -> N + intent_to_bits(I) end
),
erlang:setelement(6, B, Intent_bits).
-spec get_gateway_info(glyph@models@discord:bot_client()) -> {ok,
glyph@models@discord:get_gateway_bot()} |
{error, bot_error()}.
get_gateway_info(Bot) ->
gleam@result:'try'(
begin
_pipe = glyph@internal@network@rest:get(
erlang:element(8, Bot),
<<"/gateway/bot"/utf8>>,
<<""/utf8>>
),
gleam@result:replace_error(
_pipe,
{bot_error, <<"Encountered error when sending request"/utf8>>}
)
end,
fun(Resp) ->
gleam@result:'try'(
begin
_pipe@1 = erlang:element(4, Resp),
_pipe@2 = gleam@json:decode(
_pipe@1,
glyph@internal@decoders:get_gateway_bot_decoder()
),
gleam@result:replace_error(
_pipe@2,
{bot_error,
<<"Encountered error when parsing JSON"/utf8>>}
)
end,
fun(Gateway_info) -> {ok, Gateway_info} end
)
end
).
-spec initialize(glyph@models@discord:bot_client()) -> {ok,
glyph@models@discord:bot_client()} |
{error, bot_error()}.
initialize(Bot) ->
Cache = glyph@internal@cache:initialize(),
gleam@result:'try'(
get_gateway_info(Bot),
fun(Gateway_info) ->
Supervisor_gateway_subj = gleam@erlang@process:new_subject(),
Gateway_actor = gleam@otp@supervisor:worker(
fun(_) ->
glyph@internal@network@gateway:start_gateway_actor(
Supervisor_gateway_subj,
Bot,
erlang:element(2, Gateway_info),
Cache
)
end
),
gleam@result:'try'(
begin
_pipe@1 = gleam@otp@supervisor:start(
fun(Children) -> _pipe = Children,
gleam@otp@supervisor:add(_pipe, Gateway_actor) end
),
gleam@result:replace_error(
_pipe@1,
{bot_error,
<<"Encountered error starting supervisor"/utf8>>}
)
end,
fun(_) -> {ok, Bot} end
)
end
).
-spec send(
glyph@models@discord:bot_client(),
binary(),
glyph@models@discord:message_payload()
) -> {ok, gleam@http@response:response(binary())} |
{error, gleam@hackney:error()}.
send(Bot, Channel_id, Message) ->
Message_json = glyph@internal@encoders:message_to_json(Message),
Endpoint = <<<<"/channels/"/utf8, Channel_id/binary>>/binary,
"/messages"/utf8>>,
glyph@internal@network@rest:post(
erlang:element(8, Bot),
Endpoint,
Message_json
).
-spec on_message_create(
glyph@models@discord:bot_client(),
fun((glyph@models@discord:bot_client(), glyph@models@discord:message()) -> {ok,
nil} |
{error, glyph@models@discord:discord_error()})
) -> glyph@models@discord:bot_client().
on_message_create(Bot, Callback) ->
erlang:setelement(7, Bot, {event_handler, Callback}).