Haha-Yes/commands/utility/musicMatch.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-01-16 00:00:30 +01:00
const { Command } = require('discord-akairo');
const { MessageEmbed } = require('discord.js');
const fpcalc = require('fpcalc');
const fetch = require('node-fetch');
const youtubedl = require('youtube-dl');
const fs = require('fs');
2019-01-16 00:05:22 +01:00
const { acoustID } = require('../../config.json');
2019-01-16 00:00:30 +01:00
class musicCommand extends Command {
constructor() {
super('musicMatch', {
aliases: ['musicMatch', 'music', 'shazam', 'soundhound'],
category: 'utility',
args: [
{
id: 'music',
type: 'string',
2019-06-23 03:41:59 +02:00
prompt: {
start: 'Send the link of wich video you want to know song',
},
2019-01-16 00:00:30 +01:00
match: 'rest'
}
],
description: {
content: 'Find what music it is from attachment or link',
usage: '[file]',
examples: ['[file]']
}
});
}
async exec(message,args) {
let link;
let Attachment = (message.attachments).array();
2019-01-16 00:29:10 +01:00
2019-01-16 00:46:24 +01:00
function checkURL(url) {
return(url.match(/\.(mp3|wav|mp4|webm)$/) != null);
}
2019-01-16 00:29:10 +01:00
2019-01-16 00:00:30 +01:00
if (!args.music && Attachment[0]) {
2019-01-16 00:46:24 +01:00
if (!checkURL(Attachment[0].url))
2019-01-16 00:29:10 +01:00
return message.channel.send('Only mp3,wav,mp4 and webm are supported');
2019-01-16 00:00:30 +01:00
link = Attachment[0].url;
} else {
link = args.music;
2019-01-16 00:46:24 +01:00
if (!checkURL(link))
2019-01-16 00:29:10 +01:00
return message.channel.send('Only mp3,wav,mp4 and webm are supported');
2019-01-16 00:00:30 +01:00
}
let video = youtubedl(link, ['-x', '--audio-format', 'mp3']);
video.pipe(fs.createWriteStream('./music.mp3'));
video.on('error', function error(err) {
console.log('error 2:', err);
message.channel.send('An error has occured, I can\'t download from the link you provided.');
});
video.on('end', function () {
fpcalc('./music.mp3', function(err, result) {
if (err) throw err;
2019-01-16 00:05:22 +01:00
fetch(`https://api.acoustid.org/v2/lookup?client=${acoustID}&meta=recordings+releasegroups+compress&duration=${result.duration}&fingerprint=${result.fingerprint}`).then((response) => {
2019-01-16 00:00:30 +01:00
return response.json();
}).then((response) => {
if (!response.results[0])
return message.channel.send('Could not identify the music');
2019-01-16 00:29:10 +01:00
let time = response.results[0].recordings[0].duration;
let minutes = Math.floor(time / 60);
let seconds = time - minutes * 60;
2019-01-16 00:00:30 +01:00
const musicEmbed = new MessageEmbed()
2019-11-02 00:54:40 +01:00
.setColor(message.member.displayHexColor)
2019-01-16 00:00:30 +01:00
.setTitle('Music found!')
.addField('Title', response.results[0].recordings[0].title, true)
.addField('Artist', response.results[0].recordings[0].artists[0].name, true)
2019-01-16 00:29:10 +01:00
.addField('Album', response.results[0].recordings[0].releasegroups[0].title, true)
.addField('Duration', `${minutes}:${seconds}`);
2019-01-16 00:00:30 +01:00
message.channel.send(musicEmbed);
});
});
});
}
}
module.exports = musicCommand;