README
drg-music2

A simple to use framework to create and manage music playlists for Discord using discord.js.
How to use ?
You need to create a new MusicHandler.
const discord = require("discord.js");
const client = new discord.Client();
const MusicHandler = require("drg-music2");
const music = new MusicHandler(client);
Then, you'll need to interact with the MusicHandler you just created.
Classes
This module lets you interact with 2 different classes.
MusicHandler extends EventEmitter
This is the main class.
Attributes
client (read-only)
The Client used to initialize this MusicHandler.guilds
A Collection containing all Guilds where the Client is currently playing music, mapped by their ID.channels
A Collection containing all Guilds where the Client is currently playing music, mapped by their ID.playlists
A Collection containing all Playlists from the Guilds where the Client is currently playing music, , mapped by the corresponding ID.
Methods
join
music.join(tojoin);tojoin: the GuildMember or VoiceChannel to joinReturns: Promise<VoiceConnection>
leave
music.leave(guild);guild: the Guild to leaveReturns: Promise
add
music.add(request, member, options);request: Youtube link/Youtube query/path to a local filemember: the GuildMember who requested the musicoptionsis optionaloptions.type: 'link', 'ytquery' or 'file'options.passes: how many times to send the voice packet to reduce packet lossReturns: Promise<MusicInfo (added music)>
remove
music.remove(guild, index);guild: Guildindex: the index of the music in the playlist (starting at 0)Returns: Promise<MusicInfo (removed music)>
playNext
skip
Alias for playNextclear
shuffle
resume
pause
togglePaused
toggleLooping
togglePlaylistLooping
getVolume
setVolume
isConnected
isPlaying
isPaused
isLooping
isPlaylistLooping
currentInfo
playlistInfo
Static methods
videoWebsite
playYoutube
youtubeInfo
queryYoutube
Events
clientMove: emitted when the Client that instantiated this MusicHandler moves from one VoiceChannel to another
Returns: old VoiceChannel and new VoiceChannelmemberJoin: emitted when a GuildMember joins a VoiceChannel where the Client is playing music
Returns: VoiceChannel and GuildMembermemberLeave: emitted when a GuildMember leaves a VoiceChannel where the Client is playing music
Returns: VoiceChannel and GuildMemberstart: emitted when the current music starts playing
Returns: Playlist and MusicInfo (current music)end: emitted when the current music ends
Returns: Playlist and MusicInfo (current music)next: emitted when the playlist switches to the next music
Returns: Playlist and MusicInfo (next music)empty: emitted when switching to the next music and the playlist is empty
Returns: Playlist
Playlist
This class does not really store information and is more of an alias but in some cases it can be useful.
Attributes
guild (read-only)
Guild represented by this Playlistchannel (read-only)
VoiceChannel joined by the ClientfirstJoinedAt (read-only)
Date representing the first time the Client joined a VoiceChannel in this GuildfirstJoinedTimestamp (read-only)
Alias for firstJoinedAt.getTime()lastJoinedAt (read-only)
Date representing the last time the Client joined a VoiceChannel in this GuildlastJoinedTimestamp (read-only)
Alias for lastJoinedAt.getTime()connected (read-only)
Whether or not the Client is connected in this Guildplaying (read-only)
Whether or not the Client is currently playing musicpaused
Whether or not the Client is pausedlooping
Whether or not the Client is looping the current musicplaylistLooping
Whether or not the Client is looping the Playlistcurrent (read-only)
Information about the current musicinfo (read-only)
Information about all the musics in this Playlistvolume
The volume at which music is played
Methods
Those methods are just an alias for the methods with the same name in MusicHandler, except you don't need to precise the guild as a parameter when it is required. Example: music.shuffle(guild) <==> music.playlists.get(guild.id).shuffle()
join
Alias for MusicHandler.prototype.joinleave
Alias for MusicHandler.prototype.leaveadd
Alias for MusicHandler.prototype.addremove
Alias for MusicHandler.prototype.removeplayNext
Alias for MusicHandler.prototype.playNextskip
Alias for MusicHandler.prototype.skipclear
Alias for MusicHandler.prototype.clearshuffle
Alias for MusicHandler.prototype.shuffle
Example
const discord = require("discord.js");
const client = new discord.Client();
const MusicHandler = require("drg-music2");
const music = new MusicHandler(client);
client.on("message", message => {
if (message.content == "/join") {
if (music.isConnected(message.guild)) {
message.reply("I am already connected!");
return;
}
music.join(message.member).then(() => {
message.reply("hello!");
}).catch(console.error);
}
else if (message.content == "/leave") {
if (!music.isConnected(message.guild)) {
message.reply("I am not connected!");
return;
}
music.leave(message.guild).then(() => {
message.reply("bye!");
}).catch(console.error);
}
else if (message.content.startsWith("/request ")) {
if (!music.isConnected(message.guild)) {
message.reply("I am not connected!");
return;
}
let youtubeLink = message.content.replace("/request ", "");
music.add(youtubeLink, message.member).then(added => {
message.reply(added.title + " has been added to the playlist!");
}).catch(console.error);
}
});
client.login("MYAWESOMEBOTTOKEN");