mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
feat(GuildBanManager): Support pagination results (#7734)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { Collection } = require('@discordjs/collection');
|
const { Collection } = require('@discordjs/collection');
|
||||||
|
const { makeURLSearchParams } = require('@discordjs/rest');
|
||||||
const { Routes } = require('discord-api-types/v10');
|
const { Routes } = require('discord-api-types/v10');
|
||||||
const CachedManager = require('./CachedManager');
|
const CachedManager = require('./CachedManager');
|
||||||
const { TypeError, Error } = require('../errors');
|
const { TypeError, Error } = require('../errors');
|
||||||
@@ -55,9 +56,12 @@ 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
|
* @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
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,13 +69,13 @@ class GuildBanManager extends CachedManager {
|
|||||||
* @param {UserResolvable|FetchBanOptions|FetchBansOptions} [options] Options for fetching guild ban(s)
|
* @param {UserResolvable|FetchBanOptions|FetchBansOptions} [options] Options for fetching guild ban(s)
|
||||||
* @returns {Promise<GuildBan|Collection<Snowflake, GuildBan>>}
|
* @returns {Promise<GuildBan|Collection<Snowflake, GuildBan>>}
|
||||||
* @example
|
* @example
|
||||||
* // Fetch all bans from a guild
|
* // Fetch multiple bans from a guild
|
||||||
* guild.bans.fetch()
|
* guild.bans.fetch()
|
||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
* @example
|
* @example
|
||||||
* // Fetch all bans from a guild without caching
|
* // Fetch a maximum of 5 bans from a guild without caching
|
||||||
* guild.bans.fetch({ cache: false })
|
* guild.bans.fetch({ limit: 5, cache: false })
|
||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
* @example
|
* @example
|
||||||
@@ -92,14 +96,15 @@ class GuildBanManager extends CachedManager {
|
|||||||
*/
|
*/
|
||||||
fetch(options) {
|
fetch(options) {
|
||||||
if (!options) return this._fetchMany();
|
if (!options) return this._fetchMany();
|
||||||
const user = this.client.users.resolveId(options);
|
const { user, cache, force, limit, before, after } = options;
|
||||||
if (user) return this._fetchSingle({ user, cache: true });
|
const resolvedUser = this.client.users.resolveId(user ?? options);
|
||||||
options.user &&= this.client.users.resolveId(options.user);
|
if (resolvedUser) return this._fetchSingle({ user: resolvedUser, cache, force });
|
||||||
if (!options.user) {
|
|
||||||
if ('cache' in options) return this._fetchMany(options.cache);
|
if (!before && !after && !limit && typeof cache === 'undefined') {
|
||||||
return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID'));
|
return Promise.reject(new Error('FETCH_BAN_RESOLVE_ID'));
|
||||||
}
|
}
|
||||||
return this._fetchSingle(options);
|
|
||||||
|
return this._fetchMany(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _fetchSingle({ user, cache, force = false }) {
|
async _fetchSingle({ user, cache, force = false }) {
|
||||||
@@ -112,9 +117,12 @@ class GuildBanManager extends CachedManager {
|
|||||||
return this._add(data, cache);
|
return this._add(data, cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _fetchMany(cache) {
|
async _fetchMany(options = {}) {
|
||||||
const data = await this.client.rest.get(Routes.guildBans(this.guild.id));
|
const data = await this.client.rest.get(Routes.guildBans(this.guild.id), {
|
||||||
return data.reduce((col, ban) => col.set(ban.user.id, this._add(ban, cache)), new Collection());
|
query: makeURLSearchParams(options),
|
||||||
|
});
|
||||||
|
|
||||||
|
return data.reduce((col, ban) => col.set(ban.user.id, this._add(ban, options.cache)), new Collection());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
5
packages/discord.js/typings/index.d.ts
vendored
5
packages/discord.js/typings/index.d.ts
vendored
@@ -4262,7 +4262,10 @@ export interface FetchBanOptions extends BaseFetchOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface FetchBansOptions {
|
export interface FetchBansOptions {
|
||||||
cache: boolean;
|
limit?: number;
|
||||||
|
before?: Snowflake;
|
||||||
|
after?: Snowflake;
|
||||||
|
cache?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FetchChannelOptions extends BaseFetchOptions {
|
export interface FetchChannelOptions extends BaseFetchOptions {
|
||||||
|
|||||||
@@ -117,6 +117,8 @@ import {
|
|||||||
TextInputComponent,
|
TextInputComponent,
|
||||||
Embed,
|
Embed,
|
||||||
MessageActionRowComponentBuilder,
|
MessageActionRowComponentBuilder,
|
||||||
|
GuildBanManager,
|
||||||
|
GuildBan,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
||||||
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
|
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
|
||||||
@@ -1027,6 +1029,20 @@ expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch()
|
|||||||
expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch(undefined, {}));
|
expectType<Promise<Collection<Snowflake, GuildEmoji>>>(guildEmojiManager.fetch(undefined, {}));
|
||||||
expectType<Promise<GuildEmoji>>(guildEmojiManager.fetch('0'));
|
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;
|
declare const typing: Typing;
|
||||||
expectType<User | PartialUser>(typing.user);
|
expectType<User | PartialUser>(typing.user);
|
||||||
if (typing.user.partial) expectType<null>(typing.user.username);
|
if (typing.user.partial) expectType<null>(typing.user.username);
|
||||||
|
|||||||
Reference in New Issue
Block a user