From ac6cdf9f2ff162e2acfcba70c5e3acec6b261b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Sat, 4 Apr 2020 06:53:17 +0300 Subject: [PATCH] Added calculateGlobal function to datastore and if playlist doesnt exist then bot's status is dnd. --- client/datastore.js | 41 +++++++++++++++++++++++++++++++++++++++++ client/events/ready.js | 7 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/client/datastore.js b/client/datastore.js index a412d3e..99d3485 100644 --- a/client/datastore.js +++ b/client/datastore.js @@ -23,6 +23,47 @@ module.exports = class { //console.log(""); } + calculateGlobal(client){ + let guilds = this.map.keys(); + let stations = client.stations; + let statistics = {}; + + if(!client.stations) return; + + let calculation = guilds.next(); + + while (!calculation.done) { + let currentGuild = this.getEntry(calculation.value); + if(calculation.value == 'global'){ + return calculation = guilds.next(); + } + + if(stations){ + Object.keys(stations).forEach(function(station) { + if(currentGuild.statistics[stations[station].name] && currentGuild.statistics[stations[station].name].time && !parseInt(currentGuild.statistics[stations[station].name].time) == 0 && currentGuild.statistics[stations[station].name].used && !parseInt(currentGuild.statistics[stations[station].name].used) == 0){ + if(!statistics[stations[station].name]){ + statistics[stations[station].name] = {}; + statistics[stations[station].name].time = 0; + statistics[stations[station].name].used = 0; + } + + statistics[stations[station].name].time = parseInt(statistics[stations[station].name].time)+parseInt(currentGuild.statistics[stations[station].name].time); + statistics[stations[station].name].used = parseInt(statistics[stations[station].name].used)+parseInt(currentGuild.statistics[stations[station].name].used); + } + }); + } + calculation = guilds.next(); + } + + let newData = {}; + newData.guild = {}; + newData.guild.id = "global"; + newData.guild.name = "global"; + newData.statistics = statistics; + this.updateEntry(newData.guild, newData); + } + + checkEntry(id){ if(!this.map.has(id)){ this.createEntry(id); diff --git a/client/events/ready.js b/client/events/ready.js index 1ca6f8e..81fac5c 100644 --- a/client/events/ready.js +++ b/client/events/ready.js @@ -23,7 +23,6 @@ module.exports = { client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') .then(res => res.json()); } catch (err) { - client.stations = null; console.error(err); } @@ -32,6 +31,12 @@ module.exports = { .then(res => res.json()); }, 3600000); + if(!client.stations) { + client.user.setStatus('dnd'); + } + + client.datastore.calculateGlobal(client); require(`../emojis.js`).execute(client); + } } \ No newline at end of file