From 60127c8a9bf82fb0fc02483aef5ab3ce02aeac49 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:03:14 +0530 Subject: [PATCH 1/8] added vocie channel status change with current playing song --- package-lock.json | 37 +++++++++++++++++++++++++++++++++ package.json | 1 + src/commands/music/pause.js | 2 ++ src/commands/music/resume.js | 3 +++ src/commands/music/stop.js | 3 +++ src/handlers/lavaclient.js | 40 ++++++++++++++++++++++++++++++++++-- 6 files changed, 84 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 84860170e..3d70e41c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@lavaclient/spotify": "^3.1.0", "@vitalets/google-translate-api": "^9.2.0", "ascii-table": "0.0.9", + "axios": "^1.6.7", "btoa": "^1.2.1", "common-tags": "^1.8.2", "connect-mongo": "^5.1.0", @@ -1986,6 +1987,16 @@ "node": ">=8.0.0" } }, + "node_modules/axios": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", + "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "dependencies": { + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2554,6 +2565,7 @@ "version": "0.1.4", "resolved": "https://registry.npmjs.org/erlpack/-/erlpack-0.1.4.tgz", "integrity": "sha512-CJYbkEvsB5FqCCu2tLxF1eYKi28PvemC12oqzJ9oO6mDFrFO9G9G7nNJUHhiAyyL9zfXTOJx/tOcrQk+ncD65w==", + "hasInstallScript": true, "license": "MIT", "optional": true, "dependencies": { @@ -3123,6 +3135,25 @@ "dev": true, "license": "ISC" }, + "node_modules/follow-redirects": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", + "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/form-data": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", @@ -4400,6 +4431,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", @@ -5463,6 +5499,7 @@ "version": "0.1.9", "resolved": "https://registry.npmjs.org/zlib-sync/-/zlib-sync-0.1.9.tgz", "integrity": "sha512-DinB43xCjVwIBDpaIvQqHbmDsnYnSt6HJ/yiB2MZQGTqgPcwBSZqLkimXwK8BvdjQ/MaZysb5uEenImncqvCqQ==", + "hasInstallScript": true, "license": "MIT", "optional": true, "dependencies": { diff --git a/package.json b/package.json index 724f5e389..841ce33d4 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@lavaclient/spotify": "^3.1.0", "@vitalets/google-translate-api": "^9.2.0", "ascii-table": "0.0.9", + "axios": "^1.6.7", "btoa": "^1.2.1", "common-tags": "^1.8.2", "connect-mongo": "^5.1.0", diff --git a/src/commands/music/pause.js b/src/commands/music/pause.js index 180bf74ba..510cec5cf 100644 --- a/src/commands/music/pause.js +++ b/src/commands/music/pause.js @@ -34,5 +34,7 @@ function pause({ client, guildId }) { if (player.paused) return "The player is already paused."; player.pause(true); + // emiting a new event 'playerPaused' to handle voice channel status reset + client.musicManager.emit('playerPaused', player, player.trackData) return "⏸️ Paused the music player."; } diff --git a/src/commands/music/resume.js b/src/commands/music/resume.js index 4d81aceb1..ffd0210ae 100644 --- a/src/commands/music/resume.js +++ b/src/commands/music/resume.js @@ -33,5 +33,8 @@ function resumePlayer({ client, guildId }) { const player = client.musicManager.getPlayer(guildId); if (!player.paused) return "The player is already resumed"; player.resume(); + + // emiting a new event 'playerResume' to handle voice channel status reset + client.musicManager.emit('playerResumed', player, player.trackData) return "▶️ Resumed the music player"; } diff --git a/src/commands/music/stop.js b/src/commands/music/stop.js index 60277209d..b0ef13862 100644 --- a/src/commands/music/stop.js +++ b/src/commands/music/stop.js @@ -34,5 +34,8 @@ async function stop({ client, guildId }) { const player = client.musicManager.getPlayer(guildId); player.disconnect(); await client.musicManager.destroyPlayer(guildId); + + // emiting a new event 'playerDestroy' to handle voice channel status reset + client.musicManager.emit('playerDestroy', player) return "🎶 The music player is stopped and queue has been cleared"; } diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index af1364d71..02e970be6 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -1,5 +1,6 @@ const { EmbedBuilder } = require("discord.js"); const { Cluster } = require("lavaclient"); +const axios = require("axios"); const prettyMs = require("pretty-ms"); const { load, SpotifyItemType } = require("@lavaclient/spotify"); require("@lavaclient/queue/register"); @@ -41,7 +42,7 @@ module.exports = (client) => { client.logger.debug(`Node "${node.id}" debug: ${message}`); }); - lavaclient.on("nodeTrackStart", (_node, queue, song) => { + lavaclient.on("nodeTrackStart", async (_node, queue, song) => { const fields = []; const embed = new EmbedBuilder() @@ -72,12 +73,47 @@ module.exports = (client) => { embed.setFields(fields); queue.data.channel.safeSend({ embeds: [embed] }); + + // update voice channel status with 'Now Playing' + await client.wait(1000) // waiting 1 sec, because channel id is null initially + await updateVoiceStatus(queue.player.channelId, `Playing **${song.title}**`) }); lavaclient.on("nodeQueueFinish", async (_node, queue) => { queue.data.channel.safeSend("Queue has ended."); await client.musicManager.destroyPlayer(queue.player.guildId).then(queue.player.disconnect()); - }); + // reset voice channel's status + await updateVoiceStatus(queue.player.channelId, '') + }); + + // for when player is paused, indicate 'paused' in the status + lavaclient.on('playerPaused', async (player, song) => { + await updateVoiceStatus(player.channelId, `Paused **${song.title}**`) + }) + // for when player is resumed, indicate 'playing' in the status + lavaclient.on('playerResumed', async (player, song) => { + await updateVoiceStatus(player.channelId, `Playing **${song.title}**`) + }) + // for when player is stopped, reset the status + lavaclient.on('playerDestroy', async (player) => { + await updateVoiceStatus(player.channelId, '') + }) return lavaclient; }; + + +async function updateVoiceStatus(channel, status) { + const url = `https://discord.com/api/v10/channels/${channel}/voice-status`; + const payload = { + status: status + }; + axios.put(url, payload, { + headers: { + Authorization: `Bot ${process.env.BOT_TOKEN}` + } + }) + .catch(error => { + console.error('Error updating VC status:', error.response ? error.response.data : error.message); + }); +} \ No newline at end of file From 1449b2de060dd85e371747ddf6a7df1d9cc9acd4 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:41:11 +0530 Subject: [PATCH 2/8] fixed comments --- src/commands/music/resume.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/music/resume.js b/src/commands/music/resume.js index ffd0210ae..9190c6af2 100644 --- a/src/commands/music/resume.js +++ b/src/commands/music/resume.js @@ -34,7 +34,7 @@ function resumePlayer({ client, guildId }) { if (!player.paused) return "The player is already resumed"; player.resume(); - // emiting a new event 'playerResume' to handle voice channel status reset + // emiting a new event 'playerResume' to handle voice channel status update client.musicManager.emit('playerResumed', player, player.trackData) return "▶️ Resumed the music player"; } From 451438a28780bf6931c9cc21935d9178456e2a87 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sat, 24 Feb 2024 23:41:42 +0530 Subject: [PATCH 3/8] fixed comment --- src/commands/music/pause.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/music/pause.js b/src/commands/music/pause.js index 510cec5cf..f4d672946 100644 --- a/src/commands/music/pause.js +++ b/src/commands/music/pause.js @@ -34,7 +34,7 @@ function pause({ client, guildId }) { if (player.paused) return "The player is already paused."; player.pause(true); - // emiting a new event 'playerPaused' to handle voice channel status reset + // emiting a new event 'playerPaused' to handle voice channel status update client.musicManager.emit('playerPaused', player, player.trackData) return "⏸️ Paused the music player."; } From 80d02be8e5056e0569caf71b3e7dcd083434b107 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 14 Apr 2024 05:41:00 +0530 Subject: [PATCH 4/8] Updated to use restful api --- src/handlers/lavaclient.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index 02e970be6..54f592972 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -76,7 +76,7 @@ module.exports = (client) => { // update voice channel status with 'Now Playing' await client.wait(1000) // waiting 1 sec, because channel id is null initially - await updateVoiceStatus(queue.player.channelId, `Playing **${song.title}**`) + await updateVoiceStatus(queue.player.channelId, `Playing **${song.title}**`, client) }); lavaclient.on("nodeQueueFinish", async (_node, queue) => { @@ -84,36 +84,34 @@ module.exports = (client) => { await client.musicManager.destroyPlayer(queue.player.guildId).then(queue.player.disconnect()); // reset voice channel's status - await updateVoiceStatus(queue.player.channelId, '') + await updateVoiceStatus(queue.player.channelId, '', client) }); // for when player is paused, indicate 'paused' in the status lavaclient.on('playerPaused', async (player, song) => { - await updateVoiceStatus(player.channelId, `Paused **${song.title}**`) + await updateVoiceStatus(player.channelId, `Paused **${song.title}**`, client) }) // for when player is resumed, indicate 'playing' in the status lavaclient.on('playerResumed', async (player, song) => { - await updateVoiceStatus(player.channelId, `Playing **${song.title}**`) + await updateVoiceStatus(player.channelId, `Playing **${song.title}**`, client) }) // for when player is stopped, reset the status lavaclient.on('playerDestroy', async (player) => { - await updateVoiceStatus(player.channelId, '') + await updateVoiceStatus(player.channelId, '', client) }) return lavaclient; }; async function updateVoiceStatus(channel, status) { - const url = `https://discord.com/api/v10/channels/${channel}/voice-status`; + const url = `/channels/${channel}/voice-status`; const payload = { status: status }; - axios.put(url, payload, { - headers: { - Authorization: `Bot ${process.env.BOT_TOKEN}` - } + await client.rest.put(url, { + body: { + status: status + } }) - .catch(error => { - console.error('Error updating VC status:', error.response ? error.response.data : error.message); - }); + .catch(() => {}); } \ No newline at end of file From 01c70079d90b4248b6b4a006e158e4c2c4fc2e9e Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 14 Apr 2024 05:41:48 +0530 Subject: [PATCH 5/8] fix --- src/handlers/lavaclient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index 54f592972..63f810354 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -103,7 +103,7 @@ module.exports = (client) => { }; -async function updateVoiceStatus(channel, status) { +async function updateVoiceStatus(channel, status, client) { const url = `/channels/${channel}/voice-status`; const payload = { status: status From 9a7d9b955556ffea10c4fd3d1d804acd0cb523b6 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 14 Apr 2024 05:44:06 +0530 Subject: [PATCH 6/8] Update package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 841ce33d4..724f5e389 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "@lavaclient/spotify": "^3.1.0", "@vitalets/google-translate-api": "^9.2.0", "ascii-table": "0.0.9", - "axios": "^1.6.7", "btoa": "^1.2.1", "common-tags": "^1.8.2", "connect-mongo": "^5.1.0", From 7cf86036205b65497eaf39a7f3ab922ef96ea52a Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 14 Apr 2024 05:44:22 +0530 Subject: [PATCH 7/8] Update lavaclient.js --- src/handlers/lavaclient.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index 63f810354..b5eafddbf 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -1,6 +1,5 @@ const { EmbedBuilder } = require("discord.js"); const { Cluster } = require("lavaclient"); -const axios = require("axios"); const prettyMs = require("pretty-ms"); const { load, SpotifyItemType } = require("@lavaclient/spotify"); require("@lavaclient/queue/register"); From a42bd28b9685acfcabe16f6646ca4254932eec5f Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Thu, 27 Jun 2024 12:05:18 +0530 Subject: [PATCH 8/8] jsdoc --- src/handlers/lavaclient.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/handlers/lavaclient.js b/src/handlers/lavaclient.js index b5eafddbf..47715e73d 100644 --- a/src/handlers/lavaclient.js +++ b/src/handlers/lavaclient.js @@ -85,32 +85,34 @@ module.exports = (client) => { // reset voice channel's status await updateVoiceStatus(queue.player.channelId, '', client) }); - + // for when player is paused, indicate 'paused' in the status - lavaclient.on('playerPaused', async (player, song) => { - await updateVoiceStatus(player.channelId, `Paused **${song.title}**`, client) + lavaclient.on('playerPaused', async (player, song) => { + await updateVoiceStatus(player.channelId, `Paused **${song.title}**`, client) }) // for when player is resumed, indicate 'playing' in the status - lavaclient.on('playerResumed', async (player, song) => { - await updateVoiceStatus(player.channelId, `Playing **${song.title}**`, client) + lavaclient.on('playerResumed', async (player, song) => { + await updateVoiceStatus(player.channelId, `Playing **${song.title}**`, client) }) // for when player is stopped, reset the status lavaclient.on('playerDestroy', async (player) => { - await updateVoiceStatus(player.channelId, '', client) + await updateVoiceStatus(player.channelId, '', client) }) return lavaclient; }; - +/** + * + * @param {string} channel The channel Id to update the status + * @param {string} status The status + * @param {import("discord.js").Client} client Bot's client + */ async function updateVoiceStatus(channel, status, client) { const url = `/channels/${channel}/voice-status`; - const payload = { - status: status - }; await client.rest.put(url, { body: { status: status - } + } }) - .catch(() => {}); + .catch(() => { }); } \ No newline at end of file