-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
63 lines (58 loc) · 2.35 KB
/
index.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
const ytdl = require('ytdl-core');
const YouTube = require('simple-youtube-api');
const { Client } = require('discord.js'); // eslint-disable-line no-unused-vars
const { version } = require('./package.json');
class Player {
/**
* @param {Client} client - Your d.js client.
* @param {string} ytkey - Your Youtube v3 data key.
* @param {string} channel - The channel you want bot to stay in id.
* @param {string} playlist - playlist or video for the bot to play.
*/
constructor(client, ytkey, channel, playlist) {
this.client = client || null;
this.ytkey = ytkey || null;
this.channel = channel || null;
this.playlist = playlist || null;
this.queue = null;
this.dispatcher = null;
}
async play() {
const youtube = new YouTube(this.ytkey);
const playlist = await youtube.getPlaylist(this.playlist);
const getVideos = await playlist.getVideos();
const queue = getVideos.filter(v => v.thumbnails !== undefined);
this.queue = queue;
const stream = (this, async () => {
try {
var connection = await this.client.channels.cache.get(this.channel).join(); // eslint-disable-line no-var
} catch (error) {
console.error(`[ERROR:CONNECTION] Error occurred when joining voice channel.`);
}
const ytStream = ytdl(queue[0].url, {
filter: 'audioonly',
quality: 'highestaudio'
}).on('error', err => {
console.error(`[ERROR:STREAMING] Couldn't play **${queue[0].title}**`, err);
queue.shift();
});
const thisDispatcher = connection.play(ytStream) // eslint-disable-line block-scoped-var
.on('finish', () => {
const loop = queue.shift();
queue.push(loop);
stream(this.client, this.channel).then(dispatcher => this.dispatcher = dispatcher).catch(err => console.log(`[ERROR:STREAMING] ${err}`));
}).on('error', err => {
console.error(`[ERROR:DISPATCHER] ${err}`);
})
.on('start', () => {
this.client.user.setActivity(queue[0].title, { type: 'LISTENING' });
console.log(`[INFO] Started streaming: ${queue[0].title} at ${this.client.channels.cache.get(this.channel).name}.`);
});
return thisDispatcher;
});
this.client.user.setActivity('Loading...', { type: 'LISTENING' });
stream(this.client, this.channel).then(dispatcher => this.dispatcher = dispatcher).catch(err => console.error(`[ERROR:STREAMING] ${err}`));
}
}
module.exports = Player;
exports.version = version;