Added !list command

This commit is contained in:
dbroqua 2019-12-25 13:47:42 +01:00
parent ef694b116f
commit 5a15cdf7b4
8 changed files with 211 additions and 77 deletions

View File

@ -7,4 +7,5 @@
* ~~!stats <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~ * ~~!stats <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~
* ~~!when <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~ * ~~!when <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~
* ~~!help {artist/song/stats/when}~~ * ~~!help {artist/song/stats/when}~~
* !notifications <on/off/state> * !notifications <on/off/state>
* ~~!list <day/week/lastweek/month/lastmonth/year/lastyear> <artist>~~

View File

@ -1,56 +0,0 @@
const moment = require('moment')
/**
* Fonction permettant de générer le filtre de date sur les commandes !stats et !when
* @param {String} period
*/
function setPeriod (period) {
switch (period) {
case 'day':
return {
$gte: moment().startOf('day'),
$lte: moment().endOf('day')
}
case 'week':
return {
$gte: moment().startOf('week'),
$lte: moment().endOf('day')
}
case 'month':
return {
$gte: moment().startOf('month'),
$lte: moment().endOf('day')
}
case 'year':
return {
$gte: moment().startOf('year'),
$lte: moment().endOf('day')
}
case 'lastday':
return {
$gte: moment().subtract('day').startOf('day'),
$lte: moment().subtract('day').endOf('day')
}
case 'lastweek':
return {
$gte: moment().subtract(1, 'week').startOf('week'),
$lte: moment().subtract(1, 'week').endOf('week')
}
case 'lastmonth':
return {
$gte: moment().subtract(1, 'month').startOf('month'),
$lte: moment().subtract(1, 'month').endOf('month')
}
case 'lastyear':
return {
$gte: moment().subtract(1, 'year').startOf('year'),
$lte: moment().subtract(1, 'year').endOf('year')
}
default:
return {}
}
}
module.exports = {
setPeriod: setPeriod
}

View File

@ -6,13 +6,15 @@ const Help = require('./libs/Help')
const ArtistSong = require('./libs/ArtistSong') const ArtistSong = require('./libs/ArtistSong')
const When = require('./libs/When') const When = require('./libs/When')
const Statistics = require('./libs/Statistics') const Statistics = require('./libs/Statistics')
const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications'] const List = require('./libs/List')
const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list']
// Init des librairies // Init des librairies
const db = new Db(models) const db = new Db(models)
const artistSong = new ArtistSong(models) const artistSong = new ArtistSong(models)
const when = new When(models) const when = new When(models)
const stats = new Statistics(models) const stats = new Statistics(models)
const list = new List(models)
// Stockage du morceau en cours de lecture // Stockage du morceau en cours de lecture
let currentSong = { let currentSong = {
@ -71,9 +73,14 @@ const actions = (where, message, from) => {
case '!stats': case '!stats':
stats.action(botSay, where, exploded) stats.action(botSay, where, exploded)
break break
case '!list':
list.action(botSay, where, exploded)
break
default: default:
break break
} }
} else {
botSay(where, 'Commande inconnue, tape !help si tu es perdu')
} }
} }
} }

View File

@ -19,6 +19,9 @@ class Help {
case '!when': case '!when':
Help.showWhen(say, where) Help.showWhen(say, where)
break break
case '!list':
Help.showList(say, where)
break
case '!notifications': case '!notifications':
Help.showNotifications(say, where) Help.showNotifications(say, where)
break break
@ -29,9 +32,10 @@ class Help {
say(where, 'IRC Radio Bot :: Help') say(where, 'IRC Radio Bot :: Help')
say(where, '!artist <add/del> {nom à sauvegarder si différent du morceau en cours}') say(where, '!artist <add/del> {nom à sauvegarder si différent du morceau en cours}')
say(where, '!song <add/del> {nom à sauvegarder si différent du morceau en cours}') say(where, '!song <add/del> {nom à sauvegarder si différent du morceau en cours}')
say(where, '!stats <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>') say(where, '!stats <period> <artist/song>')
say(where, '!when <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>') say(where, '!when <period> <artist/song>')
say(where, '!help {!artist/!song/!stats/!when}') say(where, '!list <period> <artist/song>')
say(where, '!help {command}')
say(where, '!notifications <on/off/state>') say(where, '!notifications <on/off/state>')
} }
say(where, '__ END __') say(where, '__ END __')
@ -85,6 +89,24 @@ class Help {
say(where, '!when lastyear <title>') say(where, '!when lastyear <title>')
} }
/**
* Fonction affichant l'aide sur la commande !list
* @param {Function} say
* @param {String} where
*/
static showList (say, where) {
say(where, 'IRC Radio Bot :: List')
say(where, 'Permet d\'afficher la liste des titres joués pour un artiste sr une période donnée')
say(where, '!list day <artist>')
say(where, '!list week <artist>')
say(where, '!list month <artist>')
say(where, '!list year <artist>')
say(where, '!list lastday <artist>')
say(where, '!list lastweek <artist>')
say(where, '!list lastmonth <artist>')
say(where, '!list lastyear <artist>')
}
/** /**
* Fonction affichant l'aide sur la commande !notifications * Fonction affichant l'aide sur la commande !notifications
* @param {Function} say * @param {Function} say

83
libs/List.js Normal file
View File

@ -0,0 +1,83 @@
const Queries = require('./Queries')
const moment = require('moment')
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) {
if (line.length < 4) {
botSay(where, 'Tu as oublié la période et ou l\'artiste !')
return false
}
let isOk = true
this.setBaseFilter(line, 3, false)
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':
this.setPeriod(period)
filter.createdAt = this.period
break
default:
isOk = false
}
if (!isOk) {
botSay(where, 'Période invalide !')
return false
}
this.models.Histories
.find(filter)
.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
} else {
const list = []
for (let i = 0; i < items.length; i += 1) {
if (list.indexOf(items[i].title) === -1) {
list.push(items[i].title)
}
}
// list.push('__ END __')
let index = 0
const max = list.length
const _call = setInterval(() => {
botSay(where, `${index + 1}/${max} - ${list[index]}`)
index += 1
if (index === list.length) {
clearInterval(_call)
}
}, 750)
}
})
.catch(err => {
console.log('ERR:', err)
botSay(where, 'Impossible de te répondre pour le moment, j\'ai buggé...')
})
}
}
module.exports = When

View File

@ -1,37 +1,106 @@
const moment = require('moment')
const formatString = require('../helpers/strings').formatString const formatString = require('../helpers/strings').formatString
class Queries { class Queries {
constructor () { constructor () {
this.filter = null this.filter = null
this.value = null this.value = null
this.period = null
} }
/** /**
* Fonction permettant de générer la query sur artist et title * Fonction permettant de générer la query sur artist et title
* @param {Array} line * @param {Array} line
* @param {Integer} start * @param {Integer} start
* @param {Boolean} artistAndTitle
*/ */
setBaseFilter (line, start) { setBaseFilter (line, start, artistAndTitle) {
let value = '' let value = ''
for (let i = start; i < line.length; i += 1) { for (let i = start; i < line.length; i += 1) {
value += ' ' + line[i] value += ' ' + line[i]
} }
value = value.replace(' ', '') this.value = value.replace(' ', '')
const filter = { value = formatString(this.value)
$or: [
{ if (artistAndTitle) {
artist: formatString(value) this.filter = {
}, $or: [
{ {
title: formatString(value) artist: value
} },
] {
title: value
}
]
}
} else {
this.filter = {
artist: value
}
} }
this.filter = filter
this.value = value this.value = value
} }
/**
* Fonction permettant de générer le filtre de date sur les commandes !stats et !when
* @param {String} period
*/
setPeriod (period) {
switch (period) {
case 'day':
this.period = {
$gte: moment().startOf('day'),
$lte: moment().endOf('day')
}
break
case 'week':
this.period = {
$gte: moment().startOf('week'),
$lte: moment().endOf('day')
}
break
case 'month':
this.period = {
$gte: moment().startOf('month'),
$lte: moment().endOf('day')
}
break
case 'year':
this.period = {
$gte: moment().startOf('year'),
$lte: moment().endOf('day')
}
break
case 'lastday':
this.period = {
$gte: moment().subtract('day').startOf('day'),
$lte: moment().subtract('day').endOf('day')
}
break
case 'lastweek':
this.period = {
$gte: moment().subtract(1, 'week').startOf('week'),
$lte: moment().subtract(1, 'week').endOf('week')
}
break
case 'lastmonth':
this.period = {
$gte: moment().subtract(1, 'month').startOf('month'),
$lte: moment().subtract(1, 'month').endOf('month')
}
break
case 'lastyear':
this.period = {
$gte: moment().subtract(1, 'year').startOf('year'),
$lte: moment().subtract(1, 'year').endOf('year')
}
break
default:
this.period = {}
}
}
/** /**
* Getter pour le filter * Getter pour le filter
* @return {Object} * @return {Object}
@ -47,6 +116,14 @@ class Queries {
getValue () { getValue () {
return this.value return this.value
} }
/**
* Getter pour la période
* @return {String}
*/
getPeriod () {
return this.period
}
} }
module.exports = Queries module.exports = Queries

View File

@ -1,4 +1,3 @@
const setPeriod = require('../helpers/dates').setPeriod
const Queries = require('./Queries') const Queries = require('./Queries')
class When extends Queries { class When extends Queries {
@ -34,7 +33,8 @@ class When extends Queries {
case 'lastweek': case 'lastweek':
case 'lastmonth': case 'lastmonth':
case 'lastyear': case 'lastyear':
filter.createdAt = setPeriod(period) this.setPeriod(period)
filter.createdAt = this.period
break break
default: default:
isOk = false isOk = false

View File

@ -1,4 +1,3 @@
const setPeriod = require('../helpers/dates').setPeriod
const Queries = require('./Queries') const Queries = require('./Queries')
const moment = require('moment') const moment = require('moment')
@ -35,7 +34,8 @@ class When extends Queries {
case 'lastweek': case 'lastweek':
case 'lastmonth': case 'lastmonth':
case 'lastyear': case 'lastyear':
filter.createdAt = setPeriod(period) this.setPeriod(period)
filter.createdAt = this.period
break break
default: default:
isOk = false isOk = false