mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: soundboard forward port (#10859)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -285,8 +285,8 @@ class GuildManager extends CachedManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} FetchSoundboardSoundsOptions
|
* @typedef {Object} FetchSoundboardSoundsOptions
|
||||||
* @param {Snowflake[]} guildIds The ids of the guilds to fetch soundboard sounds for
|
* @property {Snowflake[]} guildIds The ids of the guilds to fetch soundboard sounds for
|
||||||
* @param {number} [time=10_000] The timeout for receipt of the soundboard sounds
|
* @property {number} [time=10_000] The timeout for receipt of the soundboard sounds
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class GuildSoundboardSoundManager extends CachedManager {
|
|||||||
* Data for editing a soundboard sound.
|
* Data for editing a soundboard sound.
|
||||||
* @typedef {Object} GuildSoundboardSoundEditOptions
|
* @typedef {Object} GuildSoundboardSoundEditOptions
|
||||||
* @property {string} [name] The name of the soundboard sound
|
* @property {string} [name] The name of the soundboard sound
|
||||||
* @property {?number} [volume] The volume of the soundboard sound, from 0 to 1
|
* @property {?number} [volume] The volume (a double) of the soundboard sound, from 0 (inclusive) to 1
|
||||||
* @property {?Snowflake} [emojiId] The emoji id of the soundboard sound
|
* @property {?Snowflake} [emojiId] The emoji id of the soundboard sound
|
||||||
* @property {?string} [emojiName] The emoji name of the soundboard sound
|
* @property {?string} [emojiName] The emoji name of the soundboard sound
|
||||||
* @property {string} [reason] The reason for editing the soundboard sound
|
* @property {string} [reason] The reason for editing the soundboard sound
|
||||||
@@ -157,35 +157,57 @@ class GuildSoundboardSoundManager extends CachedManager {
|
|||||||
await this.client.rest.delete(Routes.guildSoundboardSound(this.guild.id, soundId), { reason });
|
await this.client.rest.delete(Routes.guildSoundboardSound(this.guild.id, soundId), { reason });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options used to fetch a soundboard sound.
|
||||||
|
* @typedef {BaseFetchOptions} FetchSoundboardSoundOptions
|
||||||
|
* @property {SoundboardSoundResolvable} soundboardSound The soundboard sound to fetch
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options used to fetch soundboard sounds from Discord
|
||||||
|
* @typedef {Object} FetchGuildSoundboardSoundsOptions
|
||||||
|
* @property {boolean} [cache] Whether to cache the fetched soundboard sounds
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* eslint-disable max-len */
|
||||||
/**
|
/**
|
||||||
* Obtains one or more soundboard sounds from Discord, or the soundboard sound cache if they're already available.
|
* Obtains one or more soundboard sounds from Discord, or the soundboard sound cache if they're already available.
|
||||||
* @param {Snowflake} [id] The soundboard sound's id
|
* @param {SoundboardSoundResolvable|FetchSoundboardSoundOptions|FetchGuildSoundboardSoundsOptions} [options] Options for fetching soundboard sound(s)
|
||||||
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
||||||
* @returns {Promise<SoundboardSound|Collection<Snowflake, SoundboardSound>>}
|
* @returns {Promise<SoundboardSound|Collection<Snowflake, SoundboardSound>>}
|
||||||
* @example
|
* @example
|
||||||
* // Fetch all soundboard sounds from the guild
|
|
||||||
* guild.soundboardSounds.fetch()
|
|
||||||
* .then(sounds => console.log(`There are ${sounds.size} soundboard sounds.`))
|
|
||||||
* .catch(console.error);
|
|
||||||
* @example
|
|
||||||
* // Fetch a single soundboard sound
|
* // Fetch a single soundboard sound
|
||||||
* guild.soundboardSounds.fetch('222078108977594368')
|
* guild.soundboardSounds.fetch('222078108977594368')
|
||||||
* .then(sound => console.log(`The soundboard sound name is: ${sound.name}`))
|
* .then(sound => console.log(`The soundboard sound name is: ${sound.name}`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
|
* @example
|
||||||
|
* // Fetch all soundboard sounds from the guild
|
||||||
|
* guild.soundboardSounds.fetch()
|
||||||
|
* .then(sounds => console.log(`There are ${sounds.size} soundboard sounds.`))
|
||||||
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async fetch(id, { cache = true, force = false } = {}) {
|
/* eslint-enable max-len */
|
||||||
if (id) {
|
async fetch(options) {
|
||||||
if (!force) {
|
if (!options) return this._fetchMany();
|
||||||
const existing = this.cache.get(id);
|
const { cache, force, soundboardSound } = options;
|
||||||
if (existing) return existing;
|
const resolvedSoundboardSound = this.resolveId(soundboardSound ?? options);
|
||||||
}
|
if (resolvedSoundboardSound) return this._fetchSingle({ cache, force, soundboardSound: resolvedSoundboardSound });
|
||||||
|
return this._fetchMany({ cache });
|
||||||
|
}
|
||||||
|
|
||||||
const sound = await this.client.rest.get(Routes.guildSoundboardSound(this.guild.id, id));
|
async _fetchSingle({ cache, force, soundboardSound } = {}) {
|
||||||
return this._add(sound, cache);
|
if (!force) {
|
||||||
|
const existing = this.cache.get(soundboardSound);
|
||||||
|
if (existing) return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = await this.client.rest.get(Routes.guildSoundboardSound(this.guild.id, soundboardSound));
|
||||||
|
return this._add(data, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
async _fetchMany({ cache } = {}) {
|
||||||
const data = await this.client.rest.get(Routes.guildSoundboardSounds(this.guild.id));
|
const data = await this.client.rest.get(Routes.guildSoundboardSounds(this.guild.id));
|
||||||
return new Collection(data.map(sound => [sound.sound_id, this._add(sound, cache)]));
|
|
||||||
|
return data.items.reduce((coll, sound) => coll.set(sound.sound_id, this._add(sound, cache)), new Collection());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -489,6 +489,13 @@ class Guild extends AnonymousGuild {
|
|||||||
} else {
|
} else {
|
||||||
this.incidentsData ??= null;
|
this.incidentsData ??= null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.soundboard_sounds) {
|
||||||
|
this.soundboardSounds.cache.clear();
|
||||||
|
for (const soundboardSound of data.soundboard_sounds) {
|
||||||
|
this.soundboardSounds._add(soundboardSound);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -181,8 +181,8 @@ class SoundboardSound extends Base {
|
|||||||
this.available === other.available &&
|
this.available === other.available &&
|
||||||
this.name === other.name &&
|
this.name === other.name &&
|
||||||
this.volume === other.volume &&
|
this.volume === other.volume &&
|
||||||
this.emojiId === other.emojiId &&
|
this._emoji?.id === other._emoji?.id &&
|
||||||
this.emojiName === other.emojiName &&
|
this._emoji?.name === other._emoji?.name &&
|
||||||
this.guildId === other.guildId &&
|
this.guildId === other.guildId &&
|
||||||
this.user?.id === other.user?.id
|
this.user?.id === other.user?.id
|
||||||
);
|
);
|
||||||
@@ -193,8 +193,8 @@ class SoundboardSound extends Base {
|
|||||||
this.available === other.available &&
|
this.available === other.available &&
|
||||||
this.name === other.name &&
|
this.name === other.name &&
|
||||||
this.volume === other.volume &&
|
this.volume === other.volume &&
|
||||||
this.emojiId === other.emoji_id &&
|
(this._emoji?.id ?? null) === other.emoji_id &&
|
||||||
this.emojiName === other.emoji_name &&
|
(this._emoji?.name ?? null) === other.emoji_name &&
|
||||||
this.guildId === other.guild_id &&
|
this.guildId === other.guild_id &&
|
||||||
this.user?.id === other.user?.id
|
this.user?.id === other.user?.id
|
||||||
);
|
);
|
||||||
|
|||||||
10
packages/discord.js/typings/index.d.ts
vendored
10
packages/discord.js/typings/index.d.ts
vendored
@@ -4290,8 +4290,15 @@ export interface GuildSoundboardSoundEditOptions {
|
|||||||
volume?: number | null;
|
volume?: number | null;
|
||||||
emojiId?: Snowflake | null;
|
emojiId?: Snowflake | null;
|
||||||
emojiName?: string | null;
|
emojiName?: string | null;
|
||||||
|
reason?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FetchGuildSoundboardSoundOptions extends BaseFetchOptions {
|
||||||
|
soundboardSound: SoundboardSoundResolvable;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FetchGuildSoundboardSoundsOptions extends Pick<BaseFetchOptions, 'cache'> {}
|
||||||
|
|
||||||
export class GuildSoundboardSoundManager extends CachedManager<Snowflake, SoundboardSound, SoundboardSoundResolvable> {
|
export class GuildSoundboardSoundManager extends CachedManager<Snowflake, SoundboardSound, SoundboardSoundResolvable> {
|
||||||
private constructor(guild: Guild, iterable?: Iterable<APISoundboardSound>);
|
private constructor(guild: Guild, iterable?: Iterable<APISoundboardSound>);
|
||||||
public guild: Guild;
|
public guild: Guild;
|
||||||
@@ -5804,8 +5811,7 @@ export interface GuildAuditLogsEntryTargetField<TAction extends AuditLogEvent> {
|
|||||||
ApplicationCommand: ApplicationCommand | { id: Snowflake };
|
ApplicationCommand: ApplicationCommand | { id: Snowflake };
|
||||||
AutoModeration: AutoModerationRule;
|
AutoModeration: AutoModerationRule;
|
||||||
GuildOnboardingPrompt: GuildOnboardingPrompt | { id: Snowflake; [x: string]: unknown };
|
GuildOnboardingPrompt: GuildOnboardingPrompt | { id: Snowflake; [x: string]: unknown };
|
||||||
// TODO: Update when https://github.com/discordjs/discord.js/pull/10590 is merged
|
SoundboardSound: SoundboardSound | { id: Snowflake };
|
||||||
SoundboardSound: { id: Snowflake };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GuildAuditLogsFetchOptions<Event extends GuildAuditLogsResolvable> {
|
export interface GuildAuditLogsFetchOptions<Event extends GuildAuditLogsResolvable> {
|
||||||
|
|||||||
Reference in New Issue
Block a user