Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Guild): add Guild#fetchBan() #3170

Merged
merged 6 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/client/rest/RESTMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
27 changes: 25 additions & 2 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<BanInfo>}
* @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<Collection<Snowflake, User>>}
* @returns {Promise<Collection<Snowflake, User|BanInfo>>}
* @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();
Expand Down
8 changes: 7 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ declare module 'discord.js' {
public edit(data: GuildEditData, reason?: string): Promise<Guild>;
public equals(guild: Guild): boolean;
public fetchAuditLogs(options?: GuildAuditLogsFetchOptions): Promise<GuildAuditLogs>;
public fetchBans(): Promise<Collection<Snowflake, User>>;
public fetchBan(user: UserResolvable): Promise<BanInfo>;
public fetchBans(withReasons?: boolean): Promise<Collection<Snowflake, User | BanInfo>>;
izexi marked this conversation as resolved.
Show resolved Hide resolved
public fetchEmbed(): Promise<GuildEmbedData>;
public fetchInvites(): Promise<Collection<Snowflake, Invite>>;
public fetchMember(user: UserResolvable, cache?: boolean): Promise<GuildMember>;
Expand Down Expand Up @@ -1600,6 +1601,11 @@ declare module 'discord.js' {

type AwaitReactionsOptions = ReactionCollectorOptions & { errors?: string[] };

type BanInfo = {
user: User;
reason?: string;
izexi marked this conversation as resolved.
Show resolved Hide resolved
};

type BanOptions = {
days?: number;
reason?: string;
Expand Down