DiscordInteractions.Embed (discord_interactions v0.1.0)
View SourceHelper module for creating Discord embed objects.
This module provides functions for creating and manipulating Discord embed objects as defined in the Discord API documentation: https://discord.com/developers/docs/resources/channel#embed-object
Embeds are rich content blocks that can contain formatted text, images, fields, and more. They can be used in messages sent by bots and webhooks.
Embed Structure
An embed object can have the following fields:
title
: Title of the embed (up to 256 characters)description
: Description of the embed (up to 4096 characters)url
: URL for the embed title to link totimestamp
: ISO8601 timestamp for the embed footercolor
: Color code of the embed (as an integer or hex string)footer
: Footer information (text, icon URL)image
: Image information (URL, dimensions)thumbnail
: Thumbnail information (URL, dimensions)video
: Video information (URL, dimensions)provider
: Provider information (name, URL)author
: Author information (name, URL, icon URL)fields
: Array of field objects (name, value, inline)
Examples
# Create a basic embed
embed = DiscordInteractions.Embed.new()
|> DiscordInteractions.Embed.title("Hello, World!")
|> DiscordInteractions.Embed.description("This is an embed description")
|> DiscordInteractions.Embed.color(0x00FF00)
# Create an embed with fields
embed = DiscordInteractions.Embed.new()
|> DiscordInteractions.Embed.title("User Profile")
|> DiscordInteractions.Embed.add_field("Name", "John Doe", true)
|> DiscordInteractions.Embed.add_field("Age", "30", true)
|> DiscordInteractions.Embed.add_field("Bio", "Lorem ipsum dolor sit amet")
# Create an embed with author and footer
embed = DiscordInteractions.Embed.new()
|> DiscordInteractions.Embed.title("News Update")
|> DiscordInteractions.Embed.description("Breaking news!")
|> DiscordInteractions.Embed.author("News Bot", "https://example.com", "https://example.com/icon.png")
|> DiscordInteractions.Embed.footer("Posted at 12:00 PM", "https://example.com/footer-icon.png")
Summary
Functions
Adds a field to an embed.
Sets the author of an embed.
Sets the color of an embed.
Sets the description of an embed.
Sets the footer of an embed.
Sets the image of an embed.
Creates a new empty embed object.
Sets the provider of an embed.
Sets the thumbnail of an embed.
Sets the timestamp of an embed.
Sets the title of an embed.
Sets the URL of an embed.
Sets the video of an embed.
Types
@type t() :: %{ optional(:title) => String.t(), optional(:description) => String.t(), optional(:url) => String.t(), optional(:timestamp) => String.t(), optional(:color) => integer(), optional(:footer) => map(), optional(:image) => map(), optional(:thumbnail) => map(), optional(:video) => map(), optional(:provider) => map(), optional(:author) => map(), optional(:fields) => [map()] }
Functions
Adds a field to an embed.
Parameters
embed
: The embed to modifyname
: The name of the field (up to 256 characters)value
: The value of the field (up to 1024 characters)inline
: Whether the field should be displayed inline (default: false)
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.add_field(embed, "Name", "Value")
%{fields: [%{name: "Name", value: "Value", inline: false}]}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.add_field(embed, "Name", "Value", true)
%{fields: [%{name: "Name", value: "Value", inline: true}]}
iex> embed = DiscordInteractions.Embed.new() |> DiscordInteractions.Embed.add_field("Field 1", "Value 1")
iex> DiscordInteractions.Embed.add_field(embed, "Field 2", "Value 2")
%{fields: [%{name: "Field 1", value: "Value 1", inline: false}, %{name: "Field 2", value: "Value 2", inline: false}]}
Sets the author of an embed.
Parameters
embed
: The embed to modifyname
: Name of the authorurl
: Optional URL of the authoricon_url
: Optional URL of the author iconproxy_icon_url
: Optional proxied URL of the author icon
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.author(embed, "John Doe")
%{author: %{name: "John Doe"}}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.author(embed, "John Doe", "https://example.com", "https://example.com/icon.png")
%{author: %{name: "John Doe", url: "https://example.com", icon_url: "https://example.com/icon.png"}}
Sets the color of an embed.
Parameters
embed
: The embed to modifycolor
: The color as an integer or hex string
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.color(embed, 0x00FF00)
%{color: 65280}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.color(embed, "#00FF00")
%{color: 65280}
Sets the description of an embed.
Parameters
embed
: The embed to modifydescription
: The description text (up to 4096 characters)
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.description(embed, "This is a description")
%{description: "This is a description"}
Sets the image of an embed.
Parameters
embed
: The embed to modifyurl
: URL of the imageopts
: Optional parameters (proxy_url, height, width)
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.image(embed, "https://example.com/image.png")
%{image: %{url: "https://example.com/image.png"}}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.image(embed, "https://example.com/image.png", proxy_url: "https://proxy.example.com/image.png", height: 300, width: 400)
%{image: %{url: "https://example.com/image.png", proxy_url: "https://proxy.example.com/image.png", height: 300, width: 400}}
Creates a new empty embed object.
Examples
iex> DiscordInteractions.Embed.new()
%{}
iex> DiscordInteractions.Embed.new(title: "Hello", description: "World")
%{title: "Hello", description: "World"}
Sets the provider of an embed.
Parameters
embed
: The embed to modifyname
: Name of the providerurl
: URL of the provider
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.provider(embed, "Example Provider")
%{provider: %{name: "Example Provider"}}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.provider(embed, "Example Provider", "https://example.com")
%{provider: %{name: "Example Provider", url: "https://example.com"}}
Sets the thumbnail of an embed.
Parameters
embed
: The embed to modifyurl
: URL of the thumbnailopts
: Optional parameters (proxy_url, height, width)
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.thumbnail(embed, "https://example.com/thumbnail.png")
%{thumbnail: %{url: "https://example.com/thumbnail.png"}}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.thumbnail(embed, "https://example.com/thumbnail.png", proxy_url: "https://proxy.example.com/thumbnail.png", height: 100, width: 100)
%{thumbnail: %{url: "https://example.com/thumbnail.png", proxy_url: "https://proxy.example.com/thumbnail.png", height: 100, width: 100}}
@spec timestamp(t(), String.t() | DateTime.t()) :: t()
Sets the timestamp of an embed.
Parameters
embed
: The embed to modifytimestamp
: The timestamp as a DateTime or ISO8601 string
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.timestamp(embed, "2023-01-01T12:00:00Z")
%{timestamp: "2023-01-01T12:00:00Z"}
iex> embed = DiscordInteractions.Embed.new()
iex> timestamp = DateTime.from_naive!(~N[2023-01-01 12:00:00], "Etc/UTC")
iex> DiscordInteractions.Embed.timestamp(embed, timestamp)
%{timestamp: "2023-01-01T12:00:00Z"}
Sets the title of an embed.
Parameters
embed
: The embed to modifytitle
: The title text (up to 256 characters)
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.title(embed, "Hello, World!")
%{title: "Hello, World!"}
Sets the URL of an embed.
Parameters
embed
: The embed to modifyurl
: The URL for the embed title to link to
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.url(embed, "https://example.com")
%{url: "https://example.com"}
Sets the video of an embed.
Parameters
embed
: The embed to modifyurl
: URL of the videoopts
: Optional parameters (proxy_url, height, width)
Examples
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.video(embed, "https://example.com/video.mp4")
%{video: %{url: "https://example.com/video.mp4"}}
iex> embed = DiscordInteractions.Embed.new()
iex> DiscordInteractions.Embed.video(embed, "https://example.com/video.mp4", height: 720, width: 1280)
%{video: %{url: "https://example.com/video.mp4", height: 720, width: 1280}}