npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord.js-player

v1.1.2

Published

Play music and other tracks

Downloads

4

Readme

Discord.js-Player

Using npm Framework discord.js

Requires a Spotify API Token

discord.js-player

Recent Change:

  • Recently updated to connect with discord.js@^13.1.0
  • Added upload media playing, simply type the command and attach a file or link the media to play

Install

Install discord.js-player

$ npm install discord.js-player

Install FFmpeg

Create a Spotify application and copy-paste your spotify id and secret

Project Setup

Using the discord.js framework, here is an example of code to play a track.

const { Client, Intents} = require("discord.js");
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_PRESENCES] });
const TOKEN = "";
const PREFIX = "?";
client.login(TOKEN).then(_=>console.log("logged in!"));


//Music Setup
const { Player, EVENTS } = require("..");
const { EVT_TRACK_START, EVT_TRACK_ADD } = EVENTS;
const YOUR_SPOTIFY_ID = "";
const YOUR_SPOTIFY_SECRET = "";

// Create a new Player (you need a Spotify ID/Secret)
client.music = new Player(
    YOUR_SPOTIFY_ID, 
    YOUR_SPOTIFY_SECRET, 
    {canUseCache: true}
);
client.music.connect();

//Optional Events
client.music.on(EVT_TRACK_START, (channel, track) => {
    channel.send(`Track ${track.title} started playing!`);
});
client.music.on(EVT_TRACK_ADD, (channel, tracks) => {
    channel.send(`Added ${tracks.length} to the queue!`);
});

client.on("messageCreate", async (message) => {
    if (!message.content.startsWith(PREFIX)) return;

    const args = message.content.slice(PREFIX.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    //!play https://open.spotify.com/track/2Tax7fSPDly9OLIAZRd0Dp?si=i4825VV5THG_F-RNXBp8zA
    //!play https://www.youtube.com/watch?v=_LgTsA9-kyM
    //!play Through The Dark Alexi
    //will get track Through The Dark by Alexi Murdoch and play it
    if (command === "play") {

        //Make sure you are connected to a voice channel
        const voiceChannel = message.member.voice.channel;
        let queue = client.music.getQueue(message.guild.id);

        //If no queue, one will be created
        if (!queue) queue = client.music.createQueue(
            message.guild.id, 
            message.channel, 
            voiceChannel, 
            [], 
            {emit: {trackStart: true}}
        );

        const search = (message.attachments.first())?.url ?? args.join(' ');

        //addedBy is optional
        client.music.play(
            queue.id, 
            search, 
            {addedBy: message.author.username}
        );

    } else if (command === "queue") {
        const shownQueue = client.music.getQueue(message.guild.id).showQueue({
            limit: 10,
            show: {
                queueNumber: true,
                addedBy: true,
                align: true,
                alignmentSpace: 70
            }
        })
        await message.channel.send(shownQueue.join('\n'));
    }
    else if (command === 'skip') {
        client.music.getQueue(message.guild.id).skipTrack();
        message.channel.send("skipped!");

    } else if (command === "volume") {
        client.music.getQueue(message.guild.id).setVolume(args[0]);
        message.channel.send("Volume changed to " + args[0]);

    } else if (command === "leave") {
        client.music.getQueue(message.guild.id).leave();
    }
})

Additional Methods

  • Player
Player.deleteQueue(QueueID)
  • Queue
Queue.loop()
Queue.loopQueue()

Queue.skipTrack(numberOfTracksToSkip)
Queue.shuffle()

Queue.play()
Queue.pause()
Queue.resume()
Queue.setVolume(number) //~ 1-200 for volume
Queue.skip(number)

//Handled automatically on Player.play()
Queue.join()
Queue.leave()