2020-04-03 06:26:16 +00:00
|
|
|
/*
|
2020-03-31 04:09:50 +00:00
|
|
|
const ytdl = require('ytdl-core-discord')
|
|
|
|
const youtubeInfo = require('youtube-info')
|
|
|
|
const getYoutubeId = require('get-youtube-id')
|
|
|
|
const fetch = require('node-fetch')
|
2020-04-03 06:26:16 +00:00
|
|
|
*/
|
2020-03-31 04:09:50 +00:00
|
|
|
|
2020-04-09 07:54:18 +00:00
|
|
|
const ytdl = require('ytdl-core-discord');
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
|
2020-03-30 16:01:13 +00:00
|
|
|
module.exports = client => {
|
2020-04-09 07:54:18 +00:00
|
|
|
client.music = {guilds: {}};
|
|
|
|
|
2020-03-30 16:01:13 +00:00
|
|
|
// MUSIC - TIMESTAMP
|
2020-03-31 04:09:50 +00:00
|
|
|
client.createTimestamp = function (duration) {
|
|
|
|
var hrs = ~~(duration / 60 / 60)
|
|
|
|
var min = ~~(duration / 60) % 60
|
|
|
|
var sec = ~~(duration - min * 60)
|
2020-04-03 08:31:48 +00:00
|
|
|
|
2020-03-31 04:09:50 +00:00
|
|
|
if (String(hrs).length < 2) {
|
|
|
|
hrs = '0' + String(hrs) + ':'
|
2020-03-30 16:01:13 +00:00
|
|
|
}
|
2020-04-03 08:31:48 +00:00
|
|
|
|
2020-03-31 04:09:50 +00:00
|
|
|
if (String(min).length < 2) {
|
|
|
|
min = '0' + String(min)
|
2020-03-30 16:01:13 +00:00
|
|
|
}
|
2020-04-03 08:31:48 +00:00
|
|
|
|
2020-03-31 04:09:50 +00:00
|
|
|
if (String(sec).length < 2) {
|
|
|
|
sec = '0' + String(sec)
|
2020-03-30 16:01:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 04:09:50 +00:00
|
|
|
if (hrs === '00:') {
|
|
|
|
hrs = ''
|
2020-03-30 16:01:13 +00:00
|
|
|
}
|
2020-03-31 04:09:50 +00:00
|
|
|
|
|
|
|
var time = hrs + min + ':' + sec
|
2020-03-30 16:01:13 +00:00
|
|
|
return time
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
|
|
|
client.music.getGuild = function(id) {
|
|
|
|
let guild = client.music.guilds[id];
|
|
|
|
|
|
|
|
if(!guild) {
|
|
|
|
guild = {};
|
|
|
|
|
|
|
|
guild.dispatcher = null;
|
|
|
|
guild.playing = false;
|
|
|
|
guild.queue = [];
|
|
|
|
|
|
|
|
client.music.guilds[id] = guild;
|
|
|
|
};
|
|
|
|
|
|
|
|
return guild;
|
|
|
|
};
|
|
|
|
|
|
|
|
client.music.isYouTubeLink = function(query) {
|
2020-04-11 14:09:48 +00:00
|
|
|
return query.startsWith('https://youtube.com/') || query.startsWith('http://youtube.com/') || query.startsWith('https://youtu.be/') || query.startsWith('http://youtu.be/') || query.startsWith('https://m.youtube.com/') || query.startsWith('http://m.youtube.com/') || query.startsWith('https://www.youtube.com/') || query.startsWith('http://www.youtube.com/');
|
2020-04-09 07:54:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
client.music.getLinkFromID = function(id) {
|
|
|
|
return 'https://www.youtube.com/watch?v=' + id;
|
|
|
|
};
|
|
|
|
|
|
|
|
client.music.getVideoByQuery = async function(query) {
|
|
|
|
let isLink = client.music.isYouTubeLink(query);
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
|
|
|
if(isLink) {
|
2020-04-11 14:18:42 +00:00
|
|
|
response = await fetch('https://www.googleapis.com/youtube/v3/search?key=' + client.config.keys.yt + '&part=id,snippet&maxResults=1&type=video&id=' + id);
|
2020-04-09 07:54:18 +00:00
|
|
|
} else {
|
2020-04-11 14:46:17 +00:00
|
|
|
// TODO: replace this workaround
|
|
|
|
response = await fetch('https://www.googleapis.com/youtube/v3/search?key=' + client.config.keys.yt + '&part=id,snippet&maxResults=1&type=video&q=**' + encodeURIComponent(query) + '**');
|
2020-04-09 07:54:18 +00:00
|
|
|
};
|
|
|
|
|
2020-04-11 14:09:48 +00:00
|
|
|
let parsed = await response.json();
|
2020-04-09 07:54:18 +00:00
|
|
|
|
|
|
|
if(parsed.items) {
|
|
|
|
let video = parsed.items[0];
|
|
|
|
|
|
|
|
if(video) {
|
|
|
|
return video;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-12 09:07:01 +00:00
|
|
|
client.music.play = async function(message, query, ignoreQueue) {
|
2020-04-09 07:54:18 +00:00
|
|
|
let guild = client.music.getGuild(message.guild.id);
|
|
|
|
|
|
|
|
if(!message.member.voice.channel && !guild.voiceChannel) {
|
2020-04-12 09:07:01 +00:00
|
|
|
return message.reply('you are not in a voice channel!');
|
2020-04-09 07:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let vc = message.member.voice.channel;
|
|
|
|
|
2020-04-12 09:07:01 +00:00
|
|
|
let video;
|
|
|
|
|
|
|
|
if(!ignoreQueue) {
|
|
|
|
video = await client.music.getVideoByQuery(query);
|
|
|
|
};
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-12 09:07:01 +00:00
|
|
|
if(video || ignoreQueue) {
|
|
|
|
if(!ignoreQueue) {
|
|
|
|
// Fix the bot if somehow broken
|
|
|
|
// music "playing", nothing in queue
|
|
|
|
if((guild.playing || guild.dispatcher) && guild.queue.length == 0) {
|
|
|
|
guild.playing = false;
|
|
|
|
guild.dispatcher = null;
|
|
|
|
// music not playing, something is in queue
|
|
|
|
} else if(!guild.playing && !guild.dispatcher && guild.queue.length > 0) {
|
|
|
|
guild.queue = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add video to queue
|
|
|
|
guild.queue.push({video: video, requestedBy: message.member.id});
|
|
|
|
};
|
2020-04-09 07:54:18 +00:00
|
|
|
|
|
|
|
// Figure out if the bot should add it to queue or play it right now
|
|
|
|
if(guild.playing) {
|
2020-04-11 14:37:45 +00:00
|
|
|
message.reply('added **' + video.snippet.title + '** to the queue');
|
2020-04-09 07:54:18 +00:00
|
|
|
} else {
|
|
|
|
guild.playing = true;
|
|
|
|
|
2020-04-12 09:07:01 +00:00
|
|
|
guild.voiceChannel = vc;
|
|
|
|
|
2020-04-09 07:54:18 +00:00
|
|
|
let connection = await vc.join();
|
2020-04-11 14:09:48 +00:00
|
|
|
|
2020-04-11 14:30:08 +00:00
|
|
|
let v = guild.queue[0];
|
|
|
|
|
2020-04-12 08:48:47 +00:00
|
|
|
guild.dispatcher = connection.play(await ytdl(client.music.getLinkFromID(v.video.id.videoId), {highWaterMark: 1024 * 1024 * 32}), {type: 'opus'});
|
2020-04-12 08:47:48 +00:00
|
|
|
guild.dispatcher.setVolume(0.25);
|
2020-04-11 14:30:08 +00:00
|
|
|
|
2020-04-12 09:07:01 +00:00
|
|
|
message.channel.send('Playing **' + v.video.snippet.title + '**');
|
|
|
|
|
|
|
|
// play next in queue on end
|
|
|
|
guild.dispatcher.once('finish', () => {
|
|
|
|
guild.queue.shift();
|
|
|
|
guild.playing = false;
|
|
|
|
|
|
|
|
if(guild.queue.length > 0) {
|
|
|
|
client.music.play(message, null, true);
|
|
|
|
} else {
|
|
|
|
guild.dispatcher = null;
|
|
|
|
|
|
|
|
connection.leave();
|
|
|
|
};
|
|
|
|
});
|
2020-04-09 07:54:18 +00:00
|
|
|
};
|
|
|
|
} else {
|
2020-04-12 09:07:01 +00:00
|
|
|
return message.reply('failed to find the video!');
|
2020-04-09 07:54:18 +00:00
|
|
|
};
|
|
|
|
};
|
2020-04-11 14:45:47 +00:00
|
|
|
|
|
|
|
client.music.setVolume = function(guild, target) {
|
|
|
|
let g = client.music.getGuild(guild.id);
|
|
|
|
|
|
|
|
if(g.dispatcher) {
|
|
|
|
g.dispatcher.setVolume(target);
|
|
|
|
};
|
|
|
|
};
|
2020-04-12 09:24:58 +00:00
|
|
|
|
|
|
|
client.music.skip = function(guild, reason) {
|
|
|
|
let g = client.music.getGuild(guild.id);
|
|
|
|
|
|
|
|
if(g.dispatcher) {
|
|
|
|
g.dispatcher.end(reason);
|
|
|
|
};
|
|
|
|
};
|
2020-03-30 16:01:13 +00:00
|
|
|
}
|