Discord Player
Creating a music bot

Creating a Skip Command

Learn how to create a skip command for your music bot

In this guide, we will learn how to create a skip command in your Discord bot using Discord Player. The skip command will skip the currently playing song in the voice channel.

Design Planning

Before we dive into the code, let's plan the design of our skip command:

  1. Command Definition: We will define a new slash command named skip using SlashCommandBuilder.
  2. Queue Retrieval: We will use the useQueue hook to get the current queue.
  3. Playing Check: We will check if there is a track currently playing.
  4. Skipping: We will skip the current track using the skip method.
  5. Response: We will send a confirmation message indicating the track has been skipped.
skip.js
import { SlashCommandBuilder } from 'discord.js';
import { useQueue } from 'discord-player';
 
export const data = new SlashCommandBuilder()
  .setName('skip') // Command name
  .setDescription('Skip 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.');
  }
 
  if (!queue.isPlaying()) {
    return interaction.reply('There is no track playing.');
  }
 
  // Skip the current track
  queue.node.skip();
 
  // Send a confirmation message
  return interaction.reply('The current song has been skipped.');
}

In this example:

  • We define a new slash command named skip 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 skip the current track using the skip method.
  • We send a confirmation message after skipping the current song.

On this page