Added error if no result found

This commit is contained in:
2020-12-19 10:50:10 +01:00
parent e2201ded4a
commit 27632ff226
2 changed files with 57 additions and 53 deletions

View File

@@ -10,9 +10,9 @@ class Genius {
url: "https://api.genius.com/search?q=" + q, url: "https://api.genius.com/search?q=" + q,
headers: {'Authorization': 'Bearer ' + config["genius-token"]} headers: {'Authorization': 'Bearer ' + config["genius-token"]}
}).then(response => { }).then(response => {
return callback(response.data.response.hits[0].result); return callback(null, response.data.response.hits[0].result);
}).catch(err => { }).catch(err => {
console.error(err); return callback(err);
}) })
} }

106
index.js
View File

@@ -31,59 +31,63 @@ client.on('message', message => {
} }
else if (command === "play") { else if (command === "play") {
Genius.search_song(args.join(" "), song => { Genius.search_song(args.join(" "), (err, song) => {
Genius.get_lyrics(song.id, true, (err, lyrics) => { if (err) {
if (err) { message.channel.send("No result found for \"" + args.join(" ") + "\".");
console.log("Error while fetching lyrics: " + err.message); } else {
message.channel.send("Error fetching lyrics, please retry in a few seconds"); Genius.get_lyrics(song.id, true, (err, lyrics) => {
} if (err) {
else { console.log("Error while fetching lyrics: " + err.message);
const tab_lyrics = lyrics.split("\n"); message.channel.send("Error fetching lyrics, please retry in a few seconds");
const new_tab_lyrics = [] }
for (let i = 0; i < tab_lyrics.length; i++) { else {
if (tab_lyrics[i] === "" || tab_lyrics[i].includes("[")) { const tab_lyrics = lyrics.split("\n");
tab_lyrics.splice(i, 1); const new_tab_lyrics = []
i--; 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 random = Math.floor(Math.random() * tab_lyrics.length) + 4; let lyricToFind = tab_lyrics[random];
let lyricToFind = tab_lyrics[random]; while (lyricToFind.indexOf("(") != -1) {
while (lyricToFind.indexOf("(") != -1) { const start = lyricToFind.indexOf("(");
const start = lyricToFind.indexOf("("); const end = lyricToFind.indexOf(")");
const end = lyricToFind.indexOf(")"); const endLyric = lyricToFind.slice(end + 1);
const endLyric = lyricToFind.slice(end + 1); lyricToFind = lyricToFind.slice(0, start - 1) + endLyric;
lyricToFind = lyricToFind.slice(0, start - 1) + endLyric; }
} let newMessage = "```\n";
let newMessage = "```\n"; for (let i = 4; i > 0; i--) {
for (let i = 4; i > 0; i--) { newMessage += tab_lyrics[random - i] + "\n";
newMessage += tab_lyrics[random - i] + "\n"; }
} lyricToFind.split(" ").forEach(word => {
lyricToFind.split(" ").forEach(word => { if (word.includes(",")) newMessage += "_, ";
if (word.includes(",")) newMessage += "_, "; else 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');
}); });
}); 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');
});
});
}
});
}
}); });
} }
}); });