-module(discord_gleam). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/discord_gleam.gleam"). -export([bot/3, continue/1, with_selector/2, stop/0, stop_abnormal/1, simple/2, new/3, start/1, send_message/4, create_dm_channel/2, send_direct_message/4, reply/5, kick_member/4, ban_member/4, delete_message/4, edit_message/5, wipe_global_commands/1, wipe_guild_commands/2, register_global_commands/2, register_guild_commands/3, interaction_reply_message/3, request_guild_members/5]). -export_type([next/2, mode/2, handler_message/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " The primary module of discord_gleam. \\\n" " This module contains high-level functions to interact with the Discord API. \\\n" " But you can always implement stuff yourself using the low-level functions from the rest of the library. \\\n" ). -opaque next(URZ, USA) :: {continue, URZ, gleam@option:option(gleam@erlang@process:selector(USA))} | stop | {stop_abnormal, binary()}. -opaque mode(USB, USC) :: {simple, discord_gleam@types@bot:bot(), list(fun((discord_gleam@types@bot:bot(), discord_gleam@event_handler:packet()) -> nil)), next(USB, USC), USB} | {normal, discord_gleam@types@bot:bot(), fun((gleam@erlang@process:selector(USC)) -> {USB, gleam@erlang@process:selector(USC)}), fun((discord_gleam@types@bot:bot(), USB, handler_message(USC)) -> next(USB, USC))}. -type handler_message(USD) :: {packet, discord_gleam@event_handler:packet()} | {user, USD}. -file("src/discord_gleam.gleam", 36). ?DOC( " Create a new bot instance.\n" " \n" " Example:\n" " ```gleam\n" " import discord_gleam/discord/intents\n" " \n" " fn main() {\n" " let bot = discord_gleam.bot(\"TOKEN\", \"CLIENT_ID\", intents.default()))\n" " }\n" " ```\n" ). -spec bot(binary(), binary(), discord_gleam@discord@intents:intents()) -> discord_gleam@types@bot:bot(). bot(Token, Client_id, Intents) -> {bot, Token, Client_id, Intents, {cache, booklet_ffi:make(maps:new())}, none}. -file("src/discord_gleam.gleam", 64). ?DOC( " Continue processing with the updated state. Use `with_selector` to add a \n" " selector for custom user messages.\n" ). -spec continue(USE) -> next(USE, any()). continue(State) -> {continue, State, none}. -file("src/discord_gleam.gleam", 69). ?DOC(" Add a selector for custom user messages.\n"). -spec with_selector(next(USI, USJ), gleam@erlang@process:selector(USJ)) -> next(USI, USJ). with_selector(State, Selector) -> case State of {continue, User_state, _} -> {continue, User_state, {some, Selector}}; _ -> State end. -file("src/discord_gleam.gleam", 80). ?DOC(" Stop the event loop\n"). -spec stop() -> next(any(), any()). stop() -> stop. -file("src/discord_gleam.gleam", 85). ?DOC(" Stop the event loop with an abnormal reason\n"). -spec stop_abnormal(binary()) -> next(any(), any()). stop_abnormal(Reason) -> {stop_abnormal, Reason}. -file("src/discord_gleam.gleam", 121). ?DOC(" Create a simple mode with multiple handlers\n"). -spec simple( discord_gleam@types@bot:bot(), list(fun((discord_gleam@types@bot:bot(), discord_gleam@event_handler:packet()) -> nil)) ) -> mode(nil, nil). simple(Bot, Handlers) -> {simple, Bot, Handlers, {continue, nil, none}, nil}. -file("src/discord_gleam.gleam", 134). ?DOC( " Create a normal mode with a single handler\n" " \n" " `on_init` function is called once discord websocket connection is\n" " initialized. It must return a tuple with initial state and selector for \n" " custom messages. If there is no custom messages, user can pass the same \n" " selector from the argument\n" ). -spec new( discord_gleam@types@bot:bot(), fun((gleam@erlang@process:selector(UTA)) -> {UTC, gleam@erlang@process:selector(UTA)}), fun((discord_gleam@types@bot:bot(), UTC, handler_message(UTA)) -> next(UTC, UTA)) ) -> mode(UTC, UTA). new(Bot, On_init, Handler) -> {normal, Bot, On_init, Handler}. -file("src/discord_gleam.gleam", 207). -spec internal_to_handler_message( discord_gleam@event_handler:handler_message(UTX) ) -> handler_message(UTX). internal_to_handler_message(Msg) -> case Msg of {discord_packet, Packet} -> {packet, Packet}; {user, Msg@1} -> {user, Msg@1} end. -file("src/discord_gleam.gleam", 216). -spec to_internal_next(next(UUA, UUB)) -> discord_gleam@event_handler:next(UUA, UUB). to_internal_next(Next) -> case Next of {continue, User_state, Opt} -> {continue, User_state, Opt}; stop -> stop; {stop_abnormal, Reason} -> {stop_abnormal, Reason} end. -file("src/discord_gleam.gleam", 190). -spec to_internal_mode(mode(UTR, UTS)) -> discord_gleam@event_handler:mode(UTR, UTS). to_internal_mode(Mode) -> case Mode of {simple, Bot, Handlers, Next, Nil_state} -> {simple, Bot, Handlers, to_internal_next(Next), Nil_state}; {normal, Bot@1, On_init, Handler} -> Handler@1 = fun(Bot@2, User_state, Msg) -> _pipe = Handler( Bot@2, User_state, internal_to_handler_message(Msg) ), to_internal_next(_pipe) end, {normal, Bot@1, On_init, Handler@1} end. -file("src/discord_gleam.gleam", 173). ?DOC( " Start the event loop with a current mode.\n" "\n" " Example:\n" " ```gleam\n" " import discord_gleam/discord/intents\n" " import discord_gleam/event_handler\n" " import gleam/erlang/process\n" " \n" " fn main() {\n" " let bot = discord_gleam.bot(\"TOKEN\", \"CLIENT_ID\", intents.default())\n" " \n" " let assert Ok(_) = \n" " discord_gleam.simple(bot, [handler])\n" " |> discord_gleam.start()\n" " \n" " process.sleep_forever()\n" " }\n" " \n" " fn handler(bot: bot.Bot, packet: event_handler.Packet) {\n" " case packet {\n" " event_handler.ReadyPacket(ready) -> {\n" " logging.log(logging.Info, \"Logged in as \" <> ready.d.user.username)\n" " }\n" " \n" " _ -> Nil\n" " }\n" " }\n" " ```\n" ). -spec start(mode(any(), any())) -> {ok, gleam@otp@actor:started(gleam@erlang@process:subject(discord_gleam@ws@event_loop:event_loop_message()))} | {error, gleam@otp@actor:start_error()}. start(Mode) -> State = booklet_ffi:make(maps:new()), discord_gleam@ws@event_loop:start_event_loop( to_internal_mode(Mode), <<"gateway.discord.gg"/utf8>>, false, <<""/utf8>>, State ). -file("src/discord_gleam.gleam", 242). ?DOC( " Send a message to a channel.\n" " \n" " Example:\n" " ```gleam\n" " import discord_gleam\n" " \n" " fn main() {\n" " ...\n" " \n" " let msg = discord_gleam.send_message(\n" " bot, \n" " \"CHANNEL_ID\",\n" " \"Hello world!\",\n" " [] // embeds\n" " )\n" " }\n" ). -spec send_message( discord_gleam@types@bot:bot(), binary(), binary(), list(discord_gleam@types@message:embed()) ) -> {ok, discord_gleam@types@message_send_response:message_send_response()} | {error, discord_gleam@internal@error:discord_error()}. send_message(Bot, Channel_id, Message, Embeds) -> Msg = {message, Message, Embeds}, discord_gleam@http@endpoints:send_message( erlang:element(2, Bot), Channel_id, Msg ). -file("src/discord_gleam.gleam", 255). ?DOC( " Create a DM channel with a user. \\\n" " Returns a channel object, or a DiscordError if it fails.\n" ). -spec create_dm_channel(discord_gleam@types@bot:bot(), binary()) -> {ok, discord_gleam@types@channel:channel()} | {error, discord_gleam@internal@error:discord_error()}. create_dm_channel(Bot, User_id) -> discord_gleam@http@endpoints:create_dm_channel( erlang:element(2, Bot), User_id ). -file("src/discord_gleam.gleam", 267). ?DOC( " Send a direct message to a user. \\\n" " Same use as `send_message`, but use user_id instead of channel_id. \\\n" " `discord_gleam.send_direct_message(bot, \"USER_ID\", \"Hello world!\", [])`\n" " \n" " Note: This will return a DiscordError if the DM channel cant be made\n" ). -spec send_direct_message( discord_gleam@types@bot:bot(), binary(), binary(), list(discord_gleam@types@message:embed()) ) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. send_direct_message(Bot, User_id, Message, Embeds) -> Msg = {message, Message, Embeds}, discord_gleam@http@endpoints:send_direct_message( erlang:element(2, Bot), User_id, Msg ). -file("src/discord_gleam.gleam", 291). ?DOC( " Reply to a message in a channel.\n" " \n" " Example:\n" " \n" " ```gleam\n" " import discord_gleam\n" " \n" " fn main() {\n" " ...\n" " \n" " discord_gleam.reply(bot, \"CHANNEL_ID\", \"MESSAGE_ID\", \"Hello world!\", [])\n" " }\n" " ```\n" ). -spec reply( discord_gleam@types@bot:bot(), binary(), binary(), binary(), list(discord_gleam@types@message:embed()) ) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. reply(Bot, Channel_id, Message_id, Message, Embeds) -> Msg = {reply, Message, Message_id, Embeds}, discord_gleam@http@endpoints:reply(erlang:element(2, Bot), Channel_id, Msg). -file("src/discord_gleam.gleam", 319). ?DOC( " Kicks an member from an server. \\\n" " The reason will be what is shown in the audit log.\n" " \n" " Example:\n" " \n" " ```gleam\n" " import discord_gleam\n" " \n" " fn main() {\n" " ...\n" " \n" " discord_gleam.kick_member(bot, \"GUILD_ID\", \"USER_ID\", \"REASON\")\n" " }\n" " \n" " For an full example, see the `examples/kick.gleam` file.\n" ). -spec kick_member(discord_gleam@types@bot:bot(), binary(), binary(), binary()) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. kick_member(Bot, Guild_id, User_id, Reason) -> discord_gleam@http@endpoints:kick_member( erlang:element(2, Bot), Guild_id, User_id, Reason ). -file("src/discord_gleam.gleam", 328). -spec ban_member(discord_gleam@types@bot:bot(), binary(), binary(), binary()) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. ban_member(Bot, Guild_id, User_id, Reason) -> discord_gleam@http@endpoints:ban_member( erlang:element(2, Bot), Guild_id, User_id, Reason ). -file("src/discord_gleam.gleam", 356). ?DOC( " Deletes an message from a channel. \\\n" " The reason will be what is shown in the audit log.\n" " \n" " Example:\n" " ```gleam\n" " import discord_gleam\n" " \n" " fn main() {\n" " ...\n" " \n" " discord_gleam.delete_message(\n" " bot, \n" " \"CHANNEL_ID\",\n" " \"MESSAGE_ID\",\n" " \"REASON\",\n" " )\n" " }\n" " \n" " For an full example, see the `examples/delete_message.gleam` file.\n" ). -spec delete_message( discord_gleam@types@bot:bot(), binary(), binary(), binary() ) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. delete_message(Bot, Channel_id, Message_id, Reason) -> discord_gleam@http@endpoints:delete_message( erlang:element(2, Bot), Channel_id, Message_id, Reason ). -file("src/discord_gleam.gleam", 367). ?DOC( " Edits an existing message in a channel. \\\n" " The message must have been sent by the bot itself.\n" ). -spec edit_message( discord_gleam@types@bot:bot(), binary(), binary(), binary(), list(discord_gleam@types@message:embed()) ) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. edit_message(Bot, Channel_id, Message_id, Content, Embeds) -> Msg = {message, Content, Embeds}, discord_gleam@http@endpoints:edit_message( erlang:element(2, Bot), Channel_id, Message_id, Msg ). -file("src/discord_gleam.gleam", 381). ?DOC( " Wipes all the global slash commands for the bot. \\\n" " Restarting your client might be required to see the changes. \\\n" ). -spec wipe_global_commands(discord_gleam@types@bot:bot()) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. wipe_global_commands(Bot) -> discord_gleam@http@endpoints:wipe_global_commands( erlang:element(2, Bot), erlang:element(3, Bot) ). -file("src/discord_gleam.gleam", 387). ?DOC( " Wipes all the guild slash commands for the bot. \\\n" " Restarting your client might be required to see the changes. \\\n" ). -spec wipe_guild_commands(discord_gleam@types@bot:bot(), binary()) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. wipe_guild_commands(Bot, Guild_id) -> discord_gleam@http@endpoints:wipe_guild_commands( erlang:element(2, Bot), erlang:element(3, Bot), Guild_id ). -file("src/discord_gleam.gleam", 396). ?DOC( " Registers a global slash command. \\\n" " Restarting your client might be required to see the changes. \\\n" ). -spec register_global_commands( discord_gleam@types@bot:bot(), list(discord_gleam@types@slash_command:slash_command()) ) -> {ok, nil} | {error, {discord_gleam@types@slash_command:slash_command(), discord_gleam@internal@error:discord_error()}}. register_global_commands(Bot, Commands) -> gleam@list:try_each( Commands, fun(Command) -> case discord_gleam@http@endpoints:register_global_command( erlang:element(2, Bot), erlang:element(3, Bot), Command ) of {ok, _} -> {ok, nil}; {error, Err} -> {error, {Command, Err}} end end ). -file("src/discord_gleam.gleam", 410). ?DOC( " Registers a guild-specific slash command. \\\n" " Restarting your client might be required to see the changes. \\\n" ). -spec register_guild_commands( discord_gleam@types@bot:bot(), binary(), list(discord_gleam@types@slash_command:slash_command()) ) -> {ok, nil} | {error, {discord_gleam@types@slash_command:slash_command(), discord_gleam@internal@error:discord_error()}}. register_guild_commands(Bot, Guild_id, Commands) -> gleam@list:try_each( Commands, fun(Command) -> case discord_gleam@http@endpoints:register_guild_command( erlang:element(2, Bot), erlang:element(3, Bot), Guild_id, Command ) of {ok, _} -> {ok, nil}; {error, Err} -> {error, {Command, Err}} end end ). -file("src/discord_gleam.gleam", 431). ?DOC(" Make a basic text reply to an interaction.\n"). -spec interaction_reply_message( discord_gleam@ws@packets@interaction_create:interaction_create_packet(), binary(), boolean() ) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}. interaction_reply_message(Interaction, Message, Ephemeral) -> discord_gleam@http@endpoints:interaction_send_text( Interaction, Message, Ephemeral ). -file("src/discord_gleam.gleam", 445). ?DOC( " Used to request all members of a guild. The server will send \n" " GUILD_MEMBERS_CHUNK events in response with up to 1000 members per chunk \n" " until all members that match the request have been sent.\n" " \n" " Nonce can only be up to 32 bytes. If you send an invalid nonce it will be\n" " ignored and the reply member_chunk(s) will not have a nonce set.\n" ). -spec request_guild_members( discord_gleam@types@bot:bot(), binary(), discord_gleam@ws@commands@request_guild_members:request_guild_members_option(), gleam@option:option(boolean()), gleam@option:option(binary()) ) -> nil. request_guild_members(Bot, Guild_id, Option, Presences, Nonce) -> discord_gleam@ws@commands@request_guild_members:request_guild_members( Bot, Guild_id, Option, Presences, Nonce ).