Extracted play to a function

This commit is contained in:
2020-12-19 12:15:32 +01:00
parent 766cd4cbb6
commit d1ed48b3fe

100
index.js
View File

@@ -36,7 +36,57 @@ client.on('message', message => {
if (err) {
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") {
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);
}
});
}
});
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");
@@ -88,52 +138,6 @@ 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"]);