Slash Commands API β
Command β
A command extends the default command type provided by discord.js
with a run
method that encapsulates the logic of each slash command.
Type
tsexport interface Command<T = boolean> extends ChatInputApplicationCommandData { run: (interaction: ChatInputCommandInteraction<"cached">) => Promise<T>; }
Types
ChatInputApplicationCommandData
andChatInputCommandInteraction
are imported fromdiscord.js
.By default all commands expect the
run
method to return a Promise with a boolean that indicates whether the command has been executed successfully.Example
tsimport { PlayerManager, Command } from "discord-player-plus"; const playerManager = new PlayerManager(); const pauseCommand: Command = { name: "pause", description: "Pauses the player.", run: async (interaction) => { const player = playerManager.find(interaction.guildId); if (!player) { await interaction.reply({ content: "π€ I am currently not playing anything.", ephemeral: true, }); return false; } const paused = player.setPause(true); if (!paused) { await interaction.reply({ content: "β Unable to pause current song.", ephemeral: true, }); return false; } await interaction.reply({ content: "βΈοΈ Song paused.", }); return true; }, };
handleSlashCommand() β
Helper function to execute slash commands for a given interaction from discord.js. Will check if command is supported, calls its
run
method and handles unexpected errors.Type
tsimport { Interaction } from "discord.js"; /** * @param interaction discord.js interaction received by `client.on("interactionCreate")`. * @param commands All available slash commands that should be executable. * @param translations Translations used for error messages (e.g. unknown command). */ async function handleSlashCommand( interaction: Interaction, commands: Command[], translations: Translations ): Promise<void>;
- Example
tsimport { Command, handleSlashCommand, PlayerManager, } from "discord-player-plus"; import { Client, GatewayIntentBits } from "discord.js"; const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates, ], }); const playerManager = new PlayerManager(); // add commands here, e.g. pre-build commands from discord-player-plus or custom commands const slashCommands: Command[] = []; client.on("ready", async (client) => { console.log(`Bot ready and logged in as ${client.user.tag}`); await client.application.commands.set(slashCommands); }); client.on("interactionCreate", async (interaction) => { await handleSlashCommand( interaction, slashCommands, playerManager.translations ); });
pre-build commands β
createAddCommand() β
Creates a /add
command for adding tracks to the queue.
Type
tsfunction createAddCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createAddCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createAddCommand(playerManager)];
createClearCommand() β
Creates a /clear
command for clearing queued tracks.
Type
tsfunction createClearCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createClearCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createClearCommand()];
createHelpCommand() β
Creates a /help
command for showing all available commands and bot metadata.
Type
tsfunction createHelpCommand( playerManager: PlayerManager, options?: CreateHelpCommandOptions ): Command;
For available options see CreateHelpCommandOptions.
- Example
tsimport { Command, createHelpCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); // other commands const slashCommands: Command[] = []; slashCommands.push( createHelpCommand(playerManager, { commands: slashCommands, title: "Example bot for discord-player-plus", url: "https://github.com/larsrickert/discord-player-plus", description: "This an example help command to show you all available commands.", author: { name: "Lars Rickert", url: "https://lars-rickert.de", iconUrl: "https://avatars.githubusercontent.com/u/67898185?v=4", }, footerText: "Thanks for using discord-player-plus", }) );
createInsertCommand() β
Creates a /insert
command for adding a track to a specific position in the queue. If the user query searches a playlist instead of a single track, only the first track will be inserted.
Type
tsfunction createInsertCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createInsertCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createInsertCommand()];
createJumpCommand() β
Creates a /jump
command for jumping to a specific track inside the queue (will skip all songs before the given track).
Type
tsfunction createJumpCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createJumpCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createJumpCommand()];
createPauseCommand() β
Creates a /pause
command for pausing the current track.
Type
tsfunction createPauseCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createPauseCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createPauseCommand()];
createPlayCommand() β
Creates a /play
command for immediately searching and playing a track. If query is a track, will play first result. If its a playlist, first track will be played and rest will be queued.
Type
tsfunction createPlayCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createPlayCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createPlayCommand()];
createQueueCommand() β
Creates a /queue
command for displaying queued tracks.
Type
tsfunction createQueueCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createQueueCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createQueueCommand()];
createRemoveCommand() β
Creates a /remove
command for removing tracks from the queue.
Type
tsfunction createRemoveCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createRemoveCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createRemoveCommand()];
createResumeCommand() β
Creates a /resume
command for resuming the currently paused track.
Type
tsfunction createResumeCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createResumeCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createResumeCommand()];
createShuffleCommand() β
Creates a /shuffle
command for shuffling the queue.
Type
tsfunction createShuffleCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createShuffleCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createShuffleCommand()];
createSkipCommand() β
Creates a /skip
command for skipping the current track.
Type
tsfunction createSkipCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createSkipCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createSkipCommand()];
createSongCommand() β
Creates a /song
command for getting information about the currently played track.
Type
tsfunction createSongCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createSongCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createSongCommand()];
createStopCommand() β
Creates a /stop
command for stopping the playback and leaving the voice channel.
Type
tsfunction createStopCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createStopCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createStopCommand()];
createSetVolumeCommand() β
Creates a /setvolume
command for setting the player volume between 0 and 200.
Type
tsfunction createSetVolumeCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createSetVolumeCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createSetVolumeCommand()];
createRepeatCommand() β
Creates a /repeat
command for setting the repeat mode.
Type
tsfunction createRepeatCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createRepeatCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createRepeatCommand()];
createSeekCommand() β
Creates a /seek
command for seeking the current track to a specific duration.
Type
tsfunction createSeekCommand( playerManager: PlayerManager, options?: CreateCommandOptions ): Command;
For available options see CreateCommandOptions.
- Example
tsimport { Command, createSeekCommand, PlayerManager, } from "discord-player-plus"; const playerManager = new PlayerManager(); const slashCommands: Command[] = [createSeekCommand()];
Interfaces β
Options for creating pre-build commands from discord-player-plus
.
CreateCommandOptions β
export interface CreateCommandOptions {
/**
* Whether messages should only be visible for the user that executed the command.
*
* @default `false`
*/
ephemeral?: boolean;
/**
* Whether error messages should only be visible for the user that executed the command.
*
* @default `true`
*/
ephemeralError?: boolean;
}
CreateHelpCommandOptions β
export interface CreateHelpCommandOptions {
/** Title that is being displayed at the top of the help prompt. Markdown supported. */
title?: string;
/** URL that the title should be linked to. */
url?: string;
/** Description/text inside the help prompt. Markdown supported. */
description?: string;
/** Commands that the help prompt should be generated for. Will be sorted alphabetically. */
commands: Command[];
/** Information about the author that developed the discord bot. */
author?: {
name: string;
iconUrl?: string;
url?: string;
};
/** Footer text, e.g. to specify the discord bot version. */
footerText?: string;
}