@@ -157,35 +157,57 @@ class GuildSoundboardSoundManager extends CachedManager {
157
157
await this . client . rest . delete ( Routes . guildSoundboardSound ( this . guild . id , soundId ) , { reason } ) ;
158
158
}
159
159
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 */
160
173
/**
161
174
* 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)
164
176
* @returns {Promise<SoundboardSound|Collection<Snowflake, SoundboardSound>> }
165
177
* @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
171
178
* // Fetch a single soundboard sound
172
179
* guild.soundboardSounds.fetch('222078108977594368')
173
180
* .then(sound => console.log(`The soundboard sound name is: ${sound.name}`))
174
181
* .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);
175
187
*/
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 ;
185
201
}
186
202
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 } = { } ) {
187
208
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 ( ) ) ;
189
211
}
190
212
}
191
213
0 commit comments