From 766cd4cbb69276c3248d178bda947ac5c5dbe368 Mon Sep 17 00:00:00 2001 From: "lucas.mathieu" Date: Sat, 19 Dec 2020 12:09:59 +0100 Subject: [PATCH] Added list and add commands --- data.json | 3 +++ index.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 data.json diff --git a/data.json b/data.json new file mode 100644 index 0000000..4eb7c3f --- /dev/null +++ b/data.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/index.js b/index.js index 296ded0..fda6c13 100644 --- a/index.js +++ b/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}!`); @@ -90,6 +91,49 @@ client.on('message', message => { } }); } + + 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") { + fs.readFile('data.json', (err, data) => { + if (err) throw err; + data = JSON.parse(data); + const songs = data[message.author.id]; + 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"]); \ No newline at end of file