-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: add soundboard v14 #10843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add soundboard v14 #10843
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/discord.js/src/client/actions/GuildSoundboardSoundDelete.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action.js'); | ||
const Events = require('../../util/Events.js'); | ||
Qjuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class GuildSoundboardSoundDeleteAction extends Action { | ||
handle(data) { | ||
const guild = this.client.guilds.cache.get(data.guild_id); | ||
|
||
if (!guild) return {}; | ||
|
||
const soundboardSound = this.getSoundboardSound(data, guild); | ||
|
||
if (soundboardSound) { | ||
guild.soundboardSounds.cache.delete(soundboardSound.soundId); | ||
|
||
/** | ||
* Emitted whenever a soundboard sound is deleted in a guild. | ||
* @event Client#guildSoundboardSoundDelete | ||
* @param {SoundboardSound} soundboardSound The soundboard sound that was deleted | ||
*/ | ||
this.client.emit(Events.GuildSoundboardSoundDelete, soundboardSound); | ||
} | ||
|
||
return { soundboardSound }; | ||
} | ||
} | ||
|
||
module.exports = GuildSoundboardSoundDeleteAction; |
24 changes: 24 additions & 0 deletions
24
packages/discord.js/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUNDS_UPDATE.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const { Collection } = require('@discordjs/collection'); | ||
const Events = require('../../../util/Events.js'); | ||
Qjuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = (client, { d: data }) => { | ||
const guild = client.guilds.cache.get(data.guild_id); | ||
|
||
if (!guild) return; | ||
|
||
const soundboardSounds = new Collection(); | ||
|
||
for (const soundboardSound of data.soundboard_sounds) { | ||
soundboardSounds.set(soundboardSound.sound_id, guild.soundboardSounds._add(soundboardSound)); | ||
} | ||
|
||
/** | ||
* Emitted whenever multiple guild soundboard sounds are updated. | ||
* @event Client#guildSoundboardSoundsUpdate | ||
* @param {Collection<Snowflake, SoundboardSound>} soundboardSounds The updated soundboard sounds | ||
* @param {Guild} guild The guild that the soundboard sounds are from | ||
*/ | ||
client.emit(Events.GuildSoundboardSoundsUpdate, soundboardSounds, guild); | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/discord.js/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_CREATE.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const Events = require('../../../util/Events.js'); | ||
Qjuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = (client, { d: data }) => { | ||
const guild = client.guilds.cache.get(data.guild_id); | ||
|
||
if (!guild) return; | ||
|
||
const soundboardSound = guild.soundboardSounds._add(data); | ||
|
||
/** | ||
* Emitted whenever a guild soundboard sound is created. | ||
* @event Client#guildSoundboardSoundCreate | ||
* @param {SoundboardSound} soundboardSound The created guild soundboard sound | ||
*/ | ||
client.emit(Events.GuildSoundboardSoundCreate, soundboardSound); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/discord.js/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_DELETE.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, { d: data }) => { | ||
client.actions.GuildSoundboardSoundDelete.handle(data); | ||
}; |
20 changes: 20 additions & 0 deletions
20
packages/discord.js/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_UPDATE.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const Events = require('../../../util/Events.js'); | ||
Qjuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = (client, { d: data }) => { | ||
const guild = client.guilds.cache.get(data.guild_id); | ||
|
||
if (!guild) return; | ||
|
||
const oldGuildSoundboardSound = guild.soundboardSounds.cache.get(data.sound_id)?._clone() ?? null; | ||
const newGuildSoundboardSound = guild.soundboardSounds._add(data); | ||
|
||
/** | ||
* Emitted whenever a guild soundboard sound is updated. | ||
* @event Client#guildSoundboardSoundUpdate | ||
* @param {?SoundboardSound} oldGuildSoundboardSound The guild soundboard sound before the update | ||
* @param {SoundboardSound} newGuildSoundboardSound The guild soundboard sound after the update | ||
*/ | ||
client.emit(Events.GuildSoundboardSoundUpdate, oldGuildSoundboardSound, newGuildSoundboardSound); | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/discord.js/src/client/websocket/handlers/SOUNDBOARD_SOUNDS.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const { Collection } = require('@discordjs/collection'); | ||
const Events = require('../../../util/Events.js'); | ||
Qjuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = (client, { d: data }) => { | ||
const guild = client.guilds.cache.get(data.guild_id); | ||
|
||
if (!guild) return; | ||
|
||
const soundboardSounds = new Collection(); | ||
|
||
for (const soundboardSound of data.soundboard_sounds) { | ||
soundboardSounds.set(soundboardSound.sound_id, guild.soundboardSounds._add(soundboardSound)); | ||
} | ||
|
||
/** | ||
* Emitted whenever soundboard sounds are received (all soundboard sounds come from the same guild). | ||
* @event Client#soundboardSounds | ||
* @param {Collection<Snowflake, SoundboardSound>} soundboardSounds The sounds received | ||
* @param {Guild} guild The guild that the soundboard sounds are from | ||
*/ | ||
client.emit(Events.SoundboardSounds, soundboardSounds, guild); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ const process = require('node:process'); | |
const { setTimeout, clearTimeout } = require('node:timers'); | ||
const { Collection } = require('@discordjs/collection'); | ||
const { makeURLSearchParams } = require('@discordjs/rest'); | ||
const { Routes, RouteBases } = require('discord-api-types/v10'); | ||
const { GatewayOpcodes, Routes, RouteBases } = require('discord-api-types/v10'); | ||
const CachedManager = require('./CachedManager'); | ||
const { ErrorCodes, DiscordjsError } = require('../errors/index.js'); | ||
const ShardClientUtil = require('../sharding/ShardClientUtil'); | ||
const { Guild } = require('../structures/Guild'); | ||
const GuildChannel = require('../structures/GuildChannel'); | ||
|
@@ -282,6 +283,79 @@ class GuildManager extends CachedManager { | |
return data.reduce((coll, guild) => coll.set(guild.id, new OAuth2Guild(this.client, guild)), new Collection()); | ||
} | ||
|
||
/** | ||
* @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 | ||
*/ | ||
|
||
/** | ||
* Fetches soundboard sounds for the specified guilds. | ||
* @param {FetchSoundboardSoundsOptions} options The options for fetching soundboard sounds | ||
* @returns {Promise<Collection<Snowflake, Collection<Snowflake, SoundboardSound>>>} | ||
* @example | ||
* // Fetch soundboard sounds for multiple guilds | ||
* const soundboardSounds = await client.guilds.fetchSoundboardSounds({ | ||
* guildIds: ['123456789012345678', '987654321098765432'], | ||
* }) | ||
* | ||
* console.log(soundboardSounds.get('123456789012345678')); | ||
*/ | ||
async fetchSoundboardSounds({ guildIds, time = 10_000 }) { | ||
const shardCount = await this.client.options.shardCount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem right, it could be this.client.ws._ws.getShardCount or w/e |
||
const shardIds = new Map(); | ||
|
||
for (const guildId of guildIds) { | ||
const shardId = ShardClientUtil.shardIdForGuildId(guildId, shardCount); | ||
const group = shardIds.get(shardId); | ||
|
||
if (group) group.push(guildId); | ||
else shardIds.set(shardId, [guildId]); | ||
} | ||
|
||
for (const [shardId, shardGuildIds] of shardIds) { | ||
this.client.ws.shards.get(shardId).send({ | ||
op: GatewayOpcodes.RequestSoundboardSounds, | ||
d: { | ||
guild_ids: shardGuildIds, | ||
}, | ||
}); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const remainingGuildIds = new Set(guildIds); | ||
|
||
const fetchedSoundboardSounds = new Collection(); | ||
|
||
const handler = (soundboardSounds, guild) => { | ||
timeout.refresh(); | ||
|
||
if (!remainingGuildIds.has(guild.id)) return; | ||
|
||
fetchedSoundboardSounds.set(guild.id, soundboardSounds); | ||
|
||
remainingGuildIds.delete(guild.id); | ||
|
||
if (remainingGuildIds.size === 0) { | ||
clearTimeout(timeout); | ||
this.client.removeListener(Events.SoundboardSounds, handler); | ||
this.client.decrementMaxListeners(); | ||
|
||
resolve(fetchedSoundboardSounds); | ||
} | ||
}; | ||
|
||
const timeout = setTimeout(() => { | ||
this.client.removeListener(Events.SoundboardSounds, handler); | ||
this.client.decrementMaxListeners(); | ||
reject(new DiscordjsError(ErrorCodes.GuildSoundboardSoundsTimeout)); | ||
}, time).unref(); | ||
|
||
this.client.incrementMaxListeners(); | ||
this.client.on(Events.SoundboardSounds, handler); | ||
}); | ||
} | ||
|
||
/** | ||
* Options used to set incident actions. Supplying `null` to any option will disable the action. | ||
* @typedef {Object} IncidentActionsEditOptions | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.