Compare commits

...

10 Commits

Author SHA1 Message Date
Lucas
2a1811d8dc Nagui à l'école 2020-12-19 13:58:30 +01:00
Lucas
cf6cabb8f0 Nagui parle toujours FR 2020-12-19 13:54:08 +01:00
Clement Prost
584b04a302 Changed message when you loose 2020-12-19 13:30:46 +01:00
Lucas
346ab1abfa Changed time 2020-12-19 13:17:24 +01:00
Lucas
9259ee7aee Improved formatting 2020-12-19 12:30:27 +01:00
Lucas
3050c91f4f Added random play 2020-12-19 12:27:48 +01:00
Lucas
b728d76f52 Extracted getList to a function 2020-12-19 12:20:58 +01:00
Lucas
b0c56c5dd9 Extracted play to a function 2020-12-19 12:15:32 +01:00
Lucas
71ec22194a Added list and add commands 2020-12-19 12:09:59 +01:00
Lucas
531e1a1d49 Nagui parle fr 2020-12-19 10:52:00 +01:00
2 changed files with 125 additions and 54 deletions

3
data.json Normal file
View File

@@ -0,0 +1,3 @@
{
}

View File

@@ -3,6 +3,7 @@ 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}!`);
@@ -31,14 +32,71 @@ client.on('message', message => {
}
else if (command === "play") {
if (args.length == 0) {
getList(message.author.id, songs => {
if (songs === undefined) {
message.channel.send("Vous devez ajouter des musiques à votre liste pour jouer aléatoirement.");
} else {
play(message, songs[Math.floor(Math.random() * songs.length)].id);
}
})
} else {
Genius.search_song(args.join(" "), (err, song) => {
if (err) {
message.channel.send("No result found for \"" + args.join(" ") + "\".");
message.channel.send("Je connais pas \"" + args.join(" ") + "\".");
} else {
Genius.get_lyrics(song.id, true, (err, lyrics) => {
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");
message.channel.send("J'arrive pas à trouver les paroles, réessaye dans quelques secondes bg");
}
else {
const tab_lyrics = lyrics.split("\n");
@@ -68,7 +126,7 @@ client.on('message', message => {
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: [] })
message.channel.awaitMessages(filter, { max: 0, time: 15000 + lyricToFind.split(" ").length * 1500, errors: [] })
.then(messages => {
message.channel.send("La bonne phrase était: `" + lyricToFind + "`");
messages.forEach(message => {
@@ -77,7 +135,12 @@ client.on('message', message => {
if (acc >= 0.80) {
message.reply("**Gagné!** (" + Math.round(acc * 100) + "%)");
}
else message.reply("Perdu! (" + Math.round(acc * 100) + "%)");
else if (acc <= 0.60) {
message.reply("Perdu! (" + Math.round(acc * 100) + "%)");
}
else{
message.reply("Retourne en cours de francais sale merde! (" + Math.round(acc * 100) + "%)");
}
});
})
.catch(collected => {
@@ -88,8 +151,13 @@ client.on('message', message => {
}
});
}
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"]);