From 7d2015a249fee16690859538db6b0936bc5aa229 Mon Sep 17 00:00:00 2001 From: "lucas.mathieu" Date: Fri, 18 Dec 2020 22:52:24 +0100 Subject: [PATCH] Added play command --- genius.js | 4 +-- index.js | 62 +++++++++++++++++++++++++++++++++++++++++++++-- package-lock.json | 5 ++++ package.json | 3 ++- 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/genius.js b/genius.js index bd88f74..1e10989 100644 --- a/genius.js +++ b/genius.js @@ -1,6 +1,7 @@ const axios = require("axios"); const config = require("./config/config.json"); const $ = require('cheerio'); +const { Collection } = require("discord.js"); class Genius { search_song(q, callback) { @@ -32,10 +33,9 @@ class Genius { else if (element.children[0]) { element.children.forEach(children => { if (children.data) lyrics += children.data; + if (children.children) if (children.children[0]) lyrics += children.children[0].data; }); } - - //else if (element.children[0]) lyrics += element.children[0].data; }); return callback(null, lyrics); }).catch(err => { diff --git a/index.js b/index.js index a71b94e..db1a757 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const Discord = require("discord.js"); const client = new Discord.Client(); const config = require("./config/config.json"); const Genius = require("./genius"); +const levenshtein = require('js-levenshtein'); client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); @@ -26,8 +27,65 @@ client.on('message', message => { } } }); - }) + }); + } + + else if (command === "play") { + Genius.search_song(args.join(" "), song => { + Genius.get_lyrics(song.id, (err, lyrics) => { + if (err) { + console.log("Error while fetching lyrics: " + err.message); + message.channel.send("Error fetching lyrics, please retry in a few seconds"); + } + else { + const tab_lyrics = lyrics.split("\n"); + const new_tab_lyrics = [] + for (let i = 0; i < tab_lyrics.length; i++) { + if (tab_lyrics[i] === "" || tab_lyrics[i].includes("[")) { + tab_lyrics.splice(i, 1); + i--; + } + } + const random = Math.floor(Math.random() * tab_lyrics.length) + 4; + const lyricToFind = tab_lyrics[random]; + while (lyricToFind.indexOf("(") != -1) { + const start = lyricToFind.indexOf("("); + const end = lyricToFind.indexOf(")"); + const endLyric = lyricToFind.slice(end + 1); + lyricToFind = lyricToFind.slice(0, start - 1) + endLyric; + } + let newMessage = "```\n"; + for (let i = 4; i > 0; i--) { + newMessage += tab_lyrics[random - i] + "\n"; + } + lyricToFind.split(" ").forEach(word => { + if (word.includes(",")) newMessage += "_, "; + else newMessage += "_ "; + }); + newMessage += "```"; + message.channel.send(newMessage).then(() => { + var filter = m => m.content.includes(""); + message.channel.awaitMessages(filter, { max: 0, time: 15000 + lyricToFind.split(" ").length * 1000, errors: [] }) + .then(messages => { + message.channel.send("La bonne phrase était: `" + lyricToFind + "`"); + messages.forEach(message => { + const max = Math.max(lyricToFind.length, message.content.length); + const acc = 1 - (levenshtein(lyricToFind.toLowerCase(), message.content.toLowerCase()) / max); + if (acc >= 0.80) { + message.reply("**Gagné!** (" + Math.round(acc * 100) + "%)"); + } + else message.reply("Perdu! (" + Math.round(acc * 100) + "%)"); + }); + }) + .catch(collected => { + console.log(collected); + message.channel.send('Timeout'); + }); + }); + } + }); + }); } }); -client.login(config["discord-token"]); +client.login(config["discord-token"]); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d2bdbf0..c4fa2d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -171,6 +171,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "js-levenshtein": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/js-levenshtein/-/js-levenshtein-1.1.6.tgz", + "integrity": "sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==" + }, "lodash": { "version": "4.17.20", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", diff --git a/package.json b/package.json index 7a34453..6545c9d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "axios": "^0.21.0", "cheerio": "^1.0.0-rc.3", - "discord.js": "^12.5.1" + "discord.js": "^12.5.1", + "js-levenshtein": "^1.1.6" } }