Added error if no result found
This commit is contained in:
106
index.js
106
index.js
@@ -31,59 +31,63 @@ client.on('message', message => {
|
||||
}
|
||||
|
||||
else if (command === "play") {
|
||||
Genius.search_song(args.join(" "), song => {
|
||||
Genius.get_lyrics(song.id, true, (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--;
|
||||
Genius.search_song(args.join(" "), (err, song) => {
|
||||
if (err) {
|
||||
message.channel.send("No result found for \"" + args.join(" ") + "\".");
|
||||
} else {
|
||||
Genius.get_lyrics(song.id, true, (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;
|
||||
let 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');
|
||||
const random = Math.floor(Math.random() * tab_lyrics.length) + 4;
|
||||
let 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');
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user