Added statistics command and datastore for statistics

This commit is contained in:
Christer Warén 2020-03-31 09:53:58 +03:00
parent 3ad80310d1
commit 3da41e44cb
7 changed files with 190 additions and 3 deletions

View File

@ -38,14 +38,27 @@ module.exports = {
const sstation = await searchStation(args.slice(1).join(' '), client);
if (!sstation) return msg.channel.send(client.messageEmojis["x"] + client.messages.noSearchResults);
url = sstation.stream[sstation.stream.default];
station = sstation
station = sstation;
}
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);
radio.connection.dispatcher.destroy();
radio.station = station;
radio.textChannel = msg.channel;
play(msg.guild, client, url);
return;
}
@ -62,6 +75,17 @@ module.exports = {
const connection = await voiceChannel.join();
construct.connection = connection;
play(msg.guild, client, url);
client.datastore.checkEntry(msg.guild.id);
construct.currentGuild = client.datastore.getEntry(msg.guild.id);
if(!construct.currentGuild.statistics[construct.station.name]){
construct.currentGuild.statistics[construct.station.name] = {};
construct.currentGuild.statistics[construct.station.name].time = 0;
construct.currentGuild.statistics[construct.station.name].used = 0;
client.datastore.updateEntry(msg.guild, construct.currentGuild);
}
} catch (error) {
client.radio.delete(msg.guild.id);
return msg.channel.send(client.messageEmojis["x"] + `An error occured: ${error}`);

59
commands/statistics.js Normal file
View File

@ -0,0 +1,59 @@
module.exports = {
name: 'statistics',
alias: 'stats',
usage: '',
description: 'Show usage statistics.',
permission: 'none',
category: 'info',
execute(msg, args, client, Discord, command) {
let stations = client.stations;
let currentGuildStatistics = client.datastore.getEntry(msg.guild.id).statistics;
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";
}
i++;
}
});
if(!statistics){
statistics = "You have not listened any radio station";
}
const embed = new Discord.MessageEmbed()
.setTitle(client.messages.statisticsTitle)
.setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["list"].replace(/[^0-9]+/g, ''))
.setColor(client.config.embedColor)
.setDescription(statistics)
.setFooter('EximiaBots by Warén Media', "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, ''));
return msg.channel.send(embed);
}
};
function msToTime(duration, format) {
var seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 24);
days = (days < 10) ? "0" + days : days;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (format === "hh:mm:ss") {
return `${hours}:${minutes}:${seconds}`;
} else if (format === "dd:hh:mm:ss") {
return `${days}:${hours}:${minutes}:${seconds}`;
}
}

View File

@ -8,6 +8,21 @@ 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);
radio.connection.dispatcher.destroy();
radio.voiceChannel.leave();
client.radio.delete(msg.guild.id);

View File

@ -4,7 +4,8 @@
"description": "We will bring you finnish radio to your discord server",
"main": "index.js",
"scripts": {
"start": "node ."
"start": "node .",
"install": "mkdir datastore"
},
"repository": {
"type": "git",

View File

@ -3,6 +3,7 @@ const Discord = require('discord.js');
const fs = require('fs');
const path = require('path');
const events = '../events/';
const Datastore = require('./datastore.js');
module.exports = class extends Client {
constructor() {
@ -28,6 +29,7 @@ module.exports = class extends Client {
this.on('ready', () => {
require(`${events}ready`).execute(this, Discord);
this.datastore = new Datastore();
});
this.on('message', (msg) => {
require(`${events}msg`).execute(this, msg, Discord);

85
struct/datastore.js Normal file
View File

@ -0,0 +1,85 @@
const fs = require('fs');
const path = require('path');
module.exports = class {
constructor() {
this.map = new Map();
this.loadData();
}
loadData() {
console.log("");
const dataFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'datastore')).filter(f => f.endsWith('.json'));
for (const file of dataFiles) {
try {
const json = require(`../datastore/${file}`);
this.map.set(json.guild.id, json);
//console.log('[LOADED] ' + file + " (" + json.guild.id + ")");
//console.log(JSON.stringify(json, null, 4));
} catch (error) {
//console.log('[ERROR] Loading ' + file + ' failed');
}
}
console.log("");
}
checkEntry(id){
if(!this.map.has(id)){
this.createEntry(id);
//this.showEntry(this.getEntry(id));
} else {
//this.showEntry(this.getEntry(id));
}
}
createEntry(id){
let newData = {};
newData.guild = {};
newData.guild.id = id;
newData.statistics = {};
this.map.set(id, newData);
this.saveEntry(id, newData);
}
getEntry(id){
return this.map.get(id);
}
updateEntry(guild, newData) {
newData.guild.name = guild.name;
this.map.set(guild.id, newData);
this.saveEntry(guild.id, newData);
//this.showEntry(this.getEntry(guild.id));
}
showEntry(data){
console.log(data);
}
createTestFile () {
let newData = {
"guild": {
"id": "test",
"name": "Test"
},
"statistics": {
"test": {
"time": 0,
"used": 0
}
}
}
this.updateEntry(newData.guild, newData);
}
saveEntry(file, data) {
data = JSON.stringify(data, null, 4);
fs.writeFile(path.join(path.dirname(__dirname), 'datastore') + "/" + file + ".json", data, function(err) {
if (err) {
//console.log(err);
}
});
}
};

View File

@ -28,5 +28,6 @@ module.exports = {
maxVolume: "The max volume is `100`!",
invalidVolume: "You need to enter a valid __number__.",
negativeVolume: "The volume needs to be a positive number!",
newVolume: "Volume is now: **%volume%**"
newVolume: "Volume is now: **%volume%**",
statisticsTitle: "Statistics"
};