EDA.API.Message (EDA v0.3.0)

Copy Markdown View Source

REST API endpoints for Discord messages.

All functions return {:ok, result} or {:error, reason}.

Summary

Functions

Bulk deletes messages (2-100, not older than 14 days).

Creates a message in a channel.

Forwards a message to another channel.

Gets a message by ID.

Retrieves message history from a channel with automatic pagination.

Gets messages from a channel.

Pins a message in a channel.

Gets pinned messages in a channel, paginating automatically.

Gets one page of a channel's pins.

Purges messages from a channel with automatic chunking and 14-day filtering.

Replies to a message, automatically setting message_reference.

Returns a lazy Stream that yields messages from a channel, page by page.

Unpins a message from a channel.

Functions

bulk_delete(channel_id, message_ids)

@spec bulk_delete(String.t() | integer(), [String.t() | integer()]) ::
  :ok | {:error, term()}

Bulk deletes messages (2-100, not older than 14 days).

create(channel_id, content)

@spec create(String.t() | integer(), String.t() | map() | keyword()) ::
  {:ok, map()} | {:error, term()}

Creates a message in a channel.

Parameters

  • channel_id - The ID of the channel
  • content - Message content (string), full message payload (map), or keyword options

Examples

EDA.API.Message.create(channel_id, "Hello!")
EDA.API.Message.create(channel_id, content: "Look!", embeds: [embed])
EDA.API.Message.create(channel_id, content: "File!", files: [file])

delete(channel_id, message_id, opts \\ [])

@spec delete(String.t() | integer(), String.t() | integer(), keyword()) ::
  :ok | {:error, term()}

Deletes a message.

edit(channel_id, message_id, opts)

@spec edit(String.t() | integer(), String.t() | integer(), map() | keyword()) ::
  {:ok, map()} | {:error, term()}

Edits a message.

edit(channel_id, message_id, payload, opts \\ [])

forward(target_channel_id, source_channel_id, message_id)

@spec forward(String.t() | integer(), String.t() | integer(), String.t() | integer()) ::
  {:ok, map()} | {:error, term()}

Forwards a message to another channel.

Creates a message reference with type: 1 (forward) pointing to the original message.

Examples

EDA.API.Message.forward(target_channel_id, source_channel_id, message_id)

get(channel_id, message_id)

@spec get(String.t() | integer(), String.t() | integer()) ::
  {:ok, map()} | {:error, term()}

Gets a message by ID.

history(channel_id, limit, opts \\ [])

@spec history(String.t() | integer(), pos_integer() | :infinity, keyword()) ::
  {:ok, [map()]} | {:error, term()}

Retrieves message history from a channel with automatic pagination.

Handles Discord's 100-message-per-request limit transparently. Supports :infinity to retrieve all messages.

Options

  • :before - snowflake ID, get messages before this
  • :after - snowflake ID, get messages after this
  • :around - snowflake ID, get messages around this (single page, max 100)

Examples

{:ok, msgs} = EDA.API.Message.history(channel_id, 250)
{:ok, msgs} = EDA.API.Message.history(channel_id, 500, before: msg_id)
{:ok, msgs} = EDA.API.Message.history(channel_id, :infinity)

list(channel_id, opts \\ [])

@spec list(
  String.t() | integer(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Gets messages from a channel.

pin(channel_id, message_id, opts \\ [])

@spec pin(String.t() | integer(), String.t() | integer(), keyword()) ::
  :ok | {:error, term()}

Pins a message in a channel.

Options

  • :reason - Audit log reason

pinned(channel_id, opts \\ [])

@spec pinned(
  String.t() | integer(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Gets pinned messages in a channel, paginating automatically.

Returns message objects. Use pins/2 if you need the pinned_at timestamps or want to drive the pagination yourself.

Options

  • :limit - maximum number of messages to return (default :infinity)

Examples

{:ok, msgs} = EDA.API.Message.pinned(channel_id)
{:ok, msgs} = EDA.API.Message.pinned(channel_id, limit: 10)

pins(channel_id, opts \\ [])

@spec pins(
  String.t() | integer(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Gets one page of a channel's pins.

Returns Discord's raw envelope: %{"items" => [pin], "has_more" => boolean}, where each pin is %{"pinned_at" => iso8601, "message" => message}.

Options

  • :before - ISO8601 timestamp, get pins pinned before this (use the pinned_at of the last item of the previous page)
  • :limit - pins per page (1-50, default 50)

purge(channel_id, opts \\ [])

@spec purge(
  String.t() | integer(),
  keyword()
) :: {:ok, non_neg_integer()} | {:error, term()}

Purges messages from a channel with automatic chunking and 14-day filtering.

Options

  • :limit - max messages to purge (default 100, max :infinity)
  • :before - purge messages before this ID
  • :after - purge messages after this ID
  • :filter - predicate fn(message) -> boolean to select which messages to purge
  • :filter_old - filter out messages older than 14 days (default true)

Examples

{:ok, count} = EDA.API.Message.purge(channel_id, limit: 200)
{:ok, count} = EDA.API.Message.purge(channel_id,
  limit: 500,
  filter: fn msg -> msg["author"]["id"] == user_id end
)

reply(map, content)

@spec reply(map(), String.t() | keyword() | map()) :: {:ok, map()} | {:error, term()}

Replies to a message, automatically setting message_reference.

Accepts a message struct (with :channel_id and :id) or a raw map (with "channel_id" and "id").

Examples

EDA.API.Message.reply(msg, "Got it!")
EDA.API.Message.reply(msg, content: "Reply with embed", embeds: [embed])

stream(channel_id, opts \\ [])

@spec stream(
  String.t() | integer(),
  keyword()
) :: Enumerable.t()

Returns a lazy Stream that yields messages from a channel, page by page.

Options

  • :before - start before this message ID
  • :after - start after this message ID
  • :per_page - messages per request (1-100, default 100)

Examples

EDA.API.Message.stream(channel_id) |> Stream.take(50) |> Enum.to_list()
EDA.API.Message.stream(channel_id) |> Enum.find(&(&1["author"]["id"] == user_id))

unpin(channel_id, message_id, opts \\ [])

@spec unpin(String.t() | integer(), String.t() | integer(), keyword()) ::
  :ok | {:error, term()}

Unpins a message from a channel.

Options

  • :reason - Audit log reason