feat: soundboard forward port (#10859)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Danial Raza
2025-05-09 09:43:40 +02:00
committed by GitHub
parent f6da9495ed
commit c4cd6ea637
5 changed files with 60 additions and 25 deletions

View File

@@ -285,8 +285,8 @@ class GuildManager extends CachedManager {
/**
* @typedef {Object} FetchSoundboardSoundsOptions
* @param {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 {Snowflake[]} guildIds The ids of the guilds to fetch soundboard sounds for
* @property {number} [time=10_000] The timeout for receipt of the soundboard sounds
*/
/**

View File

@@ -105,7 +105,7 @@ class GuildSoundboardSoundManager extends CachedManager {
* Data for editing a soundboard sound.
* @typedef {Object} GuildSoundboardSoundEditOptions
* @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 {?string} [emojiName] The emoji name of 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 });
}
/**
* 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.
* @param {Snowflake} [id] The soundboard sound's id
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @param {SoundboardSoundResolvable|FetchSoundboardSoundOptions|FetchGuildSoundboardSoundsOptions} [options] Options for fetching soundboard sound(s)
* @returns {Promise<SoundboardSound|Collection<Snowflake, SoundboardSound>>}
* @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
* guild.soundboardSounds.fetch('222078108977594368')
* .then(sound => console.log(`The soundboard sound name is: ${sound.name}`))
* .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 } = {}) {
if (id) {
if (!force) {
const existing = this.cache.get(id);
if (existing) return existing;
}
/* eslint-enable max-len */
async fetch(options) {
if (!options) return this._fetchMany();
const { cache, force, soundboardSound } = options;
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));
return this._add(sound, cache);
async _fetchSingle({ cache, force, soundboardSound } = {}) {
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));
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());
}
}

View File

@@ -489,6 +489,13 @@ class Guild extends AnonymousGuild {
} else {
this.incidentsData ??= null;
}
if (data.soundboard_sounds) {
this.soundboardSounds.cache.clear();
for (const soundboardSound of data.soundboard_sounds) {
this.soundboardSounds._add(soundboardSound);
}
}
}
/**

View File

@@ -181,8 +181,8 @@ class SoundboardSound extends Base {
this.available === other.available &&
this.name === other.name &&
this.volume === other.volume &&
this.emojiId === other.emojiId &&
this.emojiName === other.emojiName &&
this._emoji?.id === other._emoji?.id &&
this._emoji?.name === other._emoji?.name &&
this.guildId === other.guildId &&
this.user?.id === other.user?.id
);
@@ -193,8 +193,8 @@ class SoundboardSound extends Base {
this.available === other.available &&
this.name === other.name &&
this.volume === other.volume &&
this.emojiId === other.emoji_id &&
this.emojiName === other.emoji_name &&
(this._emoji?.id ?? null) === other.emoji_id &&
(this._emoji?.name ?? null) === other.emoji_name &&
this.guildId === other.guild_id &&
this.user?.id === other.user?.id
);

View File

@@ -4290,8 +4290,15 @@ export interface GuildSoundboardSoundEditOptions {
volume?: number | null;
emojiId?: Snowflake | 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> {
private constructor(guild: Guild, iterable?: Iterable<APISoundboardSound>);
public guild: Guild;
@@ -5804,8 +5811,7 @@ export interface GuildAuditLogsEntryTargetField<TAction extends AuditLogEvent> {
ApplicationCommand: ApplicationCommand | { id: Snowflake };
AutoModeration: AutoModerationRule;
GuildOnboardingPrompt: GuildOnboardingPrompt | { id: Snowflake; [x: string]: unknown };
// TODO: Update when https://github.com/discordjs/discord.js/pull/10590 is merged
SoundboardSound: { id: Snowflake };
SoundboardSound: SoundboardSound | { id: Snowflake };
}
export interface GuildAuditLogsFetchOptions<Event extends GuildAuditLogsResolvable> {