Skip to content

Commit

Permalink
feat: add support for guild banners
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC committed Mar 27, 2019
1 parent 1be5ea1 commit c7ae875
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/rest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ defmodule Crux.Rest do
optional(:afk_timeout) => non_neg_integer(),
optional(:icon) => Util.image(),
optional(:splash) => Util.image(),
optional(:banner) => Util.image(),
optional(:owner_id) => Crux.Rest.snowflake(),
optional(:system_channel_id) => Crux.Rest.snowflake() | nil,
optional(:reason) => String.t()
Expand All @@ -735,6 +736,7 @@ defmodule Crux.Rest do
| {:afk_timeout, non_neg_integer()}
| {:icon, Util.image()}
| {:splash, Util.image()}
| {:banner, Util.image()}
| {:owner_id, Crux.Rest.snowflake()}
| {:system_channel_id, Crux.Rest.snowflake() | nil}
| {:reason, String.t()}
Expand Down
45 changes: 44 additions & 1 deletion lib/rest/cdn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ defmodule Crux.Rest.CDN do
@doc """
Generates a url to a guild splash.
The extension "gif" is valid here.
The extension "gif" is not valid here.
```elixir
# A struct
Expand Down Expand Up @@ -186,6 +186,49 @@ defmodule Crux.Rest.CDN do
"#{@base_url}/splashes/#{id}/#{splash}.#{extension}#{qs}"
end

@doc """
Generates a url to a guild banner.
The extension "gif" is not valid here.
```elixir
# A struct
iex> %Crux.Structs.Guild{id: 269508806759809042, banner: "29c1980a3471cb2d5c1208c5196278fb"}
...> |> Crux.Rest.CDN.guild_banner()
"#{@base_url}/banners/269508806759809042/29c1980a3471cb2d5c1208c5196278fb.webp"
# A plain map
iex> %{id: 269508806759809042, banner: "29c1980a3471cb2d5c1208c5196278fb"}
...> |> Crux.Rest.CDN.guild_banner()
"#{@base_url}/banners/269508806759809042/29c1980a3471cb2d5c1208c5196278fb.webp"
# With format_options
iex> %Crux.Structs.Guild{id: 269508806759809042, banner: "29c1980a3471cb2d5c1208c5196278fb"}
...> |> Crux.Rest.CDN.guild_banner(size: 16, extension: "png")
"#{@base_url}/banners/269508806759809042/29c1980a3471cb2d5c1208c5196278fb.png?size=16"
# Without banner
iex> %Crux.Structs.Guild{id: 269508806759809042, banner: nil}
...> |> Crux.Rest.CDN.guild_banner()
nil
```
"""
@spec guild_banner(
Crux.Structs.Guild.t() | %{id: Crux.Rest.snowflake(), banner: String.t() | nil},
format_options()
) :: String.t() | nil
Version.since("0.2.0")
def guild_banner(guild, options \\ [])
def guild_banner(%{banner: nil}, _options), do: nil

def guild_banner(%{id: id, banner: banner}, options) do
extension = options[:extension] || "webp"
qs = if options[:size], do: "?size=#{options[:size]}", else: ""

"#{@base_url}/banners/#{id}/#{banner}.#{extension}#{qs}"
end

@doc """
Generates a url to the default avatar url of a user.
Expand Down
1 change: 1 addition & 0 deletions lib/rest/functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ defmodule Crux.Rest.Functions do
|> Map.new()
|> Util.resolve_image_in_map(:icon)
|> Util.resolve_image_in_map(:splash)
|> Util.resolve_image_in_map(:banner)

Request.new(:patch, path, data)
|> Request.set_transform(Guild)
Expand Down

0 comments on commit c7ae875

Please sign in to comment.