Comparing version 1.1.9 to 1.2.0
{ | ||
"name": "poru", | ||
"version": "1.1.9", | ||
"version": "1.2.0", | ||
"description": "A stable and powefull lavalink client with so many features", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -224,3 +224,3 @@ const { EventEmitter } = require("events"); | ||
if (!response || !response.tracks || ["LOAD_FAILED", "NO_MATCHES"].includes(response.type)) return this.stop(); | ||
if (!response || !response.tracks || ["LOAD_FAILED", "NO_MATCHES"].includes(response.loadType)) return this.stop(); | ||
@@ -227,0 +227,0 @@ let track = response.tracks[Math.floor(Math.random() * Math.floor(response.tracks.length))]; |
207
src/Poru.js
@@ -11,20 +11,22 @@ const { EventEmitter } = require("events"); | ||
class Poru extends EventEmitter { | ||
constructor(client, nodes, options = {}) { | ||
super(); | ||
if (!client) throw new Error("[Poru Error] you did't provide a valid client"); | ||
if (!nodes) throw new Error("[Poru Error] you did't provide a lavalink nodes"); | ||
if (!options) throw new Error('[Poru Error] options must be provided!'); | ||
this.client = client; | ||
this._nodes = nodes; | ||
this.nodes = new Map(); | ||
this.players = new Map(); | ||
this.voiceStates = new Map(); | ||
this.voiceServers = new Map(); | ||
this.user = null; | ||
this.options = options; | ||
this.shards = options.shards || 1; | ||
this.sendData = null; | ||
} | ||
constructor(client, nodes, options = {}) { | ||
super(); | ||
if (!client) throw new Error("[Poru Error] you did't provide a valid client"); | ||
if (!nodes) throw new Error("[Poru Error] you did't provide a lavalink nodes"); | ||
if (!options) throw new Error("[Poru Error] options must be provided!") | ||
this.client = client; | ||
this._nodes = nodes; | ||
this.nodes = new Map(); | ||
this.players = new Map(); | ||
this.voiceStates = new Map(); | ||
this.voiceServers = new Map(); | ||
this.user = null; | ||
this.options = options | ||
this.shards = options.shards || 1; | ||
this.sendData = null; | ||
} | ||
//create a node and connect it with lavalink | ||
//create a node and connect it with lavalink | ||
addNode(options) { | ||
@@ -77,3 +79,3 @@ const node = new Node(this, options, this.options); | ||
client.on("raw", async packet => { | ||
await this.packetUpdate(packet); | ||
await this.#packetUpdate(packet); | ||
}) | ||
@@ -115,79 +117,71 @@ | ||
return true; | ||
this.sendData({ | ||
op: 4, | ||
d: { | ||
guild_id: data.guild.id || data.guild, | ||
channel_id: data.voiceChannel.id || data.voiceChannel, | ||
self_mute: data.selfMute || false, | ||
self_deaf: data.selfDeaf || true, | ||
}, | ||
}); | ||
return this.#Player(data); | ||
} | ||
} | ||
init(client) { | ||
this.user = client.user.id; | ||
this.sendData = data => { | ||
const guild = client.guilds.cache.get(data.d.guild_id); | ||
if (guild) guild.shard.send(data); | ||
}; | ||
client.on('raw', async packet => { | ||
await this.#packetUpdate(packet); | ||
}); | ||
setStateUpdate(data) { | ||
if (data.user_id !== this.user) return; | ||
if (data.channel_id) { | ||
const guild = data.guild_id; | ||
this._nodes.forEach(node => this.addNode(node)); | ||
this.voiceStates.set(data.guild_id, data); | ||
const server = this.voiceServers.get(guild); | ||
const state = this.voiceStates.get(guild); | ||
if (!server) return false; | ||
const player = this.players.get(guild); | ||
if (!player) return false; | ||
if (this.options.spotify && this.options.spotify.clientID && this.options.spotify.clientSecret) { | ||
this.spotify = new Spotify(this, { | ||
clientID: this.options.clientID, | ||
clientSecret: this.options.clientSecret, | ||
playlistLimit: this.options.playlistLimit, | ||
albumLimit: this.options.albumLimit, | ||
artistLimit: this.options.artistLimit, | ||
searchMarket: this.options.searchMarket, | ||
}); | ||
player.connect({ | ||
sessionId: state ? state.session_id : player.voiceUpdateState.sessionId, | ||
event: server, | ||
}); | ||
return true; | ||
} | ||
this.voiceServers.delete(data.guild_id); | ||
this.voiceStates.delete(data.guild_id); | ||
} | ||
this.voiceServers.delete(data.guild_id); | ||
this.voiceStates.delete(data.guild_id); | ||
} | ||
#packetUpdate(packet) { | ||
if (!['VOICE_STATE_UPDATE', 'VOICE_SERVER_UPDATE'].includes(packet.t)) return; | ||
const player = this.players.get(packet.d.guild_id); | ||
if (!player) return; | ||
#packetUpdate(packet) { | ||
if (!['VOICE_STATE_UPDATE', 'VOICE_SERVER_UPDATE'].includes(packet.t)) return; | ||
const player = this.players.get(packet.d.guild_id); | ||
if (!player) return; | ||
if (packet.t === 'VOICE_SERVER_UPDATE') { | ||
this.setServersUpdate(packet.d); | ||
if (packet.t === "VOICE_SERVER_UPDATE") { | ||
this.setServersUpdate(packet.d); | ||
} | ||
if (packet.t === "VOICE_STATE_UPDATE") { | ||
this.setStateUpdate(packet.d); | ||
} | ||
} | ||
if (packet.t === 'VOICE_STATE_UPDATE') { | ||
this.setStateUpdate(packet.d); | ||
get leastUsedNodes() { | ||
return [...this.nodes.values()] | ||
.filter((node) => node.isConnected) | ||
.sort((a, b) => { | ||
const aLoad = a.stats.cpu ? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100 : 0; | ||
const bLoad = b.stats.cpu ? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100 : 0; | ||
return aLoad - bLoad; | ||
}); | ||
} | ||
} | ||
get leastUsedNodes() { | ||
return [...this.nodes.values()] | ||
.filter(node => node.isConnected) | ||
.sort((a, b) => { | ||
const aLoad = a.stats.cpu ? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100 : 0; | ||
const bLoad = b.stats.cpu ? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100 : 0; | ||
return aLoad - bLoad; | ||
}); | ||
} | ||
#Player(data) { | ||
const guild = data.guild.id || data.guild; | ||
const Nodes = this.nodes.get(guild); | ||
if (Nodes) return Nodes; | ||
if (this.leastUsedNodes.length === 0) throw new Error("[Poru Error] No nodes are avaliable"); | ||
const node = this.nodes.get(this.leastUsedNodes[0].name | ||
|| this.leastUsedNodes[0].host); | ||
if (!node) throw new Error("[Poru Error] No nodes are avalible"); | ||
#Player(data) { | ||
const guild = data.guild.id || data.guild; | ||
const Nodes = this.nodes.get(guild); | ||
if (Nodes) return Nodes; | ||
if (this.leastUsedNodes.length === 0) throw new Error('[Poru Error] No nodes are avaliable'); | ||
const node = this.nodes.get(this.leastUsedNodes[0].name || this.leastUsedNodes[0].host); | ||
if (!node) throw new Error('[Poru Error] No nodes are avalible'); | ||
// eslint-disable-next-line new-cap | ||
const player = new Player(this, node, data); | ||
this.players.set(guild, player); | ||
player.connect() | ||
return player; | ||
} | ||
// eslint-disable-next-line new-cap | ||
const player = new Player(this, node, data); | ||
this.players.set(guild, player); | ||
player.connect(); | ||
return player; | ||
} | ||
async resolve(track, source) { | ||
@@ -225,38 +219,21 @@ | ||
} | ||
const regex = /^https?:\/\//; | ||
if (!regex.test(track)) { | ||
// eslint-disable-next-line no-param-reassign | ||
track = `${source || 'yt'}search:${track}`; | ||
} | ||
const result = await this.#fetch(node, 'loadtracks', `identifier=${encodeURIComponent(track)}`); | ||
if (!result) throw new Error('[Poru Error] No tracks found.'); | ||
return new Response(result); | ||
} | ||
#fetch(node, endpoint, param) { | ||
return fetch(`http${node.secure ? "s" : ""}://${node.host}:${node.port}/${endpoint}?${param}`, { | ||
headers: { | ||
Authorization: node.password, | ||
async decodeTrack(track) { | ||
const node = this.leastUsedNodes[0]; | ||
if (!node) throw new Error('No nodes are available.'); | ||
const result = await this.#fetch(node, 'decodetrack', `track=${track}`); | ||
if (result.status === 500) return null; | ||
return result; | ||
} | ||
}, | ||
}) | ||
.then((r) => r.json()) | ||
.catch((e) => { | ||
throw new Error(`[Poru Error] Failed to fetch from the lavalink.\n error: ${e}`); | ||
}); | ||
} | ||
#fetch(node, endpoint, param) { | ||
return fetch(`http${node.secure ? 's' : ''}://${node.host}:${node.port}/${endpoint}?${param}`, { | ||
headers: { | ||
Authorization: node.password, | ||
}, | ||
}) | ||
.then(r => r.json()) | ||
.catch(e => { | ||
throw new Error(`[Poru Error] Failed to fetch from the lavalink.\n error: ${e}`); | ||
}); | ||
} | ||
get(guildId) { | ||
return this.players.get(guildId); | ||
} | ||
get(guildId) { | ||
return this.players.get(guildId); | ||
} | ||
} | ||
module.exports = Poru; | ||
module.exports = Poru |
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
52145
1326