Discord Player
Creating a music bot

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:

  1. Command Definition: We will define a new slash command named nowplaying using SlashCommandBuilder.
  2. Queue Retrieval: We will use the useQueue hook to get the current queue.
  3. Track Check: We will check if there is a currently playing song.
  4. 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.

now-playing.js
import { SlashCommandBuilder } from 'discord.js';
import { useQueue } from 'discord-player';
 
export const data = 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 nowplaying using SlashCommandBuilder.
  • We use the useQueue hook 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.

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.

On this page