View Source Nostrum.Struct.Embed behaviour (Nostrum v0.10.0)

Functions that work on Discord embeds.

Building Embeds

Nostrum.Struct.Embeds can be built using this module's builder functions or standard Map syntax:

iex> import Nostrum.Struct.Embed
...> embed =
...>   %Nostrum.Struct.Embed{}
...>   |> put_title("craig")
...>   |> put_description("nostrum")
...>   |> put_url("https://google.com/")
...>   |> put_timestamp("2016-05-05T21:04:13.203Z")
...>   |> put_color(431_948)
...>   |> put_field("Field 1", "Test")
...>   |> put_field("Field 2", "More test", true)
...> embed
%Nostrum.Struct.Embed{
  title: "craig",
  description: "nostrum",
  url: "https://google.com/",
  timestamp: "2016-05-05T21:04:13.203Z",
  color: 431_948,
  fields: [
    %Nostrum.Struct.Embed.Field{name: "Field 1", value: "Test"},
    %Nostrum.Struct.Embed.Field{name: "Field 2", value: "More test", inline: true}
  ]
}

Using structs

You can also create Nostrum.Struct.Embeds from structs, by using the Nostrum.Struct.Embed module. Here's how the example above could be build using structs

  defmodule MyApp.MyStruct do
    use Nostrum.Struct.Embed

    defstruct []

    def title(_), do: "craig"
    def description(_), do: "nostrum"
    def url(_), do: "https://google.com/"
    def timestamp(_), do: "2016-05-05T21:04:13.203Z"
    def color(_), do: 431_948

    def fields(_) do
      [
        %Nostrum.Struct.Embed.Field{name: "Field 1", value: "Test"},
        %Nostrum.Struct.Embed.Field{name: "Field 2", value: "More test", inline: true}
      ]
    end
  end

iex> Nostrum.Struct.Embed.from(%MyApp.MyStruct{})
%Nostrum.Struct.Embed{
  title: "craig",
  description: "nostrum",
  url: "https://google.com/",
  timestamp: "2016-05-05T21:04:13.203Z",
  color: 431_948,
  fields: [
    %Nostrum.Struct.Embed.Field{name: "Field 1", value: "Test"},
    %Nostrum.Struct.Embed.Field{name: "Field 2", value: "More test", inline: true}
  ]
}

See this modules callbacks for a list of all the functions that can be implemented.

The implementation of these callbacks is optional. Not implemented functions will simply be ignored.

Summary

Types

Author information

Color code of the embed

Description of the embed

Fields information

Footer information

Image information

Provider information

t()

Thumbnail information

Timestamp of embed content

Title of the embed

Type of the embed

Url of the embed

Video information

Functions

Create an embed from a struct that implements the Nostrum.Struct.Embed behaviour

Puts the given value under :color in embed.

Puts the given value under :description in embed.

Puts a Nostrum.Struct.Embed.Image under :image in embed.

Puts a Nostrum.Struct.Embed.Thumbnail under :thumbnail in embed.

Puts the given value under :timestamp in embed.

Puts the given value under :title in embed.

Puts the given value under :url in embed.

Types

@type author() :: Nostrum.Struct.Embed.Author.t() | nil

Author information

@type color() :: integer() | nil

Color code of the embed

@type description() :: String.t() | nil

Description of the embed

@type fields() :: [Nostrum.Struct.Embed.Field.t()] | nil

Fields information

@type footer() :: Nostrum.Struct.Embed.Footer.t() | nil

Footer information

@type image() :: Nostrum.Struct.Embed.Image.t() | nil

Image information

@type provider() :: Nostrum.Struct.Embed.Provider.t() | nil

Provider information

@type t() :: %Nostrum.Struct.Embed{
  author: author(),
  color: color(),
  description: description(),
  fields: fields(),
  footer: footer(),
  image: image(),
  provider: provider(),
  thumbnail: thumbnail(),
  timestamp: timestamp(),
  title: title(),
  type: type(),
  url: url(),
  video: video()
}
@type thumbnail() :: Nostrum.Struct.Embed.Thumbnail.t() | nil

Thumbnail information

@type timestamp() :: String.t() | nil

Timestamp of embed content

@type title() :: String.t() | nil

Title of the embed

@type type() :: String.t() | nil

Type of the embed

@type url() :: String.t() | nil

Url of the embed

@type video() :: Nostrum.Struct.Embed.Video.t() | nil

Video information

Callbacks

@callback author(struct()) :: author()
@callback color(struct()) :: integer() | nil
@callback description(struct()) :: description()
@callback fields(struct()) :: fields()
@callback footer(struct()) :: footer()
@callback image(struct()) :: url()
@callback thumbnail(struct()) :: url()
@callback timestamp(struct()) :: timestamp()
@callback title(struct()) :: title()
@callback url(struct()) :: url()

Functions

Create an embed from a struct that implements the Nostrum.Struct.Embed behaviour

Link to this function

put_author(embed, name, url, icon_url)

View Source

Puts a Nostrum.Struct.Embed.Author under :author in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_author(embed, "skippi", "https://github.com/skippi", nil)
%Nostrum.Struct.Embed{
  author: %Nostrum.Struct.Embed.Author{
    name: "skippi",
    url: "https://github.com/skippi",
    icon_url: nil
  }
}
@spec put_color(t(), color()) :: t()

Puts the given value under :color in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_color(embed, 431948)
%Nostrum.Struct.Embed{color: 431948}
Link to this function

put_description(embed, value)

View Source
@spec put_description(t(), description()) :: t()

Puts the given value under :description in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_description(embed, "An elixir library for the discord API.")
%Nostrum.Struct.Embed{description: "An elixir library for the discord API."}
Link to this function

put_field(embed, name, value, inline \\ nil)

View Source

Adds a Nostrum.Struct.Embed.Field under :fields in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_field(embed, "First User", "b1nzy")
%Nostrum.Struct.Embed{
  fields: [
    %Nostrum.Struct.Embed.Field{name: "First User", value: "b1nzy"}
  ]
}

iex> embed = %Nostrum.Struct.Embed{
...>   fields: [
...>     %Nostrum.Struct.Embed.Field{name: "First User", value: "b1nzy"}
...>   ]
...> }
...> Nostrum.Struct.Embed.put_field(embed, "Second User", "Danny")
%Nostrum.Struct.Embed{
  fields: [
    %Nostrum.Struct.Embed.Field{name: "First User", value: "b1nzy"},
    %Nostrum.Struct.Embed.Field{name: "Second User", value: "Danny"}
  ]
}
Link to this function

put_footer(embed, text, icon_url \\ nil)

View Source

Puts a Nostrum.Struct.Embed.Footer under :footer in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_footer(embed, "Discord API", nil)
%Nostrum.Struct.Embed{
  footer: %Nostrum.Struct.Embed.Footer{
    text: "Discord API",
    icon_url: nil
  }
}

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_footer(embed, "nostrum footer", "https://discord.com/assets/53ef346458017da2062aca5c7955946b.svg")
%Nostrum.Struct.Embed{
  footer: %Nostrum.Struct.Embed.Footer{
    text: "nostrum footer",
    icon_url: "https://discord.com/assets/53ef346458017da2062aca5c7955946b.svg"
  }
}
@spec put_image(t(), Nostrum.Struct.Embed.Image.url()) :: t()

Puts a Nostrum.Struct.Embed.Image under :image in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_image(embed, "https://discord.com/assets/af92e60c16b7019f34a467383b31490a.svg")
%Nostrum.Struct.Embed{
  image: %Nostrum.Struct.Embed.Image{
    url: "https://discord.com/assets/af92e60c16b7019f34a467383b31490a.svg"
  }
}
Link to this function

put_thumbnail(embed, url)

View Source
@spec put_thumbnail(t(), Nostrum.Struct.Embed.Thumbnail.url()) :: t()

Puts a Nostrum.Struct.Embed.Thumbnail under :thumbnail in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_thumbnail(embed, "https://discord.com/assets/af92e60c16b7019f34a467383b31490a.svg")
%Nostrum.Struct.Embed{
  thumbnail: %Nostrum.Struct.Embed.Thumbnail{
    url: "https://discord.com/assets/af92e60c16b7019f34a467383b31490a.svg"
  }
}
Link to this function

put_timestamp(embed, value)

View Source
@spec put_timestamp(t(), timestamp()) :: t()

Puts the given value under :timestamp in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_timestamp(embed, "2018-04-21T17:33:51.893000Z")
%Nostrum.Struct.Embed{timestamp: "2018-04-21T17:33:51.893000Z"}
@spec put_title(t(), title()) :: t()

Puts the given value under :title in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_title(embed, "nostrum")
%Nostrum.Struct.Embed{title: "nostrum"}
@spec put_url(t(), url()) :: t()

Puts the given value under :url in embed.

Examples

iex> embed = %Nostrum.Struct.Embed{}
...> Nostrum.Struct.Embed.put_url(embed, "https://github.com/Kraigie/nostrum")
%Nostrum.Struct.Embed{url: "https://github.com/Kraigie/nostrum"}