This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_play.js
196 lines (188 loc) · 6.67 KB
/
cmd_play.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
184
185
186
187
188
189
190
191
192
193
194
195
196
// WORK IN PROGRESS (PROBABLY NOT CURRENTLY WORKING CAUSE FIREC IS AN IDIOT) (r u sure)
const {
google
} = require('googleapis');
const key = 'AIzaSyBXcfJ5kwQ9t8xvppno8yjkJZkc9PwreXs'
const ytdl = require("ytdl-core");
const fs = require("fs");
const queue = {};
const connections = {};
const guildsplaying = [];
const youtube = google.youtube({
version: 'v3',
auth: key
});
//oh yeah get the latest ffmpeg binary before you run this
module.exports = function (msg, bot, args) {
async function play(video) {
let c = connections[msg.channel.guild.id];
if (!c) {
try {
c = connections[msg.channel.guild.id] = await bot.joinVoiceChannel(msg.member.voiceState.channelID)
} catch (e) {
msg.channel.createMessage(`Error joining voice channel: ${e.message}`);
}
}
let q = queue[msg.channel.guild.id];
if (!q) q = queue[msg.channel.guild.id] = [];
if (c.playing) c.stopPlaying();
const url = "https://www.youtube.com/watch?v=" + video.id.videoId
const stream = ytdl(url, {
filter: 'audioonly'
});
msg.channel.createMessage({
embed: {
title: video.snippet.title,
description: video.snippet.description,
url: "https://www.youtube.com/watch?v=" + video.id.videoId,
color: 58623,
timestamp: new Date(),
footer: {
icon_url: "https://cdn.discordapp.com/avatars/255397678492418048/a8e516d198c913fb897aa592ce21e260.png",
text: "~play"
},
thumbnail: {
url: video.snippet.thumbnails.high.url
},
author: {
name: "🎶 Now playing:",
icon_url: "https://cdn.discordapp.com/avatars/255397678492418048/a8e516d198c913fb897aa592ce21e260.png"
}
}
})
console.log(video.snippet)
try {
c.play(stream); // Play the file and notify the user
} catch (e) { // no opus stream fuck
const stream = ytdl(url, {
filter: 'audioonly'
});
c.play(stream);
}
c.once("end", () => {
msg.channel.createMessage({
embed: {
title: video.snippet.title,
description: video.snippet.description.slice(0,20)+"...",
url: "https://www.youtube.com/watch?v=" + video.id.videoId,
color: 327424,
timestamp: new Date(),
footer: {
icon_url: "https://cdn.discordapp.com/avatars/255397678492418048/a8e516d198c913fb897aa592ce21e260.png",
text: "~play"
},
thumbnail: {
url: video.snippet.thumbnails.high.url
},
author: {
name: "✅ Finished:",
icon_url: "https://cdn.discordapp.com/avatars/255397678492418048/a8e516d198c913fb897aa592ce21e260.png"
}
}
})
//isplaying = false;
if (q.length == 0) {
bot.leaveVoiceChannel(msg.member.voiceState.channelID);
delete queue[msg.channel.guild.id];
delete connections[msg.channel.guild.id];
} else {
console.log(q.length)
const link = q.shift();
if (link.includes("://www.youtube.com")) {
manageYoutubeLink(link)
} else if (link.includes("://youtu.be")) {
manageYoutuDotBe(link);
} else {
search(link);
}
}
});
}
async function search(query) {
try {
var res = await youtube.search.list({
part: 'id, snippet',
q: query // NO CONFUSION ON MY QRISTIAN SERVER
});
console.log(query);
var video = res.data.items[0];
if (typeof (video.id.videoId) === "undefined") {
console.log("oops");
video = res.data.items[1];
console.log(video);
}
if (typeof (video.id.videoId) === "undefined") {
console.log("oops");
video = res.data.items[2];
console.log(video);
}
if (typeof (video.id.videoId) === "undefined") {
console.log("oops");
video = res.data.items[3];
console.log(video);
}
if (typeof (video.id.videoId) === "undefined") {
console.log("oops");
video = res.data.items[4];
console.log(video);
}
play(video);
} catch (e) {
console.log(e);
}
}
function manageYoutuDotBe(link) {
try {
//that much pain for a fucking title
const res = youtube.search.list({
part: 'id, snippet',
q: link.split('.be/')[1]
}, (err, response) => {
if (err) return;
play(response.data.items[0])
});
} catch (e) {
console.log(e);
}
}
function manageYoutubeLink(link) {
try {
//that much pain for a fucking title
const res = youtube.search.list({
part: 'id, snippet',
q: link.split('v=')[1]
}, (err, response) => {
if (err) return;
play(response.data.items[0])
});
} catch (e) {
console.log(e);
}
}
if (args.length > 1) {
if (!msg.member.voiceState.channelID) {
msg.channel.createMessage('You have to be in a voice channel!');
} else {
console.log(args);
if (queue[msg.channel.guild.id]) {
bot.createMessage(msg.channel.id, "Adding to queue");
queue[msg.channel.guild.id].push(args);
} else {
if (typeof (args) === "undefined") {
bot.createMessage("Queue empty :smiley:");
} else {
if (args.includes("://www.youtube.com")) {
manageYoutubeLink(args)
} else if (args.includes("://youtu.be")) {
manageYoutuDotBe(args);
} else {
search(args);
}
}
}
}
} else {
msg.channel.createMessage('You have to specify a song!');
return;
}
};