Added list and add commands

This commit is contained in:
2020-12-19 12:09:59 +01:00
parent 3e11fe5fb9
commit 766cd4cbb6
2 changed files with 47 additions and 0 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}!`);
@@ -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"]);