From 5b5217e6515098e46965335d50edf46670593bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Wed, 1 Apr 2020 16:58:08 +0300 Subject: [PATCH] Added statistics to VoiceStateUpdate to save playtime when disconnected manually. Moved statistics updating to statisticsUpdate function. --- commands/play.js | 26 ++++++++++++++++---------- commands/stop.js | 33 +++++++++++++++++---------------- events/voiceStateUpdate.js | 32 +++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/commands/play.js b/commands/play.js index 1dd4941..65a1576 100644 --- a/commands/play.js +++ b/commands/play.js @@ -43,16 +43,7 @@ module.exports = { if (radio) { - if(!radio.currentGuild.statistics[radio.station.name]){ - radio.currentGuild.statistics[radio.station.name] = {}; - radio.currentGuild.statistics[radio.station.name].time = 0; - radio.currentGuild.statistics[radio.station.name].used = 0; - client.datastore.updateEntry(msg.guild, radio.currentGuild); - } - - radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.connection.dispatcher.streamTime.toFixed(0)); - radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1; - client.datastore.updateEntry(msg.guild, radio.currentGuild); + statisticsUpdate(client, msg.guild, radio); radio.connection.dispatcher.destroy(); radio.station = station; @@ -74,6 +65,8 @@ module.exports = { try { const connection = await voiceChannel.join(); construct.connection = connection; + let date = new Date(); + construct.startTime = date.getTime(); play(msg.guild, client, url); client.datastore.checkEntry(msg.guild.id); @@ -117,6 +110,19 @@ function play(guild, client, url) { radio.textChannel.send(client.messageEmojis["play"] + message.play); }; +function statisticsUpdate(client, guild, radio) { + if(!radio.currentGuild.statistics[radio.station.name]){ + radio.currentGuild.statistics[radio.station.name] = {}; + radio.currentGuild.statistics[radio.station.name].time = 0; + radio.currentGuild.statistics[radio.station.name].used = 0; + client.datastore.updateEntry(guild, radio.currentGuild); + } + + radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.connection.dispatcher.streamTime.toFixed(0)); + radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1; + client.datastore.updateEntry(guild, radio.currentGuild); +}; + function searchStation(key, client) { if (client.stations === null) return false; let foundStations = []; diff --git a/commands/stop.js b/commands/stop.js index 122a574..73cbb84 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -8,25 +8,26 @@ module.exports = { execute(msg, args, client, Discord, command) { const radio = client.radio.get(msg.guild.id); if (client.funcs.check(client, msg, command)) { - - client.datastore.checkEntry(msg.guild.id); - radio.currentGuild = client.datastore.getEntry(msg.guild.id); - if(!radio.currentGuild.statistics[radio.station.name]){ - radio.currentGuild.statistics[radio.station.name] = {}; - radio.currentGuild.statistics[radio.station.name].time = 0; - radio.currentGuild.statistics[radio.station.name].used = 0; - client.datastore.updateEntry(msg.guild, radio.currentGuild); - } - - radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.connection.dispatcher.streamTime.toFixed(0)); - radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1; - client.datastore.updateEntry(msg.guild, radio.currentGuild); - - + statisticsUpdate(client, msg.guild, radio); radio.connection.dispatcher.destroy(); radio.voiceChannel.leave(); client.radio.delete(msg.guild.id); msg.channel.send(client.messageEmojis["stop"] + client.messages.stop); } } -}; \ No newline at end of file +}; + +function statisticsUpdate(client, guild, radio) { + client.datastore.checkEntry(guild.id); + radio.currentGuild = client.datastore.getEntry(guild.id); + if(!radio.currentGuild.statistics[radio.station.name]){ + radio.currentGuild.statistics[radio.station.name] = {}; + radio.currentGuild.statistics[radio.station.name].time = 0; + radio.currentGuild.statistics[radio.station.name].used = 0; + client.datastore.updateEntry(guild, radio.currentGuild); + } + + radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.connection.dispatcher.streamTime.toFixed(0)); + radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1; + client.datastore.updateEntry(guild, radio.currentGuild); +} \ No newline at end of file diff --git a/events/voiceStateUpdate.js b/events/voiceStateUpdate.js index a1973c6..93f06d0 100644 --- a/events/voiceStateUpdate.js +++ b/events/voiceStateUpdate.js @@ -7,6 +7,7 @@ module.exports = { if (!radio) return; if (newState.member.id === client.user.id && oldState.member.id === client.user.id) { if (newState.channel === null) { + statisticsUpdate(client, newState, radio); return client.radio.delete(newState.guild.id); } @@ -16,9 +17,10 @@ module.exports = { const connection = await oldState.channel.join(); return radio.connection = connection; } catch (error) { + statisticsUpdate(client, newState, radio); radio.connection.dispatcher.destroy(); radio.voiceChannel.leave(); - client.radio.delete(msg.guild.id); + client.radio.delete(oldState.guild.id); return; } } @@ -32,6 +34,7 @@ module.exports = { setTimeout(() => { if (!radio) return; if (radio.voiceChannel.members.size === 1) { + statisticsUpdate(client, newState, radio); radio.connection.dispatcher.destroy(); radio.voiceChannel.leave(); client.radio.delete(newState.guild.id); @@ -39,4 +42,31 @@ module.exports = { }, 120000); } } +}; + +function statisticsUpdate(client, currentState, radio) { + + client.datastore.checkEntry(currentState.guild.id); + + radio.currentGuild = client.datastore.getEntry(currentState.guild.id); + + if(!radio.currentGuild.statistics[radio.station.name]){ + radio.currentGuild.statistics[radio.station.name] = {}; + radio.currentGuild.statistics[radio.station.name].time = 0; + radio.currentGuild.statistics[radio.station.name].used = 0; + client.datastore.updateEntry(currentState.guild, radio.currentGuild); + } + + if(!radio.connection.dispatcher){ + let date = new Date(); + radio.currentTime = date.getTime(); + radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime); + radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.playTime); + } else { + radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.connection.dispatcher.streamTime.toFixed(0)); + } + + radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1; + client.datastore.updateEntry(currentState.guild, radio.currentGuild); + } \ No newline at end of file