2020-04-13 04:28:53 +00:00
|
|
|
const ytdl = require('ytdl-core-discord')
|
|
|
|
const fetch = require('node-fetch')
|
2020-04-14 05:28:25 +00:00
|
|
|
const { MessageEmbed } = require('discord.js')
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-03-30 16:01:13 +00:00
|
|
|
module.exports = client => {
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music = { guilds: {} }
|
2020-04-09 07:54:18 +00:00
|
|
|
|
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
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music.getGuild = function (id) {
|
|
|
|
let guild = client.music.guilds[id]
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
if (!guild) {
|
|
|
|
guild = {}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
guild.dispatcher = null
|
|
|
|
guild.playing = false
|
|
|
|
guild.queue = []
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music.guilds[id] = guild
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
return guild
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music.getLinkFromID = function (id) {
|
|
|
|
return 'https://www.youtube.com/watch?v=' + id
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-15 05:43:31 +00:00
|
|
|
client.music.getVideoByQuery = async query => {
|
|
|
|
let resp
|
|
|
|
|
|
|
|
try {
|
|
|
|
const id = await ytdl.getURLVideoID(query)
|
2020-04-18 14:14:54 +00:00
|
|
|
resp = await fetch('https://invidious.snopyta.org/api/v1/videos/' + id)
|
2020-04-15 05:43:31 +00:00
|
|
|
console.log(resp)
|
|
|
|
} catch (err) {
|
2020-04-18 14:14:54 +00:00
|
|
|
resp = await fetch('https://invidious.snopyta.org/api/v1/search?q=' + encodeURIComponent(query))
|
2020-04-13 04:28:53 +00:00
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-15 05:43:31 +00:00
|
|
|
const parsed = await resp.json()
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-14 09:44:02 +00:00
|
|
|
if (parsed) {
|
2020-04-14 05:28:25 +00:00
|
|
|
const videos = parsed
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-14 05:28:25 +00:00
|
|
|
if (videos) {
|
|
|
|
return videos
|
2020-04-09 07:54:18 +00:00
|
|
|
} else {
|
2020-04-13 04:28:53 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
} else {
|
2020-04-13 04:28:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music.play = async function (message, query, ignoreQueue) {
|
|
|
|
const guild = client.music.getGuild(message.guild.id)
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
if (!message.member.voice.channel && !guild.voiceChannel) {
|
|
|
|
return message.reply('you are not in a voice channel!')
|
2020-04-09 07:54:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
const vc = message.member.voice.channel
|
|
|
|
|
|
|
|
let video
|
2020-04-14 05:28:25 +00:00
|
|
|
let videos
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
if (!ignoreQueue) {
|
2020-04-14 05:28:25 +00:00
|
|
|
videos = await client.music.getVideoByQuery(query)
|
2020-04-15 05:43:31 +00:00
|
|
|
if (!videos[1]) {
|
|
|
|
if (!videos[0]) {
|
|
|
|
video = videos
|
|
|
|
} else {
|
|
|
|
video = videos[0]
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 04:28:53 +00:00
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-14 05:28:25 +00:00
|
|
|
if (videos || ignoreQueue) {
|
2020-04-13 04:28:53 +00:00
|
|
|
if (!ignoreQueue) {
|
|
|
|
// Fix the bot if somehow broken
|
2020-04-12 09:07:01 +00:00
|
|
|
// music "playing", nothing in queue
|
2020-04-13 04:28:53 +00:00
|
|
|
if ((guild.playing || guild.dispatcher) && guild.queue.length === 0) {
|
|
|
|
guild.playing = false
|
|
|
|
guild.dispatcher = null
|
2020-04-12 09:07:01 +00:00
|
|
|
// music not playing, something is in queue
|
2020-04-13 04:28:53 +00:00
|
|
|
} else if (!guild.playing && !guild.dispatcher && guild.queue.length > 0) {
|
|
|
|
guild.queue = []
|
|
|
|
}
|
2020-04-12 09:07:01 +00:00
|
|
|
|
2020-04-15 05:43:31 +00:00
|
|
|
if (!video) {
|
2020-04-14 05:28:25 +00:00
|
|
|
let output = ''
|
|
|
|
let i = 0
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if (!videos[i]) break
|
|
|
|
output += `\`${i + 1}:\` **[${videos[i].title}](https://www.youtube.com/watch?v=${videos[i].videoId})** \`[${client.createTimestamp(videos[i].lengthSeconds)}]\`\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
const embed = new MessageEmbed()
|
|
|
|
embed.setTitle('Please reply with a number `1-' + i + '` to select which song you want to add to the queue.')
|
|
|
|
embed.setColor(client.embedColour(message.guild))
|
|
|
|
embed.setDescription(output)
|
|
|
|
const selection = await client.awaitReply(message, embed)
|
2020-04-15 05:43:31 +00:00
|
|
|
console.log(selection)
|
2020-04-14 05:28:25 +00:00
|
|
|
|
2020-04-15 05:43:31 +00:00
|
|
|
switch (selection) {
|
2020-04-14 05:28:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 05:43:31 +00:00
|
|
|
|
2020-04-14 09:44:02 +00:00
|
|
|
if (!video && videos[0]) {
|
2020-04-14 05:28:25 +00:00
|
|
|
video = videos[0]
|
2020-04-15 05:43:31 +00:00
|
|
|
} else if (!video) {
|
2020-04-14 09:44:02 +00:00
|
|
|
video = videos
|
2020-04-14 05:28:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 05:43:31 +00:00
|
|
|
console.log(video)
|
|
|
|
|
2020-04-12 09:07:01 +00:00
|
|
|
// Add video to queue
|
2020-04-13 04:28:53 +00:00
|
|
|
guild.queue.push({ video: video, requestedBy: message.member.id })
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
// Figure out if the bot should add it to queue or play it right now
|
|
|
|
if (guild.playing) {
|
2020-04-14 05:28:25 +00:00
|
|
|
message.reply('added **' + video.title + '** to the queue')
|
2020-04-09 07:54:18 +00:00
|
|
|
} else {
|
2020-04-13 04:28:53 +00:00
|
|
|
guild.playing = true
|
|
|
|
|
|
|
|
guild.voiceChannel = vc
|
2020-04-09 07:54:18 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
const connection = await vc.join()
|
2020-04-12 09:07:01 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
const v = guild.queue[0]
|
2020-04-14 13:13:00 +00:00
|
|
|
|
2020-04-14 05:28:25 +00:00
|
|
|
guild.dispatcher = connection.play(await ytdl(client.music.getLinkFromID(v.video.videoId), { highWaterMark: 1024 * 1024 * 32 }), { type: 'opus' })
|
2020-04-13 04:28:53 +00:00
|
|
|
guild.dispatcher.setVolume(0.25)
|
2020-04-11 14:30:08 +00:00
|
|
|
|
2020-04-14 05:28:25 +00:00
|
|
|
message.channel.send('Playing **' + v.video.title + '**')
|
2020-04-12 09:07:01 +00:00
|
|
|
|
|
|
|
// play next in queue on end
|
|
|
|
guild.dispatcher.once('finish', () => {
|
2020-04-13 04:28:53 +00:00
|
|
|
guild.queue.shift()
|
|
|
|
guild.playing = false
|
2020-04-12 09:07:01 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
if (guild.queue.length > 0) {
|
|
|
|
client.music.play(message, null, true)
|
2020-04-12 09:07:01 +00:00
|
|
|
} else {
|
2020-04-13 04:28:53 +00:00
|
|
|
guild.dispatcher = null
|
2020-04-12 09:07:01 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
connection.disconnect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-09 07:54:18 +00:00
|
|
|
} else {
|
2020-04-13 04:28:53 +00:00
|
|
|
return message.reply('failed to find the video!')
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 14:45:47 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music.setVolume = function (guild, target) {
|
|
|
|
const g = client.music.getGuild(guild.id)
|
2020-04-11 14:45:47 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
if (g.dispatcher) {
|
|
|
|
g.dispatcher.setVolume(target)
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 09:24:58 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
client.music.skip = function (guild, reason) {
|
|
|
|
const g = client.music.getGuild(guild.id)
|
2020-04-12 09:24:58 +00:00
|
|
|
|
2020-04-13 04:28:53 +00:00
|
|
|
if (g.dispatcher) {
|
|
|
|
g.dispatcher.end(reason)
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 16:01:13 +00:00
|
|
|
}
|