Skip to content

Commit 92776b0

Browse files
Add support for the GET Guild Ban endpoint (#3362)
This adds supports for the GET `/guilds/{guild.id}/bans/{user.id}` endpoint, allowing one to get a single `model::guild::Ban` for a specific user in a guild. Discord Documentation: https://discord.com/developers/docs/resources/guild#get-guild-ban Closes #3341
1 parent d1ab1f1 commit 92776b0

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

src/http/client.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,40 @@ impl Http {
27942794
.await
27952795
}
27962796

2797+
/// Gets a [`Ban`] for a specific user in a guild. Returns [`None`] if no ban was found
2798+
/// matching both the [`GuildId`] and [`UserId`].
2799+
///
2800+
/// **Note**: Requires that you have the [Ban Members] permission
2801+
///
2802+
/// # Errors
2803+
///
2804+
/// Returns [`Error::Http`] if the current user lacks permission.
2805+
///
2806+
/// [Ban Members]: Permissions::BAN_MEMBERS
2807+
pub async fn get_ban(&self, guild_id: GuildId, user_id: UserId) -> Result<Option<Ban>> {
2808+
let result = self
2809+
.fire(Request {
2810+
body: None,
2811+
multipart: None,
2812+
headers: None,
2813+
method: LightMethod::Get,
2814+
route: Route::GuildBan {
2815+
guild_id,
2816+
user_id,
2817+
},
2818+
params: None,
2819+
})
2820+
.await;
2821+
2822+
match result {
2823+
Ok(ban) => Ok(Some(ban)),
2824+
Err(Error::Http(ref err)) if err.status_code() == Some(StatusCode::NOT_FOUND) => {
2825+
Ok(None)
2826+
},
2827+
Err(e) => Err(e),
2828+
}
2829+
}
2830+
27972831
/// Gets all audit logs in a specific guild.
27982832
pub async fn get_audit_logs(
27992833
&self,

src/model/guild/guild_id.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ impl GuildId {
300300
http.as_ref().get_bans(self, target, limit).await
301301
}
302302

303+
/// Gets a user's ban from the guild.
304+
/// See [`Http::get_ban`] for details.
305+
///
306+
/// **Note**: Requires the [Ban Members] permission.
307+
///
308+
/// # Errors
309+
///
310+
/// Returns [`Error::Http`] if the current user lacks permission.
311+
///
312+
/// [Ban Members]: Permissions::BAN_MEMBERS
313+
#[inline]
314+
pub async fn get_ban(self, http: impl AsRef<Http>, user_id: UserId) -> Result<Option<Ban>> {
315+
http.as_ref().get_ban(self, user_id).await
316+
}
317+
303318
/// Gets a list of the guild's audit log entries
304319
///
305320
/// **Note**: Requires the [View Audit Log] permission.

src/model/guild/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,32 @@ impl Guild {
587587
self.id.bans(cache_http.http(), target, limit).await
588588
}
589589

590+
/// Gets a user's ban from the guild.
591+
/// See [`Http::get_bans`] for details.
592+
///
593+
/// **Note**: Requires the [Ban Members] permission.
594+
///
595+
/// # Errors
596+
///
597+
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
598+
/// does not have permission to perform bans.
599+
///
600+
/// [Ban Members]: Permissions::BAN_MEMBERS
601+
pub async fn get_ban(
602+
&self,
603+
cache_http: impl CacheHttp,
604+
user_id: UserId,
605+
) -> Result<Option<Ban>> {
606+
#[cfg(feature = "cache")]
607+
{
608+
if let Some(cache) = cache_http.cache() {
609+
self.require_perms(cache, Permissions::BAN_MEMBERS)?;
610+
}
611+
}
612+
613+
self.id.get_ban(cache_http.http(), user_id).await
614+
}
615+
590616
/// Adds a [`User`] to this guild with a valid OAuth2 access token.
591617
///
592618
/// Returns the created [`Member`] object, or nothing if the user is already a member of the

src/model/guild/partial_guild.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ impl PartialGuild {
340340
self.id.bans(http, target, limit).await
341341
}
342342

343+
/// Gets a user's ban from the guild.
344+
/// See [`Http::get_bans`] for details.
345+
///
346+
/// Requires the [Ban Members] permission.
347+
///
348+
/// # Errors
349+
///
350+
/// Returns [`Error::Http`] if the current user lacks permission.
351+
///
352+
/// [Ban Members]: Permissions::BAN_MEMBERS
353+
#[inline]
354+
pub async fn get_ban(&self, http: impl AsRef<Http>, user_id: UserId) -> Result<Option<Ban>> {
355+
self.id.get_ban(http, user_id).await
356+
}
357+
343358
/// Gets a list of the guild's audit log entries
344359
///
345360
/// **Note**: Requires the [View Audit Log] permission.

0 commit comments

Comments
 (0)