Skip to content

Commit

Permalink
add: queue management functions (#3)
Browse files Browse the repository at this point in the history
This commit adds the loop function, which allows to add different types of loop. It also updates the syntax, using enforced checking (===).
  • Loading branch information
TehPigYT committed Jan 14, 2024
1 parent 47854d2 commit 91bb6d0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 68 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ client.on('messageCreate', async (message) => {
if (commandName == 'decodetrack') {
const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) {
if (player.playerCreated() === false) {
message.channel.send('No player found.')

return;
Expand Down Expand Up @@ -134,21 +134,21 @@ client.on('messageCreate', async (message) => {

const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) player.createPlayer()
if (player.playerCreated() === false) player.createPlayer()

player.connect(message.member.voice.channel.id.toString(), { mute: false, deaf: true }, (guildId, payload) => {
client.guilds.cache.get(guildId).shard.send(payload)
})

const track = await player.loadTrack((args.startsWith('https://') ? '' : 'ytsearch:') + args)

if (track.loadType == 'error') {
if (track.loadType === 'error') {
message.channel.send('Something went wrong. ' + track.data.message)

return;
}

if (track.loadType == 'empty') {
if (track.loadType === 'empty') {
message.channel.send('No matches found.')

return;
Expand Down Expand Up @@ -194,7 +194,7 @@ client.on('messageCreate', async (message) => {
if (commandName == 'volume') {
const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) {
if (player.playerCreated() === false) {
message.channel.send('No player found.')

return;
Expand All @@ -212,7 +212,7 @@ client.on('messageCreate', async (message) => {
if (commandName == 'pause') {
const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) {
if (player.playerCreated() === false) {
message.channel.send('No player found.')

return;
Expand All @@ -228,7 +228,7 @@ client.on('messageCreate', async (message) => {
if (commandName == 'resume') {
const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) {
if (player.playerCreated() === false) {
message.channel.send('No player found.')

return;
Expand All @@ -244,7 +244,7 @@ client.on('messageCreate', async (message) => {
if (commandName == 'skip') {
const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) {
if (player.playerCreated() === false) {
message.channel.send('No player found.')

return;
Expand All @@ -261,7 +261,7 @@ client.on('messageCreate', async (message) => {
if (commandName == 'stop') {
const player = new FastLink.player.Player(message.guild.id)

if (player.playerCreated() == false) {
if (player.playerCreated() === false) {
message.channel.send('No player found.')

return;
Expand Down
104 changes: 67 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ const Event = new event()
*/
function connectNodes(nodes, config) {
if (!nodes) throw new Error('No nodes provided.')
if (typeof nodes != 'object') throw new Error('Nodes must be an array.')
if (typeof nodes !== 'object') throw new Error('Nodes must be an array.')

if (!config) throw new Error('No config provided.')
if (typeof config != 'object') throw new Error('Config must be an object.')
if (typeof config !== 'object') throw new Error('Config must be an object.')

if (!config.botId) throw new Error('No botId provided.')
if (typeof config.botId != 'string') throw new Error('BotId must be a string.')
if (typeof config.botId !== 'string') throw new Error('BotId must be a string.')

if (!config.shards) throw new Error('No shards provided.')
if (typeof config.shards != 'number') throw new Error('Shards must be a number.')
if (typeof config.shards !== 'number') throw new Error('Shards must be a number.')

if (config.queue && typeof config.queue != 'boolean') throw new Error('Queue must be a boolean.')
if (config.queue && typeof config.queue !== 'boolean') throw new Error('Queue must be a boolean.')

Config = {
botId: config.botId,
shards: config.shards,
queue: config.queue || false
queue: config.queue ?? false
}

nodes.forEach((node) => {
if (!node.hostname) throw new Error('No hostname provided.')
if (typeof node.hostname != 'string') throw new Error('Hostname must be a string.')
if (typeof node.hostname !== 'string') throw new Error('Hostname must be a string.')

if (!node.password) throw new Error('No password provided.')
if (typeof node.password != 'string') throw new Error('Password must be a string.')
if (typeof node.password !== 'string') throw new Error('Password must be a string.')

if (typeof node.secure != 'boolean') throw new Error('Secure must be a boolean.')
if (typeof node.secure !== 'boolean') throw new Error('Secure must be a boolean.')

if (!node.port) node.port = 2333

Expand Down Expand Up @@ -100,13 +100,13 @@ function connectNodes(nodes, config) {
* @returns The boolean if any node is connected or not.
*/
function anyNodeAvailable() {
return Object.values(Nodes).filter((node) => node?.connected).length == 0 ? false : true
return Object.values(Nodes).filter((node) => node?.connected).length === 0 ? false : true
}

function getRecommendedNode() {
const nodes = Object.values(Nodes).filter((node) => node?.connected)

if (nodes.length == 0) throw new Error('No node connected.')
if (nodes.length === 0) throw new Error('No node connected.')

return nodes.sort((a, b) => (a.stats.systemLoad / a.stats.cores) * 100 - (b.stats.systemLoad / b.stats.cores) * 100)[0]
}
Expand All @@ -125,7 +125,7 @@ class Player {
*/
constructor(guildId) {
if (!guildId) throw new Error('No guildId provided.')
if (typeof guildId != 'string') throw new Error('GuildId must be a string.')
if (typeof guildId !== 'string') throw new Error('GuildId must be a string.')

this.guildId = guildId
this.node = Players[this.guildId]?.node
Expand Down Expand Up @@ -176,13 +176,13 @@ class Player {
*/
connect(voiceId, options, sendPayload) {
if (!voiceId) throw new Error('No voiceId provided.')
if (typeof voiceId != 'string') throw new Error('VoiceId must be a string.')
if (typeof voiceId !== 'string') throw new Error('VoiceId must be a string.')

if (!options) options = {}
if (typeof options != 'object') throw new Error('Options must be an object.')
if (typeof options !== 'object') throw new Error('Options must be an object.')

if (!sendPayload) throw new Error('No sendPayload provided.')
if (typeof sendPayload != 'function') throw new Error('SendPayload must be a function.')
if (typeof sendPayload !== 'function') throw new Error('SendPayload must be a function.')

Players[this.guildId].connected = !!voiceId

Expand All @@ -191,8 +191,8 @@ class Player {
d: {
guild_id: this.guildId,
channel_id: voiceId,
self_mute: options.mute || false,
self_deaf: options.deaf || false
self_mute: options.mute ?? false,
self_deaf: options.deaf ?? false
}
})
}
Expand All @@ -206,7 +206,7 @@ class Player {
*/
loadTrack(search) {
if (!search) throw new Error('No search provided.')
if (typeof search != 'string') throw new Error('Search must be a string.')
if (typeof search !== 'string') throw new Error('Search must be a string.')

return this.makeRequest(`/loadtracks?identifier=${encodeURIComponent(search)}`, {
method: 'GET'
Expand All @@ -223,7 +223,7 @@ class Player {
*/
loadLyrics(track, lang) {
if (!track) throw new Error('No track provided.')
if (typeof track != 'string') throw new Error('Track must be a string.')
if (typeof track !== 'string') throw new Error('Track must be a string.')

if (lang && typeof lang != 'string') throw new Error('Lang must be a string.')

Expand All @@ -241,20 +241,20 @@ class Player {
*/
update(body, noReplace) {
if (!body) throw new Error('No body provided.')
if (typeof body != 'object') throw new Error('Body must be an object.')
if (typeof body !== 'object') throw new Error('Body must be an object.')

if (body.track?.encoded && Config.queue) {
Players[this.guildId].queue.push(body.track.encoded)

if (Players[this.guildId].queue.length != 1 && ((Object.keys(body).length == 1 && body.track.userData) || (Object.keys(body).length != 1)))
if (Players[this.guildId].queue.length !== 1 && ((Object.keys(body).length === 1 && body.track.userData) || (Object.keys(body).length !== 1)))
delete body.track.encoded
} else if (body.track?.encoded === null) Players[this.guildId].queue = []

if (body.tracks?.encodeds) {
if (!Config.queue)
throw new Error('Queue is disabled.')

if (Players[this.guildId].queue.length == 0) {
if (Players[this.guildId].queue.length === 0) {
Players[this.guildId].queue = body.tracks.encodeds

this.makeRequest(`/sessions/${Nodes[this.node].sessionId}/players/${this.guildId}`, {
Expand Down Expand Up @@ -302,7 +302,7 @@ class Player {
*/
updateSession(data) {
if (!data) throw new Error('No data provided.')
if (typeof data != 'object') throw new Error('Data must be an object.')
if (typeof data !== 'object') throw new Error('Data must be an object.')

this.makeRequest(`/sessions/${Nodes[this.node].sessionId}`, {
body: data,
Expand Down Expand Up @@ -331,8 +331,8 @@ class Player {
skipTrack() {
if (!Config.queue) throw new Error('Queue is disabled.')

if (Players[this.guildId].queue.length == 1)
return null
if (Players[this.guildId].queue.length === 1)
return false

Players[this.guildId].queue.shift()

Expand All @@ -348,6 +348,36 @@ class Player {
return Players[this.guildId].queue
}

loop() {
if (!Config.queue) throw new Error('Queue is disabled.')

let loop = null
switch (Players[this.guildId].loop){
case 'track': loop = 'track'; break;
case 'queue': loop = 'queue'; break;
case 'off': loop = 'off'; break;
default: throw new Error('Invalid option. Available options: track, queue, off')
}

return Players[this.guildId].loop = loop
}

shuffle() {
if (!Config.queue) throw new Error('Queue is disabled.')

if (Players[this.guildId].queue.length < 3)
return false

PLayers[this.guild].queue.forEach((_, i) => {
const j = Math.floor(Math.random() * (i + 1))
const temp = Players[this.guildId].queue[i]
Players[this.guildId].queue[i] = Players[this.guildId].queue[j]
Players[this.guildId].queue[j] = temp
})

return Players[this.guildId].queue
}

/**
* Decodes a track.
*
Expand All @@ -357,7 +387,7 @@ class Player {
*/
decodeTrack(track) {
if (!track) throw new Error('No track provided.')
if (typeof track != 'string') throw new Error('Track must be a string.')
if (typeof track !== 'string') throw new Error('Track must be a string.')

return this.makeRequest(`/decodetrack?encodedTrack=${track}`, {
method: 'GET'
Expand All @@ -373,7 +403,7 @@ class Player {
*/
decodeTracks(tracks) {
if (!tracks) throw new Error('No tracks provided.')
if (typeof tracks != 'object') throw new Error('Tracks must be an array.')
if (typeof tracks !== 'object') throw new Error('Tracks must be an array.')

return this.makeRequest(`/decodetracks`, {
body: tracks,
Expand Down Expand Up @@ -453,7 +483,7 @@ class Player {
*/
function getPlayers(node) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

Expand All @@ -469,7 +499,7 @@ function getPlayers(node) {
*/
function getInfo(node) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

Expand All @@ -485,7 +515,7 @@ function getInfo(node) {
*/
function getStats(node) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

Expand All @@ -501,7 +531,7 @@ function getStats(node) {
*/
function getVersion(node) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

Expand All @@ -517,7 +547,7 @@ function getVersion(node) {
*/
function getRouterPlannerStatus(node) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

Expand All @@ -534,12 +564,12 @@ function getRouterPlannerStatus(node) {
*/
function unmarkFailedAddress(node, address) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

if (!address) throw new Error('No address provided.')
if (typeof address != 'string') throw new Error('Address must be a string.')
if (typeof address !== 'string') throw new Error('Address must be a string.')

return utils.makeNodeRequest(Nodes, node, `/v4/routerplanner/free/address?address=${encodeURIComponent(address)}`, {
method: 'GET',
Expand All @@ -556,7 +586,7 @@ function unmarkFailedAddress(node, address) {
*/
function unmarkAllFailedAddresses(node) {
if (!node) throw new Error('No node provided.')
if (typeof node != 'string') throw new Error('Node must be a string.')
if (typeof node !== 'string') throw new Error('Node must be a string.')

if (!Nodes[node]) throw new Error('Node does not exist.')

Expand Down Expand Up @@ -595,7 +625,7 @@ function handleRaw(data) {
}

case 'VOICE_STATE_UPDATE': {
if (data.d.member.user.id != Config.botId) return;
if (data.d.member.user.id !== Config.botId) return;

vcsData[data.d.guild_id] = {
...vcsData[data.d.guild_id],
Expand All @@ -621,7 +651,7 @@ function handleRaw(data) {

case 'GUILD_CREATE': {
data.d.voice_states.forEach((state) => {
if (state.user_id != Config.botId) return;
if (state.user_id !== Config.botId) return;

vcsData[data.d.id] = {
...vcsData[data.d.id],
Expand Down
2 changes: 1 addition & 1 deletion src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function close(Event, ws, node, config, Nodes, Players) {
Nodes[node.hostname] = null

Object.keys(Players).forEach((key) => {
if (Players[key].node == node.hostname)
if (Players[key].node === node.hostname)
delete Players[key]
})

Expand Down

0 comments on commit 91bb6d0

Please sign in to comment.