discord_gleam

The primary module of discord_gleam.
This module contains high-level functions to interact with the Discord API.
But you can always implement stuff yourself using the low-level functions from the rest of the library. \

Types

The message type for the event handler with custom user messages

pub type HandlerMessage(user_message) {
  Packet(event_handler.Packet)
  User(user_message)
}

Constructors

The mode of the event handler

Simple mode is used for simple bots that don’t need to handle custom user state and messages. Can have multiple handlers.

Normal mode is used for bots that need to handle custom user state and messages. Can have only one handler.

pub opaque type Mode(user_state, user_message)

Instruction on how event loop actor should proceed after handling an event

  • Continue - Continue processing with the updated state and optional selector for custom user messages
  • Stop - Stop the event loop
  • StopAbnormal - Stop the event loop with an abnormal reason
pub opaque type Next(user_state, user_message)

Values

pub fn ban_member(
  bot: bot.Bot,
  guild_id: String,
  user_id: String,
  reason: String,
) -> Result(Nil, @internal DiscordError)
pub fn bot(
  token: String,
  client_id: String,
  intents: intents.Intents,
) -> bot.Bot

Create a new bot instance.

Example:

import discord_gleam/discord/intents

fn main() {
  let bot = discord_gleam.bot("TOKEN", "CLIENT_ID", intents.default()))
}
pub fn continue(
  state: user_state,
) -> Next(user_state, user_message)

Continue processing with the updated state. Use with_selector to add a selector for custom user messages.

pub fn create_dm_channel(
  bot: bot.Bot,
  user_id: String,
) -> Result(channel.Channel, @internal DiscordError)

Create a DM channel with a user.
Returns a channel object, or a DiscordError if it fails.

pub fn delete_message(
  bot: bot.Bot,
  channel_id: String,
  message_id: String,
  reason: String,
) -> Result(Nil, @internal DiscordError)

Deletes an message from a channel.
The reason will be what is shown in the audit log.

Example:

import discord_gleam

fn main() {
 ...

 discord_gleam.delete_message(
  bot,  
 "CHANNEL_ID",
 "MESSAGE_ID",
 "REASON",
 )
}

For an full example, see the `examples/delete_message.gleam` file.
pub fn edit_message(
  bot: bot.Bot,
  channel_id: String,
  message_id: String,
  content: String,
  embeds: List(message.Embed),
) -> Result(Nil, @internal DiscordError)

Edits an existing message in a channel.
The message must have been sent by the bot itself.

pub fn interaction_reply_message(
  interaction: interaction_create.InteractionCreatePacket,
  message: String,
  ephemeral: Bool,
) -> Result(Nil, @internal DiscordError)

Make a basic text reply to an interaction.

pub fn kick_member(
  bot: bot.Bot,
  guild_id: String,
  user_id: String,
  reason: String,
) -> Result(Nil, @internal DiscordError)

Kicks an member from an server.
The reason will be what is shown in the audit log.

Example:

import discord_gleam

fn main() {
 ...

 discord_gleam.kick_member(bot, "GUILD_ID", "USER_ID", "REASON")
}

For an full example, see the `examples/kick.gleam` file.
pub fn new(
  bot: bot.Bot,
  on_init: fn(process.Selector(user_message)) -> #(
    user_state,
    process.Selector(user_message),
  ),
  handler: fn(bot.Bot, user_state, HandlerMessage(user_message)) -> Next(
    user_state,
    user_message,
  ),
) -> Mode(user_state, user_message)

Create a normal mode with a single handler

on_init function is called once discord websocket connection is initialized. It must return a tuple with initial state and selector for custom messages. If there is no custom messages, user can pass the same selector from the argument

pub fn register_global_commands(
  bot: bot.Bot,
  commands: List(slash_command.SlashCommand),
) -> Result(
  Nil,
  #(slash_command.SlashCommand, @internal DiscordError),
)

Registers a global slash command.
Restarting your client might be required to see the changes. \

pub fn register_guild_commands(
  bot: bot.Bot,
  guild_id: String,
  commands: List(slash_command.SlashCommand),
) -> Result(
  Nil,
  #(slash_command.SlashCommand, @internal DiscordError),
)

Registers a guild-specific slash command.
Restarting your client might be required to see the changes. \

pub fn reply(
  bot: bot.Bot,
  channel_id: String,
  message_id: String,
  message: String,
  embeds: List(message.Embed),
) -> Result(Nil, @internal DiscordError)

Reply to a message in a channel.

Example:

import discord_gleam

fn main() {
 ...

 discord_gleam.reply(bot, "CHANNEL_ID", "MESSAGE_ID", "Hello world!", [])
}
pub fn request_guild_members(
  bot: bot.Bot,
  guild_id guild_id: String,
  option option: request_guild_members.RequestGuildMembersOption,
  presences presences: option.Option(Bool),
  nonce nonce: option.Option(String),
) -> Nil

Used to request all members of a guild. The server will send GUILD_MEMBERS_CHUNK events in response with up to 1000 members per chunk until all members that match the request have been sent.

Nonce can only be up to 32 bytes. If you send an invalid nonce it will be ignored and the reply member_chunk(s) will not have a nonce set.

pub fn send_direct_message(
  bot: bot.Bot,
  user_id: String,
  message: String,
  embeds: List(message.Embed),
) -> Result(Nil, @internal DiscordError)

Send a direct message to a user.
Same use as send_message, but use user_id instead of channel_id.
discord_gleam.send_direct_message(bot, "USER_ID", "Hello world!", [])

Note: This will return a DiscordError if the DM channel cant be made

pub fn send_message(
  bot: bot.Bot,
  channel_id: String,
  message: String,
  embeds: List(message.Embed),
) -> Result(
  message_send_response.MessageSendResponse,
  @internal DiscordError,
)

Send a message to a channel.

Example:

import discord_gleam

fn main() {
 ...

 let msg = discord_gleam.send_message(
  bot,  
  "CHANNEL_ID",
  "Hello world!",
  [] // embeds
 )
}
pub fn simple(
  bot: bot.Bot,
  handlers: List(fn(bot.Bot, event_handler.Packet) -> Nil),
) -> Mode(Nil, Nil)

Create a simple mode with multiple handlers

pub fn start(
  mode: Mode(user_state, user_message),
) -> Result(
  actor.Started(process.Subject(event_loop.EventLoopMessage)),
  actor.StartError,
)

Start the event loop with a current mode.

Example:

import discord_gleam/discord/intents
import discord_gleam/event_handler
import gleam/erlang/process

fn main() {
 let bot = discord_gleam.bot("TOKEN", "CLIENT_ID", intents.default())

 let assert Ok(_) = 
   discord_gleam.simple(bot, [handler])
   |> discord_gleam.start()

 process.sleep_forever()
}

fn handler(bot: bot.Bot, packet: event_handler.Packet) {
 case packet {
  event_handler.ReadyPacket(ready) -> {
    logging.log(logging.Info, "Logged in as " <> ready.d.user.username)
  }

  _ -> Nil
 }
}
pub fn stop() -> Next(user_state, user_message)

Stop the event loop

pub fn stop_abnormal(
  reason: String,
) -> Next(user_state, user_message)

Stop the event loop with an abnormal reason

pub fn wipe_global_commands(
  bot: bot.Bot,
) -> Result(Nil, @internal DiscordError)

Wipes all the global slash commands for the bot.
Restarting your client might be required to see the changes. \

pub fn wipe_guild_commands(
  bot: bot.Bot,
  guild_id: String,
) -> Result(Nil, @internal DiscordError)

Wipes all the guild slash commands for the bot.
Restarting your client might be required to see the changes. \

pub fn with_name(
  mode: Mode(user_state, user_message),
  name: process.Name(user_message),
) -> Mode(user_state, user_message)

Set process name for the event loop. Allows to use named subjects for custom user messages in normal mode.

pub fn with_selector(
  state: Next(user_state, user_message),
  selector: process.Selector(user_message),
) -> Next(user_state, user_message)

Add a selector for custom user messages.

Search Document