This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.js
137 lines (131 loc) · 6.86 KB
/
music.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
/*eslint-disable*/
const {
Client
} = require('discord.js');
const yt = require('ytdl-core');
const config = require('./config.json');
const client = new Client();
const YouTube = require('youtube-node');
const youTube = new YouTube();
youTube.setKey(config.yt_token);
let queue = {};
const commands = {
'play': (msg) => {
if (queue[msg.guild.id] === undefined) return msg.channel.sendMessage(`Add some songs to the queue first with ${config.prefix}add`);
if (!msg.guild.voiceConnection) return commands.join(msg).then(() => commands.play(msg));
if (queue[msg.guild.id].playing) return msg.channel.sendMessage('Already Playing');
let dispatcher;
queue[msg.guild.id].playing = true;
console.log(queue);
(function play(song) {
console.log(song);
if (song === undefined) return msg.channel.sendMessage('Queue is empty').then(() => {
queue[msg.guild.id].playing = false;
msg.member.voiceChannel.leave();
});
msg.channel.sendMessage(`Playing: **${song.title}** as requested by: **${song.requester}**`);
dispatcher = msg.guild.voiceConnection.playStream(yt(song.url, {
audioonly: true
}), {
passes: config.passes
});
let collector = msg.channel.createCollector(m => m);
collector.on('message', m => {
if (m.content.startsWith(config.prefix + 'pause')) {
msg.channel.sendMessage('paused').then(() => {
dispatcher.pause();
});
} else if (m.content.startsWith(config.prefix + 'resume')) {
msg.channel.sendMessage('resumed').then(() => {
dispatcher.resume();
});
} else if (m.content.startsWith(config.prefix + 'skip')) {
msg.channel.sendMessage('skipped').then(() => {
dispatcher.end();
});
} else if (m.content.startsWith('volume+')) {
if (Math.round(dispatcher.volume * 50) >= 100) return msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
dispatcher.setVolume(Math.min((dispatcher.volume * 50 + (2 * (m.content.split('+').length - 1))) / 50, 2));
msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
} else if (m.content.startsWith('volume-')) {
if (Math.round(dispatcher.volume * 50) <= 0) return msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
dispatcher.setVolume(Math.max((dispatcher.volume * 50 - (2 * (m.content.split('-').length - 1))) / 50, 0));
msg.channel.sendMessage(`Volume: ${Math.round(dispatcher.volume*50)}%`);
} else if (m.content.startsWith(config.prefix + 'time')) {
msg.channel.sendMessage(`time: ${Math.floor(dispatcher.time / 60000)}:${Math.floor((dispatcher.time % 60000)/1000) <10 ? '0'+Math.floor((dispatcher.time % 60000)/1000) : Math.floor((dispatcher.time % 60000)/1000)}`);
}
});
dispatcher.on('end', () => {
collector.stop();
play(queue[msg.guild.id].songs.shift());
});
dispatcher.on('error', (err) => {
return msg.channel.sendMessage('error: ' + err).then(() => {
collector.stop();
play(queue[msg.guild.id].songs.shift());
});
});
})(queue[msg.guild.id].songs.shift());
},
'join': (msg) => {
return new Promise((resolve, reject) => {
const voiceChannel = msg.member.voiceChannel;
if (!voiceChannel || voiceChannel.type !== 'voice') return msg.reply('I couldn\'t connect to your voice channel...');
voiceChannel.join().then(connection => resolve(connection)).catch(err => reject(err));
});
},
'add': (msg) => {
function addFromUrl(url) {
msg.channel.sendMessage("*Adding...*");
if (url == '' || url === undefined) return msg.channel.sendMessage(`You must add a YouTube video url after ${config.prefix}add`);
yt.getInfo(url, (err, info) => {
if (err) return msg.channel.sendMessage('Invalid YouTube Link: ' + err);
if (!queue.hasOwnProperty(msg.guild.id)) queue[msg.guild.id] = {}, queue[msg.guild.id].playing = false, queue[msg.guild.id].songs = [];
queue[msg.guild.id].songs.push({
url: url,
title: info.title,
requester: msg.author.username
});
msg.channel.sendMessage(`Added **${info.title}** to the queue`);
});
}
function addFromQuery(query) {
youTube.search(query, 2, function(error, result) {
if (error) {
msg.channel.sendMessage('Error:\n' + error);
} else {
let url = `https://www.youtube.com/watch?v=${result.items[0]["id"].videoId}`;
addFromUrl(url);
}
});
}
if (msg.content === `${config.prefix}add`) return msg.reply(`You must add a YouTube video url after ${config.prefix}add`);
let url = msg.content.split(' ')[1];
if (url.includes("https://youtube.com/") || url.includes("https://www.youtube.com/") || url.includes("http://youtube.com/") || url.includes("http://www.youtube.com/") || url.includes("https://youtu.be/") || url.includes("https://www.youtu.be/") || url.includes("http://youtu.be/") || url.includes("http://www.youtu.be/")) {
addFromUrl(url);
} else {
let query = msg.content.replace(`${config.prefix}add`, '');
addFromQuery(query);
}
},
'queue': (msg) => {
if (queue[msg.guild.id] === undefined) return msg.channel.sendMessage(`Add some songs to the queue first with ${config.prefix}add`);
let tosend = [];
queue[msg.guild.id].songs.forEach((song, i) => {
tosend.push(`${i+1}. ${song.title} - Requested by: ${song.requester}`);
});
msg.channel.sendMessage(`__**${msg.guild.name}'s Music Queue:**__ Currently **${tosend.length}** songs queued ${(tosend.length > 15 ? '*[Only next 15 shown]*' : '')}\n\`\`\`${tosend.slice(0,15).join('\n')}\`\`\``);
},
'mhelp': (msg) => {
let tosend = ['```xl', config.prefix + 'join : "Join Voice channel of msg sender"', config.prefix + 'add : "Add a song (link or name) to the queue"', config.prefix + 'queue : "Shows the current queue, up to 15 songs shown."', config.prefix + 'play : "Play the music queue if already joined to a voice channel"', '', 'the following commands only function while the play command is running:'.toUpperCase(), config.prefix + 'pause : "pauses the music"', config.prefix + 'resume : "resumes the music"', config.prefix + 'skip : "skips the playing song"', config.prefix + 'time : "Shows the playtime of the song."', 'volume+(+++) : "increases volume by 2%/+"', 'volume-(---) : "decreases volume by 2%/-"', '```'];
msg.channel.sendMessage(tosend.join('\n'));
},
};
client.on('ready', () => {
console.log('ready!');
});
client.on('message', msg => {
if (!msg.content.startsWith(config.prefix)) return;
if (commands.hasOwnProperty(msg.content.toLowerCase().slice(config.prefix.length).split(' ')[0])) commands[msg.content.toLowerCase().slice(config.prefix.length).split(' ')[0]](msg);
});
client.login(config.token);