Crux v2020-03-17 17:55:51Z Crux.Rest.CDN View Source

Functions to generate cdn urls pointing to avatars, icons, etc.

Link to this section Summary

Types

Specifies the file type / extension and size of the resource url to generate.

Functions

Base CDN address.

Generates the url to an emoji.

Generates a url to the default avatar url of a user.

Generates a url to group dm channel icon.

Generates a url to a guild banner.

Generates a url to a guild icon.

Generates a url to a guild splash.

Generates a url to a user.

Link to this section Types

Link to this type

format_options() View Source (since 0.1.5)
format_options() ::
  %{optional(:size) => pos_integer(), optional(:extension) => String.t()}
  | [size: pos_integer(), extension: String.t()]

Specifies the file type / extension and size of the resource url to generate.

Notes:

  • :size has to be any power of two between 16 and 2048
  • :extension has to be one of "jpg", "jpeg", "png", "webp", "gif"
  • See the docs of each function for information what sizes / extensions are valid there.

Link to this section Functions

Link to this function

base_url() View Source (since 0.1.5)
base_url() :: String.t()

Base CDN address.

Link to this function

custom_emoji(emoji) View Source (since 0.1.5)
custom_emoji(Crux.Structs.Emoji.t() | %{id: Crux.Rest.snowflake()}) ::
  String.t()

Generates the url to an emoji.

This function does not accept any format_options/0.

# A struct
iex> %Crux.Structs.Emoji{id: 438226248293154816, animated: false}
...> |> Crux.Rest.CDN.custom_emoji()
"https://cdn.discordapp.com/emojis/438226248293154816.png"

# A plain map
iex> %{id: 438226248293154816, animated: true}
...> |> Crux.Rest.CDN.custom_emoji()
"https://cdn.discordapp.com/emojis/438226248293154816.gif"
Link to this function

default_user_avatar(user) View Source (since 0.1.5)
default_user_avatar(Crux.Structs.User.t() | %{discriminator: String.t()}) ::
  String.t()

Generates a url to the default avatar url of a user.

# A struct
iex> %Crux.Structs.User{discriminator: "0001"}
...> |> Crux.Rest.CDN.default_user_avatar()
"https://cdn.discordapp.com/embed/avatars/1.png"

# A plain map
iex> %{discriminator: "0001"}
...> |> Crux.Rest.CDN.default_user_avatar()
"https://cdn.discordapp.com/embed/avatars/1.png"
Link to this function

group_dm_icon(group_dm, options \\ []) View Source (since 0.1.5)
group_dm_icon(
  Crux.Structs.Channel.t()
  | %{id: Crux.Rest.snowflake(), icon: String.t() | nil},
  format_options()
) :: String.t() | nil

Generates a url to group dm channel icon.

If the group dm channel has no icon nil will be returned.

# A struct
iex> %Crux.Structs.Channel{id: 354042501201526786, icon: "ecd7839b9eed535f1ae3a545c5d5f3c8"}
...> |> Crux.Rest.CDN.group_dm_icon()
"https://cdn.discordapp.com/channel-icons/354042501201526786/ecd7839b9eed535f1ae3a545c5d5f3c8.webp"

# A plain map
iex> %{id: 354042501201526786, icon: "ecd7839b9eed535f1ae3a545c5d5f3c8"}
...> |> Crux.Rest.CDN.group_dm_icon()
"https://cdn.discordapp.com/channel-icons/354042501201526786/ecd7839b9eed535f1ae3a545c5d5f3c8.webp"

  # With format options
iex> %Crux.Structs.Channel{id: 354042501201526786, icon: "ecd7839b9eed535f1ae3a545c5d5f3c8"}
...> |> Crux.Rest.CDN.group_dm_icon(size: 16, extension: "png")
"https://cdn.discordapp.com/channel-icons/354042501201526786/ecd7839b9eed535f1ae3a545c5d5f3c8.png?size=16"

# Without icon
iex> %{icon: nil}
...> |> Crux.Rest.CDN.group_dm_icon()
nil
Link to this function

guild_banner(guild, options \\ []) View Source (since 0.2.0)
guild_banner(
  Crux.Structs.Guild.t()
  | %{id: Crux.Rest.snowflake(), banner: String.t() | nil},
  format_options()
) :: String.t() | nil

Generates a url to a guild banner.

The extension "gif" is not valid here.

# A struct
iex> %Crux.Structs.Guild{id: 269508806759809042, banner: "29c1980a3471cb2d5c1208c5196278fb"}
...> |> Crux.Rest.CDN.guild_banner()
"https://cdn.discordapp.com/banners/269508806759809042/29c1980a3471cb2d5c1208c5196278fb.webp"

  # A plain map
iex> %{id: 269508806759809042, banner: "29c1980a3471cb2d5c1208c5196278fb"}
...> |> Crux.Rest.CDN.guild_banner()
"https://cdn.discordapp.com/banners/269508806759809042/29c1980a3471cb2d5c1208c5196278fb.webp"

# With format_options
iex> %Crux.Structs.Guild{id: 269508806759809042, banner: "29c1980a3471cb2d5c1208c5196278fb"}
...> |> Crux.Rest.CDN.guild_banner(size: 16, extension: "png")
"https://cdn.discordapp.com/banners/269508806759809042/29c1980a3471cb2d5c1208c5196278fb.png?size=16"

# Without banner
iex> %Crux.Structs.Guild{id: 269508806759809042, banner: nil}
...> |> Crux.Rest.CDN.guild_banner()
nil
Link to this function

guild_icon(guild, options \\ []) View Source (since 0.1.5)
guild_icon(
  Crux.Structs.Guild.t() | %{id: Crux.Rest.snowflake(), icon: String.t() | nil},
  format_options()
) :: String.t() | nil

Generates a url to a guild icon.

If the guild has no icon nil will be returned.

The extension "gif" is not valid here.

# A guild struct
iex> %Crux.Structs.Guild{id: 269508806759809042, icon: "15abb45cf1c59f90ea291185b99ab1dd"}
...> |> Crux.Rest.CDN.guild_icon()
"https://cdn.discordapp.com/icons/269508806759809042/15abb45cf1c59f90ea291185b99ab1dd.webp"

# A plain map
iex> %{id: 269508806759809042, icon: "15abb45cf1c59f90ea291185b99ab1dd"}
...> |> Crux.Rest.CDN.guild_icon()
"https://cdn.discordapp.com/icons/269508806759809042/15abb45cf1c59f90ea291185b99ab1dd.webp"

# With format_options
iex> %Crux.Structs.Guild{id: 269508806759809042, icon: "15abb45cf1c59f90ea291185b99ab1dd"}
...> |> Crux.Rest.CDN.guild_icon(size: 16, extension: "png")
"https://cdn.discordapp.com/icons/269508806759809042/15abb45cf1c59f90ea291185b99ab1dd.png?size=16"

# Without icon
iex> %Crux.Structs.Guild{id: 269508806759809042, icon: nil}
...> |> Crux.Rest.CDN.guild_icon()
nil
Link to this function

guild_splash(guild, options \\ []) View Source (since 0.1.5)
guild_splash(
  Crux.Structs.Guild.t()
  | %{id: Crux.Rest.snowflake(), splash: String.t() | nil},
  format_options()
) :: String.t() | nil

Generates a url to a guild splash.

The extension "gif" is not valid here.

# A struct
iex> %Crux.Structs.Guild{id: 269508806759809042, splash: "15abb45cf1c59f90ea291185b99ab1dd"}
...> |> Crux.Rest.CDN.guild_splash()
"https://cdn.discordapp.com/splashes/269508806759809042/15abb45cf1c59f90ea291185b99ab1dd.webp"

  # A plain map
iex> %{id: 269508806759809042, splash: "15abb45cf1c59f90ea291185b99ab1dd"}
...> |> Crux.Rest.CDN.guild_splash()
"https://cdn.discordapp.com/splashes/269508806759809042/15abb45cf1c59f90ea291185b99ab1dd.webp"

# With format_options
iex> %Crux.Structs.Guild{id: 269508806759809042, splash: "15abb45cf1c59f90ea291185b99ab1dd"}
...> |> Crux.Rest.CDN.guild_splash(size: 16, extension: "png")
"https://cdn.discordapp.com/splashes/269508806759809042/15abb45cf1c59f90ea291185b99ab1dd.png?size=16"

# Without splash
iex> %Crux.Structs.Guild{id: 269508806759809042, splash: nil}
...> |> Crux.Rest.CDN.guild_splash()
nil
Link to this function

user_avatar(user, options \\ []) View Source (since 0.1.5)
user_avatar(
  Crux.Structs.User.t()
  | %{
      id: Crux.Rest.snowflake(),
      discriminator: String.t(),
      avatar: String.t() | nil
    },
  format_options()
) :: String.t()

Generates a url to a user.

If the user has no custom avatar this will return a default one with the extension "webp".

The extension defaults to "gif" or "webp" depending on whether the user has an animated avatar.

# A struct with an avatar
iex> %Crux.Structs.User{id: 218348062828003328, avatar: "646a356e237350bf8b8dfde15667dfc4"}
...> |> Crux.Rest.CDN.user_avatar()
"https://cdn.discordapp.com/avatars/218348062828003328/646a356e237350bf8b8dfde15667dfc4.webp"

# A plain map with an avatar
iex> %{id: 218348062828003328, avatar: "646a356e237350bf8b8dfde15667dfc4"}
...> |> Crux.Rest.CDN.user_avatar()
"https://cdn.discordapp.com/avatars/218348062828003328/646a356e237350bf8b8dfde15667dfc4.webp"

# With format options
iex> %Crux.Structs.User{id: 218348062828003328, avatar: "646a356e237350bf8b8dfde15667dfc4"}
...> |> Crux.Rest.CDN.user_avatar(extension: "png", size: 2048)
"https://cdn.discordapp.com/avatars/218348062828003328/646a356e237350bf8b8dfde15667dfc4.png?size=2048"

# A struct without an avatar
iex> %Crux.Structs.User{id: 218348062828003328, avatar: nil, discriminator: "0001"}
...> |> Crux.Rest.CDN.user_avatar()
"https://cdn.discordapp.com/embed/avatars/1.png"