Nostrum.Api.Message (nostrum v0.11.0-dev)

View Source

Module for interacting with the Discord API's message endpoints.

See: https://discord.com/developers/docs/resources/message

Summary

Functions

Deletes all reactions from a message.

Posts a message to a guild text or DM channel.

Same as delete/2, but takes a Nostrum.Struct.Message instead of a channel_id and message_id.

Deletes a message.

Deletes all reactions of a given emoji from a message.

Deletes another user's reaction from a message.

Same as edit/3, but takes a Nostrum.Struct.Message instead of a channel_id and message_id.

Edits a previously sent message in a channel.

Retrieves a message from a channel.

Creates a reaction for a message.

Gets all users who reacted with an emoji.

Deletes a reaction the current user has made for the message.

Functions

clear_reactions(channel_id, message_id)

(since 0.10.1)
@spec clear_reactions(Nostrum.Struct.Channel.id(), Nostrum.Struct.Message.id()) ::
  Nostrum.Api.error() | {:ok}

Deletes all reactions from a message.

This endpoint requires the VIEW_CHANNEL, READ_MESSAGE_HISTORY, and MANAGE_MESSAGES permissions. It fires a Nostrum.Consumer.message_reaction_remove_all/0 event.

If successful, returns {:ok}. Otherwise, return Nostrum.Api.error/0.

create(channel_id, options)

(since 0.10.1)

Posts a message to a guild text or DM channel.

This endpoint requires the VIEW_CHANNEL and SEND_MESSAGES permissions. It may situationally need the SEND_MESSAGES_TTS permission. It fires the Nostrum.Consumer.message_create/0 event.

If options is a string, options will be used as the message's content.

If successful, returns {:ok, message}. Otherwise, returns a Nostrum.Api.error/0.

Options

  • :content (string) - the message contents (up to 2000 characters)
  • :nonce (Nostrum.Snowflake.t/0) - a nonce that can be used for optimistic message sending
  • :tts (boolean) - true if this is a TTS message
  • :file (Path.t/0 | map) - the path of the file being sent, or a map with the following keys if sending a binary from memory

    • :name (string) - the name of the file
    • :body (string) - binary you wish to send
  • :files - a list of files where each element is the same format as the :file option. If both :file and :files are specified, :file will be prepended to the :files list.
  • :embeds (Nostrum.Struct.Embed.t/0) - a list of embedded rich content
  • :allowed_mentions (t:allowed_mentions/0) - see the allowed mentions type documentation
  • :message_reference (map) - See "Message references" below
  • :poll (Nostrum.Struct.Message.Poll.t/0) - A poll object to send with the message

At least one of the following is required: :content, :file, :embeds, :poll.

Message reference

You can create a reply to another message on guilds using this option, given that you have the VIEW_MESSAGE_HISTORY permission. To do so, include the message_reference field in your call. The complete structure documentation can be found on the Discord Developer Portal, but simply passing message_id will suffice:

def my_command(msg) do
  # Reply to the author - ``msg`` is a ``Nostrum.Struct.Message``
  Nostrum.Api.Message.create(
    msg.channel_id,
    content: "Hello",
    message_reference: %{message_id: msg.id}
  )
end

Passing a list will merge the settings provided

Examples

Nostrum.Api.Message.create(43189401384091, content: "hello world!")

Nostrum.Api.Message.create(43189401384091, "hello world!")

import Nostrum.Struct.Embed
embed =
  %Nostrum.Struct.Embed{}
  |> put_title("embed")
  |> put_description("new desc")
Nostrum.Api.Message.create(43189401384091, embeds: [embed])

Nostrum.Api.Message.Message.create(43189401384091, file: "/path/to/file.txt")

Nostrum.Api.Message.create(43189401384091, content: "hello world!", embeds: [embed], file: "/path/to/file.txt")

Nostrum.Api.Message.create(43189401384091, content: "Hello @everyone", allowed_mentions: :none)

delete(message)

(since 0.10.1)
@spec delete(Nostrum.Struct.Message.t()) :: Nostrum.Api.error() | {:ok}

Same as delete/2, but takes a Nostrum.Struct.Message instead of a channel_id and message_id.

delete(channel_id, message_id)

(since 0.10.1)

Deletes a message.

This endpoint requires the 'VIEW_CHANNEL' and 'MANAGE_MESSAGES' permission. It fires the MESSAGE_DELETE event.

If successful, returns {:ok}. Otherwise, returns a Nostrum.Api.error/0.

Examples

Nostrum.Api.Message.delete(43189401384091, 43189401384091)

delete_emoji_reactions(channel_id, message_id, emoji)

(since 0.10.1)
@spec delete_emoji_reactions(
  Nostrum.Struct.Channel.id(),
  Nostrum.Struct.Message.id(),
  Nostrum.Api.emoji()
) :: Nostrum.Api.error() | {:ok}

Deletes all reactions of a given emoji from a message.

This endpoint requires the MANAGE_MESSAGES permissions. It fires a Nostrum.Consumer.message_reaction_remove_emoji/0 event.

If successful, returns {:ok}. Otherwise, returns Nostrum.Api.error/0.

See react/3 for similar examples.

delete_user_reaction(channel_id, message_id, emoji, user_id)

(since 0.10.1)

Deletes another user's reaction from a message.

This endpoint requires the VIEW_CHANNEL, READ_MESSAGE_HISTORY, and MANAGE_MESSAGES permissions. It fires a Nostrum.Consumer.message_reaction_remove/0 event.

If successful, returns {:ok}. Otherwise, returns Nostrum.Api.error/0.

See react/3 for similar examples.

edit(message, options)

(since 0.10.1)

Same as edit/3, but takes a Nostrum.Struct.Message instead of a channel_id and message_id.

edit(channel_id, message_id, options)

(since 0.10.1)

Edits a previously sent message in a channel.

This endpoint requires the VIEW_CHANNEL permission. It fires the Nostrum.Consumer.message_update/0 event.

If options is a string, options will be used as the message's content.

If successful, returns {:ok, message}. Otherwise, returns a Nostrum.Api.error/0.

Options

  • :content (string) - the message contents (up to 2000 characters)
  • :embeds (Nostrum.Struct.Embed.t/0) - a list of embedded rich content
  • :files - a list of files where each element is the same format as the :file option. If both :file and :files are specified, :file will be prepended to the :files list. See create_message/2 for more information.

Note that if you edit a message with attachments, all attachments that should be present after edit must be included in your request body. This includes attachments that were sent in the original request.

Examples

Nostrum.Api.Message.edit(43189401384091, 1894013840914098, content: "hello world!")

Nostrum.Api.Message.edit(43189401384091, 1894013840914098, "hello world!")

import Nostrum.Struct.Embed
embed =
  %Nostrum.Struct.Embed{}
  |> put_title("embed")
  |> put_description("new desc")
Nostrum.Api.Message.edit(43189401384091, 1894013840914098, embeds: [embed])

Nostrum.Api.Message.edit(43189401384091, 1894013840914098, content: "hello world!", embeds: [embed])

get(channel_id, message_id)

(since 0.10.1)

Retrieves a message from a channel.

This endpoint requires the 'VIEW_CHANNEL' and 'READ_MESSAGE_HISTORY' permissions.

If successful, returns {:ok, message}. Otherwise, returns a Nostrum.Api.error/0.

Examples

Nostrum.Api.Message.get(43189401384091, 198238475613443)

react(channel_id, message_id, emoji)

(since 0.10.1)

Creates a reaction for a message.

This endpoint requires the VIEW_CHANNEL and READ_MESSAGE_HISTORY permissions. Additionally, if nobody else has reacted to the message with the emoji, this endpoint requires the ADD_REACTIONS permission. It fires a Nostrum.Consumer.message_reaction_add/0 event.

If successful, returns {:ok}. Otherwise, returns Nostrum.Api.error/0.

Examples

# Using a Nostrum.Struct.Emoji.
emoji = %Nostrum.Struct.Emoji{id: 43819043108, name: "foxbot"}
Nostrum.Api.Message.react(123123123123, 321321321321, emoji)

# Using a base 16 emoji string.
Nostrum.Api.Message.react(123123123123, 321321321321, "\xF0\x9F\x98\x81")

For other emoji string examples, see Nostrum.Struct.Emoji.api_name/0.

reactions(channel_id, message_id, emoji, params \\ [])

(since 0.10.1)

Gets all users who reacted with an emoji.

This endpoint requires the VIEW_CHANNEL and READ_MESSAGE_HISTORY permissions.

If successful, returns {:ok, users}. Otherwise, returns Nostrum.Api.error/0.

The optional params are after, the user ID to query after, absent by default, and limit, the max number of users to return, 1-100, 25 by default.

See react/3 for similar examples.

unreact(channel_id, message_id, emoji)

(since 0.10.1)

Deletes a reaction the current user has made for the message.

This endpoint requires the VIEW_CHANNEL and READ_MESSAGE_HISTORY permissions. It fires a Nostrum.Consumer.message_reaction_remove/0 event.

If successful, returns {:ok}. Otherwise, returns Nostrum.Api.error/0.

See react/3 for similar examples.