Skip to content

Commit

Permalink
Decode into Guild.Ban struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristgit committed Apr 18, 2021
1 parent 21ba9e6 commit 184047f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/nostrum/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1867,12 +1867,12 @@ defmodule Nostrum.Api do
@doc """
Gets a ban object for the given user from a guild.
"""
@spec get_guild_ban(integer, integer) :: error | {:ok, Nostrum.Struct.User.t()}
@spec get_guild_ban(integer, integer) :: error | {:ok, Guild.Ban.t()}
def get_guild_ban(guild_id, user_id) do
request(:get, Constants.guild_ban(guild_id, user_id))
|> handle_request_with_decode
|> handle_request_with_decode({:struct, Guild.Ban})
end

@doc """
Gets a list of users banned from a guild.
Expand Down
34 changes: 34 additions & 0 deletions lib/nostrum/struct/guild/ban.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Nostrum.Struct.Guild.Ban do
@moduledoc """
Represents a guild ban.
"""

alias Nostrum.Struct.User
alias Nostrum.Util

defstruct [
:reason,
:user
]

@typedoc "The reason for the ban"
@type reason :: String.t() | nil

@typedoc "The banned user"
@type user :: User.t()

@type t :: %__MODULE__{
reason: reason,
user: user
}

@doc false
def to_struct(map) do
new =
map
|> Map.new(fn {k, v} -> {Util.maybe_to_atom(k), v} end)
|> Map.update(:user, nil, &Util.cast(&1, {:struct, User}))

struct(__MODULE__, new)
end
end

0 comments on commit 184047f

Please sign in to comment.