Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "shoukaku", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A lavalink client for Discord.js v12 only", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,2 +11,6 @@ const { RawRouter, ReconnectRouter } = require('./ShoukakuRouter.js'); | ||
/** | ||
* @external Guild | ||
* @see {@link https://discord.js.org/#/docs/main/master/class/Guild} | ||
*/ | ||
/** | ||
* @external EventEmitter | ||
@@ -48,3 +52,3 @@ * @see {@link https://nodejs.org/api/events.html} | ||
this.nodes = new Map(); | ||
Object.defineProperty(this, 'options', { value: this._mergeDefault(constants.ShoukakuOptions, options) }); | ||
@@ -73,3 +77,3 @@ Object.defineProperty(this, 'init', { value: true, writable: true }); | ||
} | ||
// Events | ||
@@ -147,2 +151,3 @@ /** | ||
} | ||
// noinspection JSCommentMatchesSignature | ||
/** | ||
@@ -193,4 +198,3 @@ * Function to remove a Lavalink Node | ||
if (!guildID) return null; | ||
if (!this.nodes.size) | ||
throw new Error('No nodes available. What happened?'); | ||
if (!this.nodes.size) return null; | ||
for (const node of this.nodes.values()) { | ||
@@ -200,2 +204,3 @@ const link = node.links.get(guildID); | ||
} | ||
return null; | ||
} | ||
@@ -202,0 +207,0 @@ |
@@ -1,2 +0,2 @@ | ||
/** | ||
/** | ||
* Documentation of constants for ease of usage. | ||
@@ -7,7 +7,7 @@ */ | ||
* Available Status for Node / Link managers. | ||
* @typedef {string} SHOUKAKU_STATUS | ||
* @enum {SHOUKAKU_STATUS} | ||
* @typedef {string} ShoukakuStatus | ||
* @enum {ShoukakuStatus} | ||
* @memberof ShoukakuConstants# | ||
*/ | ||
static get SHOUKAKU_STATUS() { | ||
static get ShoukakuStatus() { | ||
return { | ||
@@ -19,3 +19,3 @@ CONNECTING: 'CONNECTING', | ||
}; | ||
} | ||
} | ||
/** | ||
@@ -27,6 +27,6 @@ * Shoukaku's Node Stats Object. | ||
static get ShoukakuNodeStats() { | ||
return { | ||
return { | ||
playingPlayers: 0, | ||
op: 'stats', | ||
memory: { | ||
memory: { | ||
reservable: 0, | ||
@@ -37,12 +37,12 @@ used: 0, | ||
}, | ||
frameStats: { | ||
sent: 0, | ||
deficit: 0, | ||
nulled: 0 | ||
frameStats: { | ||
sent: 0, | ||
deficit: 0, | ||
nulled: 0 | ||
}, | ||
players: 0, | ||
cpu: { | ||
cpu: { | ||
cores: 0, | ||
systemLoad: 0, | ||
lavalinkLoad: 0 | ||
lavalinkLoad: 0 | ||
}, | ||
@@ -124,3 +124,3 @@ uptime: 0 | ||
static get ShoukakuBuildOptions() { | ||
return { | ||
return { | ||
id: null, | ||
@@ -131,2 +131,2 @@ shardCount: 1 | ||
} | ||
module.exports = ShoukakuConstants; | ||
module.exports = ShoukakuConstants; |
@@ -1,2 +0,2 @@ | ||
const { SHOUKAKU_STATUS } = require('./ShoukakuConstants.js'); | ||
const { ShoukakuStatus } = require('./ShoukakuConstants.js'); | ||
const ShoukakuPlayer = require('./ShoukakuPlayer.js'); | ||
@@ -7,5 +7,5 @@ class ShoukakuLink { | ||
* @param {ShoukakuSocket} node The node where this class initialization is called. | ||
* @param {number} shardID The shardID of the guild. | ||
* @param {external:Guild} guild the Guild Object of this guild. | ||
*/ | ||
constructor(node, shardID) { | ||
constructor(node, guild) { | ||
/** | ||
@@ -17,24 +17,24 @@ * The node that governs this Link | ||
/** | ||
* The sessionID of this Link | ||
* The ID of the guild that is being governed by this Link. | ||
* @type {string} | ||
*/ | ||
this.sessionID = null; | ||
this.guildID = guild.id; | ||
/** | ||
* The ID of the user that is being governed by this Link | ||
* @type {string} | ||
* The ID of the shard where this guild is in | ||
* @type {number} | ||
*/ | ||
this.userID = null; | ||
this.shardID = guild.shardID; | ||
/** | ||
* The ID of the guild that is being governed by this Link. | ||
* The ID of the user that is being governed by this Link | ||
* @type {string} | ||
*/ | ||
this.guildID = null; | ||
this.userID = node.shoukaku.id; | ||
/** | ||
* The ID of the shard where this guild is in | ||
* @type {number} | ||
* The sessionID of this Link | ||
* @type {?string} | ||
*/ | ||
this.shardID = shardID; | ||
this.sessionID = null; | ||
/** | ||
* The ID of the voice channel that is being governed by this link. | ||
* @type {string} | ||
* @type {?string} | ||
*/ | ||
@@ -54,5 +54,5 @@ this.voiceChannelID = null; | ||
* The current state of this link. | ||
* @type {ShoukakuConstants#SHOUKAKU_STATUS} | ||
* @type {ShoukakuConstants#ShoukakuStatus} | ||
*/ | ||
this.state = SHOUKAKU_STATUS.DISCONNECTED; | ||
this.state = ShoukakuStatus.DISCONNECTED; | ||
/** | ||
@@ -70,6 +70,4 @@ * The player class of this link. | ||
set build(data) { | ||
this.userID = data.user_id; | ||
this.selfDeaf = data.self_deaf; | ||
this.selfMute = data.self_mute; | ||
this.guildID = data.guild_id; | ||
this.voiceChannelID = data.channel_id; | ||
@@ -79,5 +77,5 @@ this.sessionID = data.session_id; | ||
set serverUpdate(packet) { | ||
this.lastServerUpdate = packet.d; | ||
this._voiceUpdate(packet.d); | ||
set serverUpdate(data) { | ||
this.lastServerUpdate = data; | ||
this._voiceUpdate(data); | ||
} | ||
@@ -94,9 +92,12 @@ /** | ||
this._callback = callback; | ||
if (this.state === SHOUKAKU_STATUS.CONNECTING) | ||
return this._callback(new Error('Can\'t connect a connecting link. Wait for it to resolve first')); | ||
if (this.state === ShoukakuStatus.CONNECTING) { | ||
this._callback(new Error('Can\'t connect a connecting link. Wait for it to resolve first')); | ||
return; | ||
} | ||
this._timeout = setTimeout(() => { | ||
this.state = SHOUKAKU_STATUS.DISCONNECTED; | ||
this.state = ShoukakuStatus.DISCONNECTED; | ||
this._callback(new Error('The voice connection is not established in 15 seconds')); | ||
}, 15000); | ||
this.state = SHOUKAKU_STATUS.CONNECTING; | ||
this.state = ShoukakuStatus.CONNECTING; | ||
this._queueConnection(options); | ||
@@ -109,7 +110,9 @@ } | ||
disconnect() { | ||
this.state = SHOUKAKU_STATUS.DISCONNECTING; | ||
this.state = ShoukakuStatus.DISCONNECTING; | ||
this.node.links.delete(this.guildID); | ||
this.player.removeAllListeners() && this._clearVoice(); | ||
this.player._clearTrack() && this.player._clearPlayer(); | ||
if (this.state !== SHOUKAKU_STATUS.DISCONNECTED) { | ||
this.player.removeAllListeners(); | ||
this._clearVoice(); | ||
this.player._clearTrack(); | ||
this.player._clearPlayer(); | ||
if (this.state !== ShoukakuStatus.DISCONNECTED) { | ||
this._destroy(); | ||
@@ -137,3 +140,3 @@ this._removeConnection(this.guildID); | ||
}); | ||
this.state = SHOUKAKU_STATUS.DISCONNECTED; | ||
this.state = ShoukakuStatus.DISCONNECTED; | ||
} | ||
@@ -154,3 +157,3 @@ | ||
_voiceUpdate(data) { | ||
_voiceUpdate(event) { | ||
this.node.send({ | ||
@@ -160,17 +163,17 @@ op: 'voiceUpdate', | ||
sessionId: this.sessionID, | ||
event: data | ||
event | ||
}) | ||
.then(() => { | ||
if (this.state !== SHOUKAKU_STATUS.CONNECTING) return; | ||
if (this.state !== ShoukakuStatus.CONNECTING) return; | ||
clearTimeout(this._timeout); | ||
this.state = SHOUKAKU_STATUS.CONNECTED; | ||
this.state = ShoukakuStatus.CONNECTED; | ||
this._callback(null, this); | ||
}) | ||
.catch((error) => { | ||
if (this.state === SHOUKAKU_STATUS.CONNECTING) { | ||
if (this.state === ShoukakuStatus.CONNECTING) { | ||
clearTimeout(this._timeout); | ||
this.state = SHOUKAKU_STATUS.DISCONNECTED; | ||
this.state = ShoukakuStatus.DISCONNECTED; | ||
return this._callback(error); | ||
} | ||
this.player.emit('voiceClose', error); | ||
} | ||
this.player._listen('voiceClose', error); | ||
}) | ||
@@ -184,3 +187,3 @@ .finally(() => { | ||
_voiceDisconnect() { | ||
this.state = SHOUKAKU_STATUS.DISCONNECTED; | ||
this.state = ShoukakuStatus.DISCONNECTED; | ||
this._destroy(); | ||
@@ -187,0 +190,0 @@ } |
@@ -20,3 +20,3 @@ const EventEmitter = require('events'); | ||
* The Track that is currently being played by this player. | ||
* @type {base64} | ||
* @type {?string} | ||
*/ | ||
@@ -63,2 +63,6 @@ this.track = null; | ||
/** | ||
* Emitted when the Shoukaku Player resumes the session by resending the playing data. | ||
* @event ShoukakuPlayer#resumed | ||
*/ | ||
/** | ||
* Emitted when the Client's Voice Connection got closed by Discord. This can also throw errors so make sure you handle this. | ||
@@ -94,3 +98,3 @@ * @event ShoukakuPlayer#voiceClose | ||
* Plays the track you specifed. Warning: If the player is playing anything, calling this will just ignore your call. Call `ShoukakuPlayer.StopTrack()` first. | ||
* @param {base64} track The Base64 encoded track you got from lavalink API. | ||
* @param {string} track The Base64 encoded track you got from lavalink API. | ||
* @param {ShoukakuConstants#ShoukakuPlayOptions} [options=ShoukakuPlayOptions] Used if you want to put a custom track start or end time. | ||
@@ -192,5 +196,5 @@ * @returns {Promise<boolean>} true if sucessful false if not. | ||
} | ||
this.position = data.position; | ||
if (data) this.position = data.position; | ||
this.emit(event, data); | ||
} | ||
} | ||
@@ -205,10 +209,21 @@ _clearTrack() { | ||
} | ||
/* soon:tm: | ||
_onNodeChange() { | ||
if (!this.track) return; | ||
this.playTrack(this.track, { startTime: this.position }).catch(() => null); | ||
async _resume() { | ||
if (!this.track) { | ||
this._listen('voiceClose', { | ||
type: 'NoTrackFound', | ||
reason: 'No track found, assuming a dead player.' | ||
}); | ||
return; | ||
} | ||
try { | ||
await this.playTrack(this.track.repeat(1), { startTime: this.position }); | ||
await this.setEqualizer(this.bands.slice(0)); | ||
await this.setVolume(Number(this.volume)); | ||
this._listen('resumed', null); | ||
} catch (error) { | ||
this._listen('voiceClose', error); | ||
} | ||
} | ||
*/ | ||
} | ||
module.exports = ShoukakuPlayer; |
@@ -1,2 +0,2 @@ | ||
const { SHOUKAKU_STATUS } = require('./ShoukakuConstants.js'); | ||
const { ShoukakuStatus } = require('./ShoukakuConstants.js'); | ||
class ShoukakuRouter { | ||
@@ -7,3 +7,3 @@ static ReconnectRouter(id) { | ||
if (!link.voiceChannelID) return; | ||
if (link.state === SHOUKAKU_STATUS.CONNECTING) return; | ||
if (link.state === ShoukakuStatus.CONNECTING) return; | ||
if (link.shardID !== id) return; | ||
@@ -16,3 +16,8 @@ link.connect({ | ||
}, (error) => { | ||
if (error) link.player._listen('voiceClose', error); | ||
if (error) { | ||
link.player._listen('voiceClose', error); | ||
return; | ||
} | ||
link.player._resume() | ||
.catch(() => null); | ||
}); | ||
@@ -35,9 +40,6 @@ }); | ||
if (packet.t === 'VOICE_STATE_UPDATE') { | ||
if (!packet.d.channel_id) { | ||
if (link.state !== SHOUKAKU_STATUS.DISCONNECTED) link._voiceDisconnect(); | ||
return; | ||
} | ||
if (!packet.d.channel_id) return link.disconnect(); | ||
link.build = packet.d; | ||
} | ||
if (packet.t === 'VOICE_SERVER_UPDATE') link.serverUpdate = packet; | ||
if (packet.t === 'VOICE_SERVER_UPDATE') link.serverUpdate = packet.d; | ||
} | ||
@@ -44,0 +46,0 @@ |
@@ -1,2 +0,2 @@ | ||
const { SHOUKAKU_STATUS, ShoukakuNodeStats, ShoukakuJoinOptions } = require('./ShoukakuConstants.js'); | ||
const { ShoukakuStatus, ShoukakuNodeStats, ShoukakuJoinOptions } = require('./ShoukakuConstants.js'); | ||
const { PacketRouter, EventRouter } = require('./ShoukakuRouter.js'); | ||
@@ -12,3 +12,3 @@ const ShoukakuResolver = require('./ShoukakuResolver.js'); | ||
* @param {Shoukaku} shoukaku Your Shoukaku Instance | ||
* @param {ShoukakuOptions} [node=ShoukakuNodeOptions] Options to initialize Shoukaku with | ||
* @param {ShoukakuOptions} node ShoukakuNodeOptions Options to initialize Shoukaku with | ||
*/ | ||
@@ -31,8 +31,8 @@ constructor(shoukaku, node) { | ||
*/ | ||
this.rest = new ShoukakuResolver(node.host, node.port, node.auth,shoukaku.options.restTimeout); | ||
this.rest = new ShoukakuResolver(node.host, node.port, node.auth, shoukaku.options.restTimeout); | ||
/** | ||
* The state of this Socket. | ||
* @type {ShoukakuConstants#SHOUKAKU_STATUS} | ||
* @type {ShoukakuConstants#ShoukakuStatus} | ||
*/ | ||
this.state = SHOUKAKU_STATUS.DISCONNECTED; | ||
this.state = ShoukakuStatus.DISCONNECTED; | ||
/** | ||
@@ -53,3 +53,3 @@ * The current stats of this Socket. | ||
this.name = node.name; | ||
Object.defineProperty(this, 'url', { value: `ws://${node.host}:${node.port}` }); | ||
@@ -122,11 +122,15 @@ Object.defineProperty(this, 'auth', { value: node.auth }); | ||
return reject(new Error('Guild ID or Channel ID is not specified.')); | ||
const link = this.links.get(options.guildID); | ||
if (link) | ||
return reject(new Error('A voice connection is already established in this channel.')); | ||
const guild = this.shoukaku.client.guilds.get(options.guildID); | ||
if (!guild) | ||
return reject(new Error('Guild not found. Cannot continue creating the voice connection.')); | ||
const newLink = new ShoukakuLink(this, guild.shardID); | ||
this.links.set(options.guildID, newLink); | ||
options = { | ||
const newLink = new ShoukakuLink(this, guild); | ||
this.links.set(guild.id, newLink); | ||
const _object = { | ||
guild_id: options.guildID, | ||
@@ -137,5 +141,6 @@ channel_id: options.voiceChannelID, | ||
}; | ||
newLink.connect(options, (error, value) => { | ||
newLink.connect(_object, (error, value) => { | ||
if (error) { | ||
this.links.delete(options.guildID); | ||
this.links.delete(guild.id); | ||
reject(error); | ||
@@ -190,3 +195,3 @@ return; | ||
this.reconnectAttempts = 0; | ||
this.state = SHOUKAKU_STATUS.CONNECTED; | ||
this.state = ShoukakuStatus.CONNECTED; | ||
this.emit('ready', this.name, this.resumed); | ||
@@ -193,0 +198,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48038
1099