Added !stats command
This commit is contained in:
parent
08b2d46a43
commit
ef694b116f
7 changed files with 201 additions and 78 deletions
68
libs/Statistics.js
Normal file
68
libs/Statistics.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
const setPeriod = require('../helpers/dates').setPeriod
|
||||
const Queries = require('./Queries')
|
||||
|
||||
class When extends Queries {
|
||||
constructor (models) {
|
||||
super()
|
||||
this.models = models
|
||||
}
|
||||
|
||||
/**
|
||||
* Point d'entrée
|
||||
* @param {Function} botSay
|
||||
* @param {String} where
|
||||
* @param {Array} line
|
||||
*/
|
||||
action (botSay, where, line) {
|
||||
let isOk = true
|
||||
if (line.length < 4) {
|
||||
botSay(where, 'Tu as oublié la période et ou l\'artiste/titre !')
|
||||
return false
|
||||
}
|
||||
|
||||
this.setBaseFilter(line, 3)
|
||||
|
||||
const period = line[2]
|
||||
const filter = this.getFilter()
|
||||
|
||||
switch (period) {
|
||||
case 'day':
|
||||
case 'week':
|
||||
case 'month':
|
||||
case 'year':
|
||||
case 'lastday':
|
||||
case 'lastweek':
|
||||
case 'lastmonth':
|
||||
case 'lastyear':
|
||||
filter.createdAt = setPeriod(period)
|
||||
break
|
||||
default:
|
||||
isOk = false
|
||||
}
|
||||
|
||||
if (!isOk) {
|
||||
botSay(where, 'Période invalide !')
|
||||
return false
|
||||
}
|
||||
|
||||
this.models.Histories
|
||||
.find(filter)
|
||||
.sort({
|
||||
createdAt: 'desc'
|
||||
})
|
||||
.then(items => {
|
||||
if (items.length === '0') {
|
||||
botSay(where, `${this.getValue()} n'a pas était joué pour cette période sur ${process.env.RADIO_ALIAS}`)
|
||||
return true
|
||||
}
|
||||
|
||||
botSay(where, `Pour cette période ${this.getValue()} a été entendu ${items.length} fois`)
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('ERR:', err)
|
||||
botSay(where, 'Impossible de te répondre pour le moment, j\'ai buggé...')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = When
|
Loading…
Add table
Add a link
Reference in a new issue