Nostrum.Struct.Embed behaviour (nostrum v0.11.0-dev)

View Source

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

author()

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

Author information

color()

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

Color code of the embed

description()

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

Description of the embed

fields()

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

Fields information

footer()

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

Footer information

image()

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

Image information

provider()

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

Provider information

t()

@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()
}

thumbnail()

@type thumbnail() :: Nostrum.Struct.Embed.Thumbnail.t() | nil

Thumbnail information

timestamp()

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

Timestamp of embed content

title()

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

Title of the embed

type()

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

Type of the embed

url()

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

Url of the embed

video()

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

Video information

Callbacks

author(struct)

@callback author(struct()) :: author()

color(struct)

@callback color(struct()) :: integer() | nil

description(struct)

@callback description(struct()) :: description()

fields(struct)

@callback fields(struct()) :: fields()

footer(struct)

@callback footer(struct()) :: footer()

image(struct)

@callback image(struct()) :: url()

thumbnail(struct)

@callback thumbnail(struct()) :: url()

timestamp(struct)

@callback timestamp(struct()) :: timestamp()

title(struct)

@callback title(struct()) :: title()

url(struct)

@callback url(struct()) :: url()

Functions

from(struct)

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

put_author(embed, name, url, icon_url)

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
  }
}

put_color(embed, value)

@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}

put_description(embed, value)

@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."}

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

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"}
  ]
}

put_footer(embed, text, icon_url \\ nil)

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"
  }
}

put_image(embed, url)

@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"
  }
}

put_thumbnail(embed, url)

@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"
  }
}

put_timestamp(embed, value)

@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"}

put_title(embed, value)

@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"}

put_url(embed, value)

@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"}