From 4151ab95f88e4a9afb5f09340c5fa75426bbe98b Mon Sep 17 00:00:00 2001 From: dbroqua Date: Wed, 25 Dec 2019 12:47:09 +0100 Subject: [PATCH] Added !when command --- TODO.md | 8 ++--- helpers/dates.js | 50 ++++++++++++++++++++++++++ helpers/strings.js | 8 +++++ index.js | 5 +++ libs/ArtistSong.js | 6 ++-- libs/When.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 helpers/dates.js create mode 100644 helpers/strings.js create mode 100644 libs/When.js diff --git a/TODO.md b/TODO.md index 946aa7f..349a40b 100644 --- a/TODO.md +++ b/TODO.md @@ -2,9 +2,9 @@ ## Les commandes -* !artist {nom de l'artiste} -* !song {nom du morceau} +* ~~!artist {nom de l'artiste}~~ +* ~~!song {nom du morceau}~~ * !stats -* !when -* !help {artist/song/stats/when} +* ~~!when ~~ +* ~~!help {artist/song/stats/when}~~ * !notifications \ No newline at end of file diff --git a/helpers/dates.js b/helpers/dates.js new file mode 100644 index 0000000..d5f43b8 --- /dev/null +++ b/helpers/dates.js @@ -0,0 +1,50 @@ +const moment = require('moment') + +module.exports = { + 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 {} + } + } +} diff --git a/helpers/strings.js b/helpers/strings.js new file mode 100644 index 0000000..7fa2bc9 --- /dev/null +++ b/helpers/strings.js @@ -0,0 +1,8 @@ +module.exports = { + formatString: function (string) { + if (string !== undefined && string !== null) { + return new RegExp('^' + string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$', 'i') + } + return ';' + } +} diff --git a/index.js b/index.js index feecaa0..19a3955 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,13 @@ const Db = require('./libs/Db') const models = require('./models').models const Help = require('./libs/Help') const ArtistSong = require('./libs/ArtistSong') +const When = require('./libs/When') const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications'] // Init des librairies const db = new Db(models) const artistSong = new ArtistSong(models) +const when = new When(models) // Stockage du morceau en cours de lecture let currentSong = { @@ -61,6 +63,9 @@ const actions = (where, message, from) => { case '!help': Help.show(botSay, where, exploded) break + case '!when': + when.action(botSay, where, exploded) + break default: break } diff --git a/libs/ArtistSong.js b/libs/ArtistSong.js index c0785a7..09d622d 100644 --- a/libs/ArtistSong.js +++ b/libs/ArtistSong.js @@ -1,3 +1,5 @@ +const formatString = require('../helpers/strings').formatString + class ArtistSong { constructor (models) { this.models = models @@ -49,11 +51,11 @@ class ArtistSong { $or: [ { type: 'artist', - value: currentSong.artist + value: formatString(currentSong.artist) }, { type: 'title', - value: currentSong.title + value: formatString(currentSong.title) } ] }) diff --git a/libs/When.js b/libs/When.js new file mode 100644 index 0000000..cedecaf --- /dev/null +++ b/libs/When.js @@ -0,0 +1,88 @@ +const setPeriod = require('../helpers/dates').setPeriod +const formatString = require('../helpers/strings').formatString +const moment = require('moment') + +class When { + constructor (models) { + 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/titre !') + return false + } + + const period = line[2] + let value = '' + for (let i = 3; i < line.length; i += 1) { + value += ' ' + line[i] + } + value = value.replace(' ', '') + let isOk = true + const filter = { + $or: [ + { + artist: formatString(value) + }, + { + title: formatString(value) + } + ] + } + + 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 + .findOne(filter) + .sort({ + createdAt: 'desc' + }) + .limit(1) + .then(item => { + if (!item) { + botSay(where, `${value} n'a pas était joué pour cette période sur ${process.env.RADIO_ALIAS}`) + return true + } + + if (item.artist.toLowerCase() === value.toLowerCase()) { + botSay(where, `Pour cette période ${value} a été entendu pour la dernière fois le ${moment(item.createdAt).format('DD/MM/YYYY à HH:mm')} avec ${item.title}`) + } else { + botSay(where, `Pour cette période ${value} a été entendu pour la dernière fois le ${moment(item.createdAt).format('DD/MM/YYYY à HH:mm')}`) + } + }) + .catch(err => { + console.log('ERR:', err) + botSay(where, 'Impossible de te répondre pour le moment, j\'ai buggé...') + }) + + console.log('PERIOD:', period, 'SONG:', value) + console.log('FILTER:', filter) + } +} + +module.exports = When