Added !when command
This commit is contained in:
parent
6947fb843e
commit
4151ab95f8
6 changed files with 159 additions and 6 deletions
8
TODO.md
8
TODO.md
|
@ -2,9 +2,9 @@
|
|||
|
||||
## Les commandes
|
||||
|
||||
* !artist <add/del> {nom de l'artiste}
|
||||
* !song <add/del> {nom du morceau}
|
||||
* ~~!artist <add/del> {nom de l'artiste}~~
|
||||
* ~~!song <add/del> {nom du morceau}~~
|
||||
* !stats <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>
|
||||
* !when <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>
|
||||
* !help {artist/song/stats/when}
|
||||
* ~~!when <day/week/lastweek/month/lastmonth/year/lastyear> <artist/song>~~
|
||||
* ~~!help {artist/song/stats/when}~~
|
||||
* !notifications <on/off/state>
|
50
helpers/dates.js
Normal file
50
helpers/dates.js
Normal file
|
@ -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 {}
|
||||
}
|
||||
}
|
||||
}
|
8
helpers/strings.js
Normal file
8
helpers/strings.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
formatString: function (string) {
|
||||
if (string !== undefined && string !== null) {
|
||||
return new RegExp('^' + string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$', 'i')
|
||||
}
|
||||
return ';'
|
||||
}
|
||||
}
|
5
index.js
5
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
]
|
||||
})
|
||||
|
|
88
libs/When.js
Normal file
88
libs/When.js
Normal file
|
@ -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
|
Loading…
Reference in a new issue