From 5a15cdf7b45fc457e1d78ce92a890390189a3aee Mon Sep 17 00:00:00 2001 From: dbroqua Date: Wed, 25 Dec 2019 13:47:42 +0100 Subject: [PATCH] Added !list command --- TODO.md | 3 +- helpers/dates.js | 56 ------------------------- index.js | 9 +++- libs/Help.js | 28 +++++++++++-- libs/List.js | 83 +++++++++++++++++++++++++++++++++++++ libs/Queries.js | 101 +++++++++++++++++++++++++++++++++++++++------ libs/Statistics.js | 4 +- libs/When.js | 4 +- 8 files changed, 211 insertions(+), 77 deletions(-) delete mode 100644 helpers/dates.js create mode 100644 libs/List.js diff --git a/TODO.md b/TODO.md index 192a353..5d1588a 100644 --- a/TODO.md +++ b/TODO.md @@ -7,4 +7,5 @@ * ~~!stats ~~ * ~~!when ~~ * ~~!help {artist/song/stats/when}~~ -* !notifications \ No newline at end of file +* !notifications +* ~~!list ~~ \ No newline at end of file diff --git a/helpers/dates.js b/helpers/dates.js deleted file mode 100644 index 41eaba2..0000000 --- a/helpers/dates.js +++ /dev/null @@ -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 -} diff --git a/index.js b/index.js index d711f69..3165420 100644 --- a/index.js +++ b/index.js @@ -6,13 +6,15 @@ const Help = require('./libs/Help') const ArtistSong = require('./libs/ArtistSong') const When = require('./libs/When') 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 const db = new Db(models) const artistSong = new ArtistSong(models) const when = new When(models) const stats = new Statistics(models) +const list = new List(models) // Stockage du morceau en cours de lecture let currentSong = { @@ -71,9 +73,14 @@ const actions = (where, message, from) => { case '!stats': stats.action(botSay, where, exploded) break + case '!list': + list.action(botSay, where, exploded) + break default: break } + } else { + botSay(where, 'Commande inconnue, tape !help si tu es perdu') } } } diff --git a/libs/Help.js b/libs/Help.js index 202df95..6fffc61 100644 --- a/libs/Help.js +++ b/libs/Help.js @@ -19,6 +19,9 @@ class Help { case '!when': Help.showWhen(say, where) break + case '!list': + Help.showList(say, where) + break case '!notifications': Help.showNotifications(say, where) break @@ -29,9 +32,10 @@ class Help { say(where, 'IRC Radio Bot :: Help') say(where, '!artist {nom à sauvegarder si différent du morceau en cours}') say(where, '!song {nom à sauvegarder si différent du morceau en cours}') - say(where, '!stats ') - say(where, '!when ') - say(where, '!help {!artist/!song/!stats/!when}') + say(where, '!stats ') + say(where, '!when ') + say(where, '!list ') + say(where, '!help {command}') say(where, '!notifications ') } say(where, '__ END __') @@ -85,6 +89,24 @@ class Help { say(where, '!when lastyear ') } + /** + * 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 * @param {Function} say diff --git a/libs/List.js b/libs/List.js new file mode 100644 index 0000000..9df1bf3 --- /dev/null +++ b/libs/List.js @@ -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 diff --git a/libs/Queries.js b/libs/Queries.js index 7e579bd..a70d97e 100644 --- a/libs/Queries.js +++ b/libs/Queries.js @@ -1,37 +1,106 @@ +const moment = require('moment') const formatString = require('../helpers/strings').formatString class Queries { constructor () { this.filter = null this.value = null + this.period = null } /** * Fonction permettant de générer la query sur artist et title * @param {Array} line * @param {Integer} start + * @param {Boolean} artistAndTitle */ - setBaseFilter (line, start) { + setBaseFilter (line, start, artistAndTitle) { let value = '' for (let i = start; i < line.length; i += 1) { value += ' ' + line[i] } - value = value.replace(' ', '') - const filter = { - $or: [ - { - artist: formatString(value) - }, - { - title: formatString(value) - } - ] + this.value = value.replace(' ', '') + value = formatString(this.value) + + if (artistAndTitle) { + this.filter = { + $or: [ + { + artist: value + }, + { + title: value + } + ] + } + } else { + this.filter = { + artist: value + } } - this.filter = filter 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 * @return {Object} @@ -47,6 +116,14 @@ class Queries { getValue () { return this.value } + + /** + * Getter pour la période + * @return {String} + */ + getPeriod () { + return this.period + } } module.exports = Queries diff --git a/libs/Statistics.js b/libs/Statistics.js index 1a3a6a1..f101c9f 100644 --- a/libs/Statistics.js +++ b/libs/Statistics.js @@ -1,4 +1,3 @@ -const setPeriod = require('../helpers/dates').setPeriod const Queries = require('./Queries') class When extends Queries { @@ -34,7 +33,8 @@ class When extends Queries { case 'lastweek': case 'lastmonth': case 'lastyear': - filter.createdAt = setPeriod(period) + this.setPeriod(period) + filter.createdAt = this.period break default: isOk = false diff --git a/libs/When.js b/libs/When.js index 8eb401e..2b70adc 100644 --- a/libs/When.js +++ b/libs/When.js @@ -1,4 +1,3 @@ -const setPeriod = require('../helpers/dates').setPeriod const Queries = require('./Queries') const moment = require('moment') @@ -35,7 +34,8 @@ class When extends Queries { case 'lastweek': case 'lastmonth': case 'lastyear': - filter.createdAt = setPeriod(period) + this.setPeriod(period) + filter.createdAt = this.period break default: isOk = false