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(GuildBanManager): Support pagination results (v13) #7776

Merged
merged 1 commit into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/managers/GuildBanManager.js
Expand Up @@ -54,23 +54,26 @@ class GuildBanManager extends CachedManager {
*/

/**
* Options used to fetch all bans from a guild.
* Options used to fetch multiple bans from a guild.
* @typedef {Object} FetchBansOptions
* @property {boolean} cache Whether or not to cache the fetched bans
* @property {number} [limit] The maximum number of bans to return
* @property {Snowflake} [before] Consider only bans before this id
* @property {Snowflake} [after] Consider only bans after this id
* @property {boolean} [cache] Whether to cache the fetched bans
*/

/**
* Fetches ban(s) from Discord.
* @param {UserResolvable|FetchBanOptions|FetchBansOptions} [options] Options for fetching guild ban(s)
* @returns {Promise<GuildBan|Collection<Snowflake, GuildBan>>}
* @example
* // Fetch all bans from a guild
* // Fetch multiple bans from a guild
* guild.bans.fetch()
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch all bans from a guild without caching
* guild.bans.fetch({ cache: false })
* // Fetch a maximum of 5 bans from a guild without caching
* guild.bans.fetch({ limit: 5, cache: false })
* .then(console.log)
* .catch(console.error);
* @example
Expand All @@ -91,14 +94,15 @@ class GuildBanManager extends CachedManager {
*/
fetch(options) {
if (!options) return this._fetchMany();
const user = this.client.users.resolveId(options);
if (user) return this._fetchSingle({ user, cache: true });
options.user &&= this.client.users.resolveId(options.user);
if (!options.user) {
if ('cache' in options) return this._fetchMany(options.cache);
const { user, cache, force, limit, before, after } = options;
const resolvedUser = this.client.users.resolveId(user ?? options);
if (resolvedUser) return this._fetchSingle({ user: resolvedUser, cache, force });

if (!before && !after && !limit && typeof cache === 'undefined') {
return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID'));
}
return this._fetchSingle(options);

return this._fetchMany(options);
}

async _fetchSingle({ user, cache, force = false }) {
Expand All @@ -111,11 +115,13 @@ class GuildBanManager extends CachedManager {
return this._add(data, cache);
}

async _fetchMany(cache) {
const data = await this.client.api.guilds(this.guild.id).bans.get();
return data.reduce((col, ban) => col.set(ban.user.id, this._add(ban, cache)), new Collection());
}
async _fetchMany(options = {}) {
const data = await this.client.api.guilds(this.guild.id).bans.get({
query: options,
});

return data.reduce((col, ban) => col.set(ban.user.id, this._add(ban, options.cache)), new Collection());
}
/**
* Options used to ban a user from a guild.
* @typedef {Object} BanOptions
Expand Down
5 changes: 4 additions & 1 deletion typings/index.d.ts
Expand Up @@ -4554,7 +4554,10 @@ export interface FetchBanOptions extends BaseFetchOptions {
}

export interface FetchBansOptions {
cache: boolean;
limit?: number;
before?: Snowflake;
after?: Snowflake;
cache?: boolean;
}

export interface FetchChannelOptions extends BaseFetchOptions {
Expand Down
16 changes: 16 additions & 0 deletions typings/index.test-d.ts
Expand Up @@ -94,6 +94,8 @@ import {
MessageSelectMenu,
PartialDMChannel,
InteractionResponseFields,
GuildBan,
GuildBanManager,
} from '.';
import type { ApplicationCommandOptionTypes } from './enums';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
Expand Down Expand Up @@ -891,6 +893,20 @@ expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch()
expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch(undefined, {}));
expectType<Promise<GuildEmoji>>(guildEmojiManager.fetch('0'));

declare const guildBanManager: GuildBanManager;
{
expectType<Promise<GuildBan>>(guildBanManager.fetch('1234567890'));
expectType<Promise<GuildBan>>(guildBanManager.fetch({ user: '1234567890' }));
expectType<Promise<GuildBan>>(guildBanManager.fetch({ user: '1234567890', cache: true, force: false }));
expectType<Promise<Collection<Snowflake, GuildBan>>>(guildBanManager.fetch());
expectType<Promise<Collection<Snowflake, GuildBan>>>(guildBanManager.fetch({}));
expectType<Promise<Collection<Snowflake, GuildBan>>>(guildBanManager.fetch({ limit: 100, before: '1234567890' }));
// @ts-expect-error
guildBanManager.fetch({ cache: true, force: false });
// @ts-expect-error
guildBanManager.fetch({ user: '1234567890', after: '1234567890', cache: true, force: false });
}

declare const typing: Typing;
expectType<PartialUser>(typing.user);
if (typing.user.partial) expectType<null>(typing.user.username);
Expand Down