Skip to content

Commit

Permalink
Continuation from #110: Add get_guild_ban and fix typo, cast into str…
Browse files Browse the repository at this point in the history
…uct. (#224)

* Add get_guild_ban and fix typo.

* Decode into `Guild.Ban` struct.

Co-authored-by: Juan <[email protected]>
  • Loading branch information
jchristgit and Xh4H authored May 14, 2021
1 parent 3a22f2b commit 2adbb30
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/nostrum/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,16 @@ defmodule Nostrum.Api do
end

@doc """
Gets a list of users banend from a guild.
Gets a ban object for the given user from a guild.
"""
@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({:struct, Guild.Ban})
end

@doc """
Gets a list of users banned from a guild.
Guild to get bans for is specified by `guild_id`.
"""
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 2adbb30

Please sign in to comment.