A simple to use framework to create and manage music playlists for Discord using discord.js.
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.
This module lets you interact with 2 different classes.
MusicHandler extends EventEmitter
This is the main class.
-
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.
-
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 musicoptions
is 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 playNext -
clear
-
shuffle
-
resume
-
pause
-
togglePaused
-
toggleLooping
-
togglePlaylistLooping
-
getVolume
-
setVolume
-
isConnected
-
isPlaying
-
isPaused
-
isLooping
-
isPlaylistLooping
-
currentInfo
-
playlistInfo
-
videoWebsite
-
playYoutube
-
youtubeInfo
-
queryYoutube
-
clientMove
: emitted when the Client that instantiated this MusicHandler moves from one VoiceChannel to another
Returns: old VoiceChannel and new VoiceChannel -
memberJoin
: emitted when a GuildMember joins a VoiceChannel where the Client is playing music
Returns: VoiceChannel and GuildMember -
memberLeave
: emitted when a GuildMember leaves a VoiceChannel where the Client is playing music
Returns: VoiceChannel and GuildMember -
start
: 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
This class does not really store information and is more of an alias but in some cases it can be useful.
-
guild (read-only)
Guild represented by this Playlist -
channel (read-only)
VoiceChannel joined by the Client -
firstJoinedAt (read-only)
Date representing the first time the Client joined a VoiceChannel in this Guild -
firstJoinedTimestamp (read-only)
Alias for firstJoinedAt.getTime() -
lastJoinedAt (read-only)
Date representing the last time the Client joined a VoiceChannel in this Guild -
lastJoinedTimestamp (read-only)
Alias for lastJoinedAt.getTime() -
connected (read-only)
Whether or not the Client is connected in this Guild -
playing (read-only)
Whether or not the Client is currently playing music -
paused
Whether or not the Client is paused -
looping
Whether or not the Client is looping the current music -
playlistLooping
Whether or not the Client is looping the Playlist -
current (read-only)
Information about the current music -
info (read-only)
Information about all the musics in this Playlist -
volume
The volume at which music is played
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.join -
leave
Alias for MusicHandler.prototype.leave -
add
Alias for MusicHandler.prototype.add -
remove
Alias for MusicHandler.prototype.remove -
playNext
Alias for MusicHandler.prototype.playNext -
skip
Alias for MusicHandler.prototype.skip -
clear
Alias for MusicHandler.prototype.clear -
shuffle
Alias for MusicHandler.prototype.shuffle
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");