spotify-discord-player
v1.0.0-beta.2
Published
A Spotify music player for Discord bots with full track and playlist support
Maintainers
Readme
Spotify Discord Player
A powerful and easy-to-use Spotify music player for Discord bots with full track and playlist support.
Installation
npm install spotify-discord-playerNote: This package uses opusscript for audio encoding. If you prefer better performance, you can optionally install @discordjs/opus instead.
Features
- 🎵 Play Spotify tracks and playlists
- 📋 Queue management with shuffle and loop support
- 🎚️ Basic player controls (play, pause, resume, stop)
- 🔄 Event-based architecture
- 📱 TypeScript support
Prerequisites
- Node.js 16.9.0 or newer
- Discord.js v14
- A Spotify Developer account and application
- A Discord bot token
Setup
- Create a Spotify application at Spotify Developer Dashboard
- Get your Spotify Client ID and Client Secret
- Create a Discord bot at Discord Developer Portal
- Enable the voice permissions for your bot
Usage
Here's a basic example of how to use the player:
const { Client, GatewayIntentBits } = require('discord.js');
const {
PlayerManager ,
SpotifyAPI,
Track
} = require('spotify-discord-player');
const { joinVoiceChannel } = require('@discordjs/voice');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
})
const playerManager = new PlayerManager();
const spotify = new SpotifyAPI('YOUR_SPOTIFY_CLIENT_ID', 'YOUR_SPOTIFY_CLIENT_SECRET');
client.once('ready', () => {
console.log('Bot is ready!');
spotify.initialize();
});
client.on('messageCreate, async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!play')) {
// Get the Spotify track or playlist URL from the message
const url = message.content.split(' ')[1];
// Join the voice channel
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('You must be in a voice channel to use this command.');
}
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
})
// Get or create a player for the voice channel
let player = playerManager.get(message.guildId);
if (!player) {
player = playerManager(message.guildId, connection);
}
try {
// Extract Sptofiy ID from URL
const trackId = url.split('/')[4];
const trackData = await spotify.getTrack(trackId);
const track = new Track(trackData);
// Add the track to the player queue and play it
player.getQueue().add(track);
if (!player.getQueue().nowPlaying) {
await player.play(track);
message.reply(`Now playing: ${track.title}`);
} else {
message.reply(`Added to queue: ${track.title}`);
}
} catch (error) {
console.error('Error playing track:', error);
message.reply('An error occurred while playing the track. Please try again.');
}
}
})
client.login(process.env.DISCORD_TOKEN);Events
The player emits several events you can listen to:
player.on('trackStart', (track) => {
console.log(`Now playing: ${track.title}`);
});
player.on('trackEnd', (track) => {
console.log(`${track.title} has ended.`);
});
player.on('queueEnd', () => {
console.log('Queue has ended.');
});
player.on('error', (error) => {
console.error('An error occurred:', error);
});API Reference
PlayerManager
create(guildId: string, connection: VoiceConnection): Playerget(guildId: string): Player | undefineddelete(guildId: string): boolean
Player
play(track: Track): Promise<void>pause(): booleanresume(): booleanstop(): booleangetQueue(): Queue
Queue
add(track: Track | Track[]): voidremove(index: number): Track | undefinedclear(): voidshuffle(): voidnext(): Track | nullsize: numbernowPlaying: Track | null
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
