From 20d5d8b3bd7af7589104c0510e73251c138f5537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Tue, 31 Mar 2020 10:23:46 +0300 Subject: [PATCH] Fixed file encoding issue & statistics command when guild has empty statistics --- commands/statistics.js | 30 ++++++++++++++++-------------- struct/datastore.js | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/commands/statistics.js b/commands/statistics.js index 0bad094..35268a3 100644 --- a/commands/statistics.js +++ b/commands/statistics.js @@ -7,24 +7,26 @@ module.exports = { category: 'info', execute(msg, args, client, Discord, command) { let stations = client.stations; - let currentGuildStatistics = client.datastore.getEntry(msg.guild.id).statistics; + let currentGuild = client.datastore.getEntry(msg.guild.id); let statistics; let i = 0; - Object.keys(client.stations).forEach(function(station) { - if(currentGuildStatistics[stations[station].name]){ - if(i > 0){ - statistics += "**" + station + " " + stations[station].name + "** \n"; - statistics += "Time: " + msToTime(currentGuildStatistics[stations[station].name].time, "hh:mm:ss") + "\n"; - statistics += "Used: " + currentGuildStatistics[stations[station].name].used + "\n"; - } else { - statistics = "**" + station + " " + stations[station].name + "** \n"; - statistics += "Time: " + msToTime(currentGuildStatistics[stations[station].name].time, "hh:mm:ss") + "\n"; - statistics += "Used: " + currentGuildStatistics[stations[station].name].used + "\n"; + if(currentGuild.statistics){ + Object.keys(client.stations).forEach(function(station) { + if(currentGuild.statistics[stations[station].name]){ + if(i > 0){ + statistics += "**" + station + " " + stations[station].name + "** \n"; + statistics += "Time: " + msToTime(currentGuild.statistics[stations[station].name].time, "hh:mm:ss") + "\n"; + statistics += "Used: " + currentGuild.statistics[stations[station].name].used + "\n"; + } else { + statistics = "**" + station + " " + stations[station].name + "** \n"; + statistics += "Time: " + msToTime(currentGuild.statistics[stations[station].name].time, "hh:mm:ss") + "\n"; + statistics += "Used: " + currentGuild.statistics[stations[station].name].used + "\n"; + } + i++; } - i++; - } - }); + }); + } if(!statistics){ statistics = "You have not listened any radio station"; diff --git a/struct/datastore.js b/struct/datastore.js index a37d691..d034e25 100644 --- a/struct/datastore.js +++ b/struct/datastore.js @@ -76,7 +76,7 @@ module.exports = class { saveEntry(file, data) { data = JSON.stringify(data, null, 4); - fs.writeFile(path.join(path.dirname(__dirname), 'datastore') + "/" + file + ".json", data, function(err) { + fs.writeFile(path.join(path.dirname(__dirname), 'datastore') + "/" + file + ".json", data, 'utf8', function(err) { if (err) { //console.log(err); }