Added !notifications

This commit is contained in:
dbroqua 2020-01-02 13:58:41 +01:00
parent e260ebc135
commit c093e4bf64
4 changed files with 128 additions and 13 deletions

View file

@ -7,6 +7,7 @@ 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 List = require('./libs/List') const List = require('./libs/List')
const Preferences = require('./libs/Preferences')
const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list'] const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list']
// Init des librairies // Init des librairies
@ -14,6 +15,7 @@ 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) const list = new List(models)
const preferences = new Preferences(models)
// Stockage du morceau en cours de lecture // Stockage du morceau en cours de lecture
let currentSong = { let currentSong = {
@ -167,6 +169,9 @@ const actions = (where, message, from) => {
case '!list': case '!list':
list.action(botSay, where, exploded) list.action(botSay, where, exploded)
break break
case '!notifications':
preferences.notifications(botSay, where, from, exploded)
break
default: default:
break break
} }

View file

@ -93,13 +93,30 @@ class ArtistSong {
const artist = [] const artist = []
const song = [] const song = []
// On get la liste des utilisateurs ne souhaitant pas être notifié pour cette radio
this.models.Preferences
.find({
radio: process.env.RADIO_ALIAS,
notification: false
})
.then(prefs => {
for (let i = 0; i < list.length; i += 1) { for (let i = 0; i < list.length; i += 1) {
let isMute = false
for (let j = 0; j < prefs.length; j += 1) {
if (prefs[j].user === list[i].user) {
isMute = true
break
}
}
if (!isMute) {
if (list[i].type === 'artist') { if (list[i].type === 'artist') {
artist.push(list[i].user) artist.push(list[i].user)
} else { } else {
song.push(list[i].user) song.push(list[i].user)
} }
} }
}
if (artist.length > 0) { if (artist.length > 0) {
botSay(where, `${process.env.RADIO_ALIAS} : Hey ${artist.toString().replace(/,/g, ', ')} ! Y'a ${currentSong.artist} ! Monte${artist.length > 1 ? 'z' : ''} le son !`) botSay(where, `${process.env.RADIO_ALIAS} : Hey ${artist.toString().replace(/,/g, ', ')} ! Y'a ${currentSong.artist} ! Monte${artist.length > 1 ? 'z' : ''} le son !`)
@ -108,6 +125,7 @@ class ArtistSong {
botSay(where, `${process.env.RADIO_ALIAS} : Hey ${song.toString().replace(/,/g, ', ')} ! Y'a ${song.title} ! Monte${song.length > 1 ? 'z' : ''} le son !`) botSay(where, `${process.env.RADIO_ALIAS} : Hey ${song.toString().replace(/,/g, ', ')} ! Y'a ${song.title} ! Monte${song.length > 1 ? 'z' : ''} le son !`)
} }
}) })
})
.catch(err => { .catch(err => {
console.log('ERR:', err) console.log('ERR:', err)
}) })

81
libs/Preferences.js Normal file
View file

@ -0,0 +1,81 @@
class Preferences {
constructor (models) {
this.models = models
}
notifications (botSay, where, from, exploded) {
if (exploded.length < 3) {
botSay(where, 'Attention ! tu as oublié de dire ce que tu voulais faire !')
return false
}
const notification = (exploded[2] === 'on')
const radio = process.env.RADIO_ALIAS
const errorCatcher = (err) => {
botSay(from, 'Oups... j\'ai pas réussi à faire ce que tu voulais :(')
console.log('ERR:', err)
}
const successCatcher = () => {
if (notification) {
botSay(from, 'C\'est parti pour de la notification à gogo ! Yeah !')
} else {
botSay(from, 'Tu ne m\'aime plus ? c\'est ça ? :\'(')
}
}
switch (exploded[2]) {
case 'on':
case 'off':
this.models.Preferences
.findOne({
user: from,
radio: radio
})
.then((item) => {
if (!item) {
const newItem = new this.models.Preferences({
user: from,
radio: radio,
notification: notification
})
newItem.save()
.then(successCatcher)
.catch(errorCatcher)
} else {
item.update({
notification: notification
})
.then(successCatcher)
.catch(errorCatcher)
}
})
.catch(errorCatcher)
break
case 'state':
this.models.Preferences
.findOne({
user: from,
radio: radio
})
.then((item) => {
if (!item) {
botSay(from, 'Bein... vu que t\'as fait ni de on, ni de off je suis.... On ! 8)')
} else {
if (item.notification) {
botSay(from, 'Toi et moi c\'est pour la vie ! tu n\'en louperas pas une miette !')
} else {
botSay(from, 'Je boude ! Je ne te dirai rien !')
}
}
})
.catch(errorCatcher)
break
default:
botSay(where, `T'es fort toi! t'as le choix entre on/off/state et tu m'envoies ${exploded[2]}...`)
}
}
}
module.exports = Preferences

11
models/Preferences.js Normal file
View file

@ -0,0 +1,11 @@
module.exports = mongoose => {
const schema = new mongoose.Schema({
user: String,
notification: Boolean,
radio: String
})
const Preferences = mongoose.model('Preferences', schema)
return Preferences
}