148 lines
4.4 KiB
JavaScript
148 lines
4.4 KiB
JavaScript
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');
|
|
const fs = require("fs");
|
|
|
|
client.on('ready', () => {
|
|
console.log(`Logged in as ${client.user.tag}!`);
|
|
});
|
|
|
|
client.on('message', message => {
|
|
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
|
|
|
|
const args = message.content.slice(config.prefix.length).trim().split(" ");
|
|
const command = args.shift().toLowerCase();
|
|
|
|
if (command === "lyrics") {
|
|
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 {
|
|
for (let i = 9; i < lyrics.length; i += 1900) {
|
|
message.channel.send("```" + lyrics.substr(i, 1900) + "```");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
else if (command === "play") {
|
|
Genius.search_song(args.join(" "), (err, song) => {
|
|
if (err) {
|
|
message.channel.send("Je connais pas \"" + args.join(" ") + "\".");
|
|
} else {
|
|
play(message, song.id);
|
|
}
|
|
});
|
|
}
|
|
|
|
else if (command === "add") {
|
|
Genius.search_song(args.join(" "), (err, song) => {
|
|
if (err) {
|
|
message.channel.send("Je connais pas \"" + args.join(" ") + "\".");
|
|
} else {
|
|
fs.readFile('data.json', (err, data) => {
|
|
if (err) throw err;
|
|
data = JSON.parse(data);
|
|
if (data[message.author.id] === undefined) {
|
|
data[message.author.id] = [];
|
|
}
|
|
data[message.author.id].push({
|
|
id: song.id,
|
|
title: song.title_with_featured,
|
|
artist: song.primary_artist.name
|
|
});
|
|
fs.writeFile('data.json', JSON.stringify(data, null, 4), (err) => {
|
|
if (err) throw err;
|
|
message.reply("J'ai ajouté \"" + song.title + "\" à votre liste");
|
|
});
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
else if (command === "list") {
|
|
getList(message.author.id, songs => {
|
|
if (songs === undefined) {
|
|
message.reply("Vous n'avez pas encore ajouté de musiques");
|
|
} else {
|
|
let newMessage = "```\nListe de " + message.author.username + "\n";
|
|
songs.forEach(song => {
|
|
newMessage += song.artist + " - " + song.title + "\n";
|
|
});
|
|
newMessage += "```";
|
|
message.channel.send(newMessage);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
function play(message, song_id) {
|
|
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');
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
async function getList(user_id, callback) {
|
|
fs.readFile('data.json', (err, data) => {
|
|
if (err) throw err;
|
|
data = JSON.parse(data);
|
|
callback(data[user_id]);
|
|
});
|
|
}
|
|
|
|
client.login(config["discord-token"]); |