-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaylist.js
183 lines (166 loc) · 6.9 KB
/
playlist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const {
Util,
MessageEmbed
} = require("discord.js");
const ytdl = require("ytdl-core");
const yts = require("yt-search");
const ytdlDiscord = require("ytdl-core-discord");
var ytpl = require('ytpl');
const sendError = require("../util/error")
const fs = require('fs');
module.exports = {
info: {
name: "playlist",
description: "To play songs :D",
usage: "<YouTube Playlist URL | Playlist Name>",
aliases: ["pl"],
},
run: async function (client, message, args) {
const channel = message.member.voice.channel;
if (!channel) return sendError("I'm sorry but you need to be in a voice channel to play music!", message.channel);
const url = args[0] ? args[0].replace(/<(.+)>/g, "$1") : "";
var searchString = args.join(" ");
const permissions = channel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT")) return sendError("I cannot connect to your voice channel, make sure I have the proper permissions!", message.channel);
if (!permissions.has("SPEAK")) return sendError("I cannot speak in this voice channel, make sure I have the proper permissions!", message.channel);
if (!searchString||!url) return sendError(`Usage: ${message.client.config.prefix}playlist <YouTube Playlist URL | Playlist Name>`, message.channel);
if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
try {
const playlist = await ytpl(url.split("list=")[1]);
if (!playlist) return sendError("Playlist not found", message.channel)
const videos = await playlist.items;
for (const video of videos) {
// eslint-disable-line no-await-in-loop
await handleVideo(video, message, channel, true); // eslint-disable-line no-await-in-loop
}
return message.channel.send({
embed: {
color: "GREEN",
description: `✅ **|** Playlist: **\`${videos[0].title}\`** has been added to the queue`
}
})
} catch (error) {
console.error(error);
return sendError("Playlist not found :(",message.channel).catch(console.error);
}
} else {
try {
var searched = await yts.search(searchString)
if (searched.playlists.length === 0) return sendError("Looks like i was unable to find the playlist on YouTube", message.channel)
var songInfo = searched.playlists[0];
let listurl = songInfo.listId;
const playlist = await ytpl(listurl)
const videos = await playlist.items;
for (const video of videos) {
// eslint-disable-line no-await-in-loop
await handleVideo(video, message, channel, true); // eslint-disable-line no-await-in-loop
}
let thing = new MessageEmbed()
.setAuthor("Playlist has been added to queue", "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
.setThumbnail(songInfo.thumbnail)
.setColor("GREEN")
.setDescription(`✅ **|** Playlist: **\`${songInfo.title}\`** has been added \`${songInfo.videoCount}\` video to the queue`)
return message.channel.send(thing)
} catch (error) {
return sendError("An unexpected error has occurred",message.channel).catch(console.error);
}
}
async function handleVideo(video, message, channel, playlist = false) {
const serverQueue = message.client.queue.get(message.guild.id);
const song = {
id: video.id,
title: Util.escapeMarkdown(video.title),
views: video.views ? video.views : "-",
ago: video.ago ? video.ago : "-",
duration: video.duration,
url: `https://www.youtube.com/watch?v=${video.id}`,
img: video.thumbnail,
req: message.author
};
if (!serverQueue) {
const queueConstruct = {
textChannel: message.channel,
voiceChannel: channel,
connection: null,
songs: [],
volume: 80,
playing: true,
loop: false
};
message.client.queue.set(message.guild.id, queueConstruct);
queueConstruct.songs.push(song);
try {
var connection = await channel.join();
queueConstruct.connection = connection;
play(message.guild, queueConstruct.songs[0]);
} catch (error) {
console.error(`I could not join the voice channel: ${error}`);
message.client.queue.delete(message.guild.id);
return sendError(`I could not join the voice channel: ${error}`, message.channel);
}
} else {
serverQueue.songs.push(song);
if (playlist) return;
let thing = new MessageEmbed()
.setAuthor("Song has been added to queue", "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
.setThumbnail(song.img)
.setColor("YELLOW")
.addField("Name", song.title, true)
.addField("Duration", song.duration, true)
.addField("Requested by", song.req.tag, true)
.setFooter(`Views: ${song.views} | ${song.ago}`)
return message.channel.send(thing);
}
return;
}
async function play(guild, song) {
const serverQueue = message.client.queue.get(message.guild.id);
let afk = JSON.parse(fs.readFileSync("./afk.json", "utf8"));
if (!afk[message.guild.id]) afk[message.guild.id] = {
afk: false,
};
var online = afk[message.guild.id]
if (!song){
if (!online.afk) {
sendError("Leaving the voice channel because I think there are no songs in the queue. If you like the bot stay 24/7 in voice channel run `!afk`\n\nThank you for using my code! [GitHub](https://github.com/SudhanPlayz/Discord-MusicBot)", message.channel)
message.guild.me.voice.channel.leave();//If you want your bot stay in vc 24/7 remove this line :D
message.client.queue.delete(message.guild.id);
}
return message.client.queue.delete(message.guild.id);
}
let stream = null;
if (song.url.includes("youtube.com")) {
stream = await ytdl(song.url);
stream.on('error', function(er) {
if (er) {
if (serverQueue) {
serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
return sendError(`An unexpected error has occurred.\nPossible type \`${er}\``, message.channel)
}
}
});
}
serverQueue.connection.on("disconnect", () => message.client.queue.delete(message.guild.id));
const dispatcher = serverQueue.connection
.play(ytdl(song.url,{quality: 'highestaudio', highWaterMark: 1 << 25 ,type: "opus"}))
.on("finish", () => {
const shiffed = serverQueue.songs.shift();
if (serverQueue.loop === true) {
serverQueue.songs.push(shiffed);
};
play(guild, serverQueue.songs[0]);
})
dispatcher.setVolume(serverQueue.volume / 100);
let thing = new MessageEmbed()
.setAuthor("Started Playing Music!", "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
.setThumbnail(song.img)
.setColor("BLUE")
.addField("Name", song.title, true)
.addField("Duration", song.duration, true)
.addField("Requested by", song.req.tag, true)
.setFooter(`Views: ${song.views} | ${song.ago}`)
serverQueue.textChannel.send(thing);
}
},
};