Compare commits
10 Commits
6c1331911c
...
2a1811d8dc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a1811d8dc | ||
|
|
cf6cabb8f0 | ||
|
|
584b04a302 | ||
|
|
346ab1abfa | ||
|
|
9259ee7aee | ||
|
|
3050c91f4f | ||
|
|
b728d76f52 | ||
|
|
b0c56c5dd9 | ||
|
|
71ec22194a | ||
|
|
531e1a1d49 |
176
index.js
176
index.js
@@ -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}!`);
|
||||
@@ -10,7 +11,7 @@ client.on('ready', () => {
|
||||
|
||||
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();
|
||||
|
||||
@@ -20,7 +21,7 @@ client.on('message', message => {
|
||||
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) + "```");
|
||||
@@ -31,65 +32,132 @@ 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("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("No result found for \"" + args.join(" ") + "\".");
|
||||
message.channel.send("Je connais pas \"" + 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');
|
||||
});
|
||||
});
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
client.login(config["discord-token"]);
|
||||
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("J'arrive pas à trouver les paroles, réessaye dans quelques secondes bg");
|
||||
}
|
||||
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 * 1500, 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 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 => {
|
||||
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"]);
|
||||
|
||||
Reference in New Issue
Block a user