Now Playing Command
Learn how to create a now playing command in your Discord bot
In this guide, we will learn how to create a now playing command in your Discord bot using Discord Player. The now playing command will display the currently playing song in the voice channel. We will cover two approaches: using player context and without using player context.
Design Planning
Before we dive into the code, let's plan the design of our now playing command:
- Command Definition: We will define a new slash command named
nowplayingusingSlashCommandBuilder. - Queue Retrieval: We will use the
useQueuehook to get the current queue. - Track Check: We will check if there is a currently playing song.
- Response: We will send a message with the currently playing song information.
With Player Context
Using player context allows Discord Player to automatically detect the queue for the current interaction. This simplifies the process as you don't need to manually provide the guild information.
import { SlashCommandBuilder } from 'discord.js';
import { useQueue } from 'discord-player';
export const command = new SlashCommandBuilder()
.setName('nowplaying') // Command name
.setDescription('Display the currently playing song'); // Command description
export async function execute(interaction) {
// Get the current queue
const queue = useQueue();
if (!queue) {
return interaction.reply(
'This server does not have an active player session.',
);
}
// Get the currently playing song
const currentSong = queue.currentTrack;
// Check if there is a song playing
if (!currentSong) {
return interaction.reply('No song is currently playing.');
}
// Send the currently playing song information
return interaction.reply(`Now playing: ${currentSong.name}`);
}In this example:
- We define a new slash command named
nowplayingusingSlashCommandBuilder. - We use the
useQueuehook to get the current queue. - If there is no active player session, we reply with an appropriate message.
- We check if there is a currently playing song and reply with its name.
Without Player Context
Without player context, you need to provide the guild to the useQueue hook to get the queue for the guild. This approach requires a bit more manual handling but is useful in scenarios where player context is not available.
import { SlashCommandBuilder } from 'discord.js';
import { useQueue } from 'discord-player';
export const command = new SlashCommandBuilder()
.setName('nowplaying') // Command name
.setDescription('Display the currently playing song'); // Command description
export async function execute(interaction) {
// Get the queue for the guild
const queue = useQueue(interaction.guild);
if (!queue) {
return interaction.reply(
'This server does not have an active player session.',
);
}
// Get the currently playing song
const currentSong = queue.currentTrack;
// Check if there is a song playing
if (!currentSong) {
return interaction.reply('No song is currently playing.');
}
// Send the currently playing song information
return interaction.reply(
`Now playing: ${currentSong.name}\n${queue.node.createProgressBar()}`,
);
}In this example:
- We define the same
nowplayingcommand. - We use the
useQueuehook with the guild information to get the queue. - The rest of the logic remains the same as the previous example.
By following this guide, you will be able to create a now playing command in your Discord bot that displays the currently playing song in the voice channel. Choose the approach that best fits your use case and integrate it into your bot.