From d7aaf47379e3691f5a1d7943c8ea31d688799d82 Mon Sep 17 00:00:00 2001
From: loicbersier <loic.bersier1@gmail.com>
Date: Wed, 16 Jan 2019 00:00:30 +0100
Subject: [PATCH] Find info about the music sent

---
 commands/utility/musicMatch.js | 70 ++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 commands/utility/musicMatch.js

diff --git a/commands/utility/musicMatch.js b/commands/utility/musicMatch.js
new file mode 100644
index 0000000..a78f070
--- /dev/null
+++ b/commands/utility/musicMatch.js
@@ -0,0 +1,70 @@
+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');
+
+
+class musicCommand extends Command {
+	constructor() {
+		super('musicMatch', {
+			aliases: ['musicMatch', 'music', 'shazam', 'soundhound'],
+			category: 'utility',
+			quoted: false,
+			args: [
+				{
+					id: 'music',
+					type: 'string',
+					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();
+		if (!args.music && Attachment[0]) {
+			link = Attachment[0].url;
+		} else {
+			link = args.music;
+		}
+		
+
+		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;
+				fetch(`https://api.acoustid.org/v2/lookup?client=LRAhvvxpDu&meta=recordings+releasegroups+compress&duration=${result.duration}&fingerprint=${result.fingerprint}`).then((response) => {
+					return response.json();
+				}).then((response) => { 
+					if (!response.results[0])
+						return message.channel.send('Could not identify the music');
+
+					const musicEmbed = new MessageEmbed()
+						.setColor('#ff9900')
+						.setTitle('Music found!')
+						.addField('Title', response.results[0].recordings[0].title, true)
+						.addField('Artist', response.results[0].recordings[0].artists[0].name, true)
+						.addField('Album', response.results[0].recordings[0].releasegroups[0].title, true);
+						
+						
+					message.channel.send(musicEmbed);
+				});
+			});
+		});
+	}
+}
+
+module.exports = musicCommand;
\ No newline at end of file