Skip to content
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

[Feature] Adds a voice channel status with the currently playing song title! #482

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/commands/music/pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 update
client.musicManager.emit('playerPaused', player, player.trackData)
return "⏸️ Paused the music player.";
}
3 changes: 3 additions & 0 deletions src/commands/music/resume.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 update
client.musicManager.emit('playerResumed', player, player.trackData)
return "▶️ Resumed the music player";
}
3 changes: 3 additions & 0 deletions src/commands/music/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
37 changes: 36 additions & 1 deletion src/handlers/lavaclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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()
Expand Down Expand Up @@ -72,12 +72,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}**`, client)
});

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, '', 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)
})
// for when player is resumed, indicate 'playing' in the status
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)
})
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`;
await client.rest.put(url, {
body: {
status: status
}
})
.catch(() => { });
}