@discordx/music

A powerful discord music library written in TypeScript for Node.js

Usage no npm install needed!

<script type="module">
  import discordxMusic from 'https://cdn.skypack.dev/@discordx/music';
</script>

README

Discord server NPM version NPM downloads Build status paypal

Create a discord bot with TypeScript and Decorators!

📖 Introduction

A powerful discord music library written in TypeScript for Node.js. Support youtube/spotify songs and playlist.

💻 Installation

Version 16.6.0 or newer of Node.js is required

Ensure that discord.js and @discordjs/voice have been installed before installing this library

npm install @discordx/music
yarn add @discordx/music

🤖 Bot Examples

discord-music-bot from @oceanroleplay

Note: Create a pull request to include your bot in the example list.

Define new player

const player = new Player();

Get queue for guild

const queue = player.queue(interaction.guild);

Join voice server

await queue.join(interaction.member.voice.channel);

Play youtube song

const status = await queue.play(songName);
if (!status) {
  interaction.followUp("The song could not be found");
} else {
  interaction.followUp("The requested song is being played");
}

Play youtube playlist

const status = await queue.playlist(playlistLink);
if (!status) {
  interaction.followUp("The playlist could not be found");
} else {
  interaction.followUp("playing requested playlist");
}

Play spotify song/playlist

const status = await queue.spotify(spotifyLink);
if (!status) {
  interaction.followUp("The spotify song/playlist could not be found");
} else {
  interaction.followUp("The requested spotify song/playlist is being played");
}

Get voice config data

const audioPlayer = queue.audioPlayer;
const voiceConnection = queue.voiceConnection;
const voiceChannelId = queue.voiceChannelId;
const voiceGroup = queue.voiceGroup;
const voiceGuildId = queue.voiceGuildId;

Get tracks

const tracks = queue.tracks;

Get loop mode

const state = queue.loop;

Set loop mode

queue.setLoop(true | false);

Get repeat mode

const state = queue.repeat;

Set repeat mode

queue.setRepeat(true | false);

Pause music

queue.pause();

Resume music

queue.resume();

Skip music

queue.skip();

Leave voice channel

queue.leave();

Mix/Shuffle tracks

queue.mix();

Get playback duration

queue.playbackDuration;

Get volume

queue.volume;

Set volume

queue.setVolume(volume: number);

Seek current track

queue.seek(time: number);

Clear queue

queue.clearTracks();

Remove specific tracks

queue.removeTracks([1, 3, 5]);

Get total tracks

const total = queue.size;

Events

this.player.on("onError", console.log);
this.player.on("onFinish", console.log);
this.player.on("onStart", console.log);
this.player.on("onLoop", console.log);
this.player.on("onFinishPlayback", console.log);
this.player.on("onRepeat", console.log);
this.player.on("onSkip", console.log);
this.player.on("onPause", console.log);
this.player.on("onResume", console.log);
this.player.on("onTrackAdd", console.log);
this.player.on("onLoopEnabled", console.log);
this.player.on("onLoopDisabled", console.log);
this.player.on("onRepeatEnabled", console.log);
this.player.on("onRepeatDisabled", console.log);
this.player.on("onMix", console.log);
this.player.on("onVolumeUpdate", console.log);
this.player.on("onSeek", console.log);
this.player.on("onJoin", console.log);
this.player.on("onLeave", console.log);

Custom queue

class MyQueue extends Queue {
  customProp = "custom queue";
}

Tell the player to use this custom queue

class MyQueue extends Queue {}
this.player.queue(guild, () => new MyQueue(this.player, guild));

Custom player

class MyPlayer extends Player {
  // custom player
}

Let your queue know about custom player

const myQueue: Queue<MyPlayer> = new Queue(this, guild);

Lava Player

Getting Started

const node = new Lava.Node({
  host: {
    address: process.env.LAVA_HOST ?? "",
    port: Number(process.env.LAVA_PORT) ?? 2333,
  },

  // your Lavalink password
  password: process.env.LAVA_PASSWORD ?? "",

  send(guildId, packet) {
    const guild = client.guilds.cache.get(guildId);
    if (guild) {
      guild.shard.send(packet);
    }
  },
  shardCount: 0, // the total number of shards that your bot is running (optional, useful if you're load balancing)
  userId: client.user?.id ?? "", // the user id of your bot
});

client.ws.on("VOICE_STATE_UPDATE", (data: Lava.VoiceStateUpdate) => {
  node.voiceStateUpdate(data);
});

client.ws.on("VOICE_SERVER_UPDATE", (data: Lava.VoiceServerUpdate) => {
  node.voiceServerUpdate(data);
});

Get Guild Player

const player = node.players.get("guild id");

Join Voice Channel

await player.join("channel id");

Play Track

const res = await voice.load("ytsearch:monstercat");
await player.play(res.tracks[0]);

Stop Music

await player.stop();
// or, to destroy the player entirely
await player.destroy();

Clustering

const cluster = new Lava.Cluster({
  nodes: [
    // node options here; see above
  ],
  send(guildId, packet) {
    // send to gateway; same as for single node usage
  },
  filter(node, guildId) {
    // optional
    // return a boolean indicating whether the given guild can be run on the given node
    // useful for limiting guilds to specific nodes (for instance, if you setup lavalink edge servers to minimize latency)
    // this must return true at least once for a given set of nodes, otherwise some methods may error
  },
});

☎️ Need help?

Ask in discord server or open a issue

Thank you

Show your support for discordx by giving us a star on github.