diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 14484a7e9..457cb103c 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -631,6 +631,14 @@ class RESTMethods { }); } + getGuildBan(guild, user) { + const id = this.client.resolver.resolveUserID(user); + return this.rest.makeRequest('get', `${Endpoints.Guild(guild).bans}/${id}`, true).then(ban => ({ + reason: ban.reason, + user: this.client.dataManager.newUser(ban.user), + })); + } + getGuildBans(guild) { return this.rest.makeRequest('get', Endpoints.Guild(guild).bans, true).then(bans => bans.reduce((collection, ban) => { diff --git a/src/structures/Guild.js b/src/structures/Guild.js index c8e73db92..ca4055741 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -468,16 +468,39 @@ class Guild { return this.client.resolver.resolveGuildMember(this, user); } + /** + * An object containing information about a guild member's ban. + * @typedef {Object} BanInfo + * @property {User} user User that was banned + * @property {?string} reason Reason the user was banned + */ + + /** + * Fetch a ban for a user. + * @returns {Promise} + * @param {UserResolvable} user The user to fetch the ban for + * @example + * // Get ban + * guild.fetchBan(message.author) + * .then(({ user, reason }) => console.log(`${user.tag} was banned for the reason: ${reason}.`)) + * .catch(console.error); + */ + fetchBan(user) { + return this.client.rest.methods.getGuildBan(this, user); + } + /** * Fetch a collection of banned users in this guild. - * @returns {Promise>} + * @returns {Promise>} + * @param {boolean} [withReasons=false] Whether or not to include the ban reason(s) * @example * // Fetch bans in guild * guild.fetchBans() * .then(bans => console.log(`This guild has ${bans.size} bans`)) * .catch(console.error); */ - fetchBans() { + fetchBans(withReasons = false) { + if (withReasons) return this.client.rest.methods.getGuildBans(this); return this.client.rest.methods.getGuildBans(this) .then(bans => { const users = new Collection(); diff --git a/typings/index.d.ts b/typings/index.d.ts index adde1e148..8950d3f0e 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -533,7 +533,10 @@ declare module 'discord.js' { public edit(data: GuildEditData, reason?: string): Promise; public equals(guild: Guild): boolean; public fetchAuditLogs(options?: GuildAuditLogsFetchOptions): Promise; - public fetchBans(): Promise>; + public fetchBan(user: UserResolvable): Promise; + public fetchBans(withReasons?: false): Promise>; + public fetchBans(withReasons: true): Promise>; + public fetchBans(withReasons: boolean): Promise>; public fetchEmbed(): Promise; public fetchInvites(): Promise>; public fetchMember(user: UserResolvable, cache?: boolean): Promise; @@ -1600,6 +1603,11 @@ declare module 'discord.js' { type AwaitReactionsOptions = ReactionCollectorOptions & { errors?: string[] }; + type BanInfo = { + user: User; + reason: string | null; + }; + type BanOptions = { days?: number; reason?: string;