Skip to content

Commit 2d817df

Browse files
authored
feat: soundboard missing things (#10850)
1 parent 1605a2c commit 2d817df

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

packages/discord.js/src/managers/GuildSoundboardSoundManager.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,35 +157,57 @@ class GuildSoundboardSoundManager extends CachedManager {
157157
await this.client.rest.delete(Routes.guildSoundboardSound(this.guild.id, soundId), { reason });
158158
}
159159

160+
/**
161+
* Options used to fetch a soundboard sound.
162+
* @typedef {BaseFetchOptions} FetchSoundboardSoundOptions
163+
* @property {SoundboardSoundResolvable} soundboardSound The soundboard sound to fetch
164+
*/
165+
166+
/**
167+
* Options used to fetch soundboard sounds from Discord
168+
* @typedef {Object} FetchGuildSoundboardSoundsOptions
169+
* @property {boolean} [cache] Whether to cache the fetched soundboard sounds
170+
*/
171+
172+
/* eslint-disable max-len */
160173
/**
161174
* Obtains one or more soundboard sounds from Discord, or the soundboard sound cache if they're already available.
162-
* @param {Snowflake} [id] The soundboard sound's id
163-
* @param {BaseFetchOptions} [options] Additional options for this fetch
175+
* @param {SoundboardSoundResolvable|FetchSoundboardSoundOptions|FetchGuildSoundboardSoundsOptions} [options] Options for fetching soundboard sound(s)
164176
* @returns {Promise<SoundboardSound|Collection<Snowflake, SoundboardSound>>}
165177
* @example
166-
* // Fetch all soundboard sounds from the guild
167-
* guild.soundboardSounds.fetch()
168-
* .then(sounds => console.log(`There are ${sounds.size} soundboard sounds.`))
169-
* .catch(console.error);
170-
* @example
171178
* // Fetch a single soundboard sound
172179
* guild.soundboardSounds.fetch('222078108977594368')
173180
* .then(sound => console.log(`The soundboard sound name is: ${sound.name}`))
174181
* .catch(console.error);
182+
* @example
183+
* // Fetch all soundboard sounds from the guild
184+
* guild.soundboardSounds.fetch()
185+
* .then(sounds => console.log(`There are ${sounds.size} soundboard sounds.`))
186+
* .catch(console.error);
175187
*/
176-
async fetch(id, { cache = true, force = false } = {}) {
177-
if (id) {
178-
if (!force) {
179-
const existing = this.cache.get(id);
180-
if (existing) return existing;
181-
}
182-
183-
const sound = await this.client.rest.get(Routes.guildSoundboardSound(this.guild.id, id));
184-
return this._add(sound, cache);
188+
/* eslint-enable max-len */
189+
async fetch(options) {
190+
if (!options) return this._fetchMany();
191+
const { cache, force, soundboardSound } = options;
192+
const resolvedSoundboardSound = this.resolveId(soundboardSound ?? options);
193+
if (resolvedSoundboardSound) return this._fetchSingle({ cache, force, soundboardSound });
194+
return this._fetchMany({ cache });
195+
}
196+
197+
async _fetchSingle({ cache, force, soundboardSound } = {}) {
198+
if (!force) {
199+
const existing = this.cache.get(soundboardSound);
200+
if (existing) return existing;
185201
}
186202

203+
const data = await this.client.rest.get(Routes.guildSoundboardSound(this.guild.id, soundboardSound));
204+
return this._add(data, cache);
205+
}
206+
207+
async _fetchMany({ cache } = {}) {
187208
const data = await this.client.rest.get(Routes.guildSoundboardSounds(this.guild.id));
188-
return new Collection(data.map(sound => [sound.sound_id, this._add(sound, cache)]));
209+
210+
return data.items.reduce((coll, sound) => coll.set(sound.sound_id, this._add(sound, cache)), new Collection());
189211
}
190212
}
191213

packages/discord.js/src/structures/GuildAuditLogsEntry.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const Targets = {
3232
AutoModeration: 'AutoModeration',
3333
GuildOnboarding: 'GuildOnboarding',
3434
GuildOnboardingPrompt: 'GuildOnboardingPrompt',
35+
SoundboardSound: 'SoundboardSound',
3536
Unknown: 'Unknown',
3637
};
3738

@@ -87,6 +88,7 @@ const Targets = {
8788
* * ApplicationCommandPermission
8889
* * GuildOnboarding
8990
* * GuildOnboardingPrompt
91+
* * SoundboardSound
9092
* * Unknown
9193
* @typedef {string} AuditLogTargetType
9294
*/
@@ -395,7 +397,8 @@ class GuildAuditLogsEntry {
395397
if (target < 110) return Targets.GuildScheduledEvent;
396398
if (target < 120) return Targets.Thread;
397399
if (target < 130) return Targets.ApplicationCommand;
398-
if (target >= 140 && target < 150) return Targets.AutoModeration;
400+
if (target < 140) return Targets.SoundboardSound;
401+
if (target >= 143 && target < 150) return Targets.AutoModeration;
399402
if (target >= 163 && target <= 165) return Targets.GuildOnboardingPrompt;
400403
if (target >= 160 && target < 170) return Targets.GuildOnboarding;
401404
return Targets.Unknown;
@@ -423,6 +426,7 @@ class GuildAuditLogsEntry {
423426
AuditLogEvent.StickerCreate,
424427
AuditLogEvent.GuildScheduledEventCreate,
425428
AuditLogEvent.ThreadCreate,
429+
AuditLogEvent.SoundboardSoundCreate,
426430
AuditLogEvent.AutoModerationRuleCreate,
427431
AuditLogEvent.AutoModerationBlockMessage,
428432
AuditLogEvent.OnboardingPromptCreate,
@@ -452,6 +456,7 @@ class GuildAuditLogsEntry {
452456
AuditLogEvent.StickerDelete,
453457
AuditLogEvent.GuildScheduledEventDelete,
454458
AuditLogEvent.ThreadDelete,
459+
AuditLogEvent.SoundboardSoundDelete,
455460
AuditLogEvent.AutoModerationRuleDelete,
456461
AuditLogEvent.OnboardingPromptDelete,
457462
].includes(action)
@@ -476,6 +481,7 @@ class GuildAuditLogsEntry {
476481
AuditLogEvent.StickerUpdate,
477482
AuditLogEvent.GuildScheduledEventUpdate,
478483
AuditLogEvent.ThreadUpdate,
484+
AuditLogEvent.SoundboardSoundUpdate,
479485
AuditLogEvent.ApplicationCommandPermissionUpdate,
480486
AuditLogEvent.AutoModerationRuleUpdate,
481487
AuditLogEvent.OnboardingPromptUpdate,

packages/discord.js/typings/index.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,6 +4862,12 @@ export interface GuildSoundboardSoundEditOptions {
48624862
emojiName?: string | null;
48634863
}
48644864

4865+
export interface FetchGuildSoundboardSoundOptions extends BaseFetchOptions {
4866+
soundboardSound: SoundboardSoundResolvable;
4867+
}
4868+
4869+
export interface FetchGuildSoundboardSoundsOptions extends Pick<BaseFetchOptions, 'cache'> {}
4870+
48654871
export class GuildSoundboardSoundManager extends CachedManager<Snowflake, SoundboardSound, SoundboardSoundResolvable> {
48664872
private constructor(guild: Guild, iterable?: Iterable<APISoundboardSound>);
48674873
public guild: Guild;
@@ -4871,8 +4877,8 @@ export class GuildSoundboardSoundManager extends CachedManager<Snowflake, Soundb
48714877
options: GuildSoundboardSoundEditOptions,
48724878
): Promise<GuildSoundboardSound>;
48734879
public delete(soundboardSound: SoundboardSoundResolvable): Promise<void>;
4874-
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<GuildSoundboardSound>;
4875-
public fetch(options?: BaseFetchOptions): Promise<Collection<Snowflake, GuildSoundboardSound>>;
4880+
public fetch(options: SoundboardSoundResolvable | FetchGuildSoundboardSoundOptions): Promise<GuildSoundboardSound>;
4881+
public fetch(options?: FetchGuildSoundboardSoundsOptions): Promise<Collection<Snowflake, GuildSoundboardSound>>;
48764882
}
48774883

48784884
export class GuildStickerManager extends CachedManager<Snowflake, Sticker, StickerResolvable> {
@@ -6334,6 +6340,9 @@ interface GuildAuditLogsTypes {
63346340
[AuditLogEvent.ThreadUpdate]: ['Thread', 'Update'];
63356341
[AuditLogEvent.ThreadDelete]: ['Thread', 'Delete'];
63366342
[AuditLogEvent.ApplicationCommandPermissionUpdate]: ['ApplicationCommand', 'Update'];
6343+
[AuditLogEvent.SoundboardSoundCreate]: ['SoundboardSound', 'Create'];
6344+
[AuditLogEvent.SoundboardSoundUpdate]: ['SoundboardSound', 'Update'];
6345+
[AuditLogEvent.SoundboardSoundDelete]: ['SoundboardSound', 'Delete'];
63376346
[AuditLogEvent.AutoModerationRuleCreate]: ['AutoModeration', 'Create'];
63386347
[AuditLogEvent.AutoModerationRuleUpdate]: ['AutoModeration', 'Update'];
63396348
[AuditLogEvent.AutoModerationRuleDelete]: ['AutoModeration', 'Delete'];
@@ -6410,6 +6419,7 @@ export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLo
64106419
ApplicationCommand: ApplicationCommand | { id: Snowflake };
64116420
AutoModerationRule: AutoModerationRule;
64126421
GuildOnboardingPrompt: GuildOnboardingPrompt;
6422+
SoundboardSound: { id: Snowflake };
64136423
}
64146424

64156425
export interface GuildAuditLogsFetchOptions<Event extends GuildAuditLogsResolvable> {

0 commit comments

Comments
 (0)