From 68f821b933f2993f25a6cd59511e885fea26fe56 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Tue, 24 Dec 2019 16:30:45 +0100 Subject: [PATCH] Added Artist/Song library --- index.js | 36 ++++++++++++++-- libs/ArtistSong.js | 96 +++++++++++++++++++++++++++++++++++++++++ libs/Db.js | 4 ++ models/Notifications.js | 2 +- models/index.js | 2 +- 5 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 libs/ArtistSong.js diff --git a/index.js b/index.js index c68b21f..b6ee398 100644 --- a/index.js +++ b/index.js @@ -3,28 +3,50 @@ const irc = require('irc-upd') const Db = require('./libs/Db') const models = require('./models').models const Help = require('./libs/Help') +const ArtistSong = require('./libs/ArtistSong') const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications'] +// Init des librairies const db = new Db(models) +const artistSong = new ArtistSong(models) +// Stockage du morceau en cours de lecture +let currentSong = { + artist: null, + title: null +} + +// Initialisation du bot IRC const bot = new irc.Client(process.env.IRC_SERVER, process.env.IRC_NICKNAME, { userName: process.env.IRC_USER_NAME, realName: process.env.IRC_REAL_NAME, password: process.env.IRC_PASSWORD, channels: [process.env.IRC_CHANNEL] }) +// Initialisation du stream radion const stream = radio.createReadStream(process.env.STREAM_URL) -// Error events +// Gestion des erreurs du bot IRC bot.addListener('error', function (message) { console.error('ERR:', message) }) +/** + * Fonction permettant de publier un message sur IRC + * @param {String} where + * @param {String} what + */ const botSay = (where, what) => { bot.say(where, what) } -const actions = (where, message) => { +/** + * Gestion des actions + * @param {String} where + * @param {String} message + * @param {String} from + */ +const actions = (where, message, from) => { const exploded = message.split(' ') // Le message publié est pour le bot @@ -34,6 +56,7 @@ const actions = (where, message) => { switch (exploded[1]) { case '!artist': case '!song': + artistSong.action(exploded[1].replace('!', ''), botSay, from, currentSong, exploded) break case '!help': Help.show(botSay, where, exploded) @@ -47,11 +70,11 @@ const actions = (where, message) => { // Listener for public message bot.addListener(`message${process.env.IRC_CHANNEL}`, function (from, to, message) { - actions(process.env.IRC_CHANNEL, message.args.splice(1).join(' ')) + actions(process.env.IRC_CHANNEL, message.args.splice(1).join(' '), from) }) // Listener for private message bot.addListener('pm', function (from, to, message) { - actions(from, message.args.join(' ')) + actions(from, message.args.join(' '), from) }) stream.on('connect', function () { @@ -75,6 +98,11 @@ stream.on('metadata', function (data) { const artist = splitted[0] const title = splitted[1] + currentSong = { + title: title, + artist: artist + } + // Save song for history db.saveSong({ radio: process.env.RADIO_ALIAS, diff --git a/libs/ArtistSong.js b/libs/ArtistSong.js new file mode 100644 index 0000000..f4a0952 --- /dev/null +++ b/libs/ArtistSong.js @@ -0,0 +1,96 @@ +class ArtistSong { + constructor (models) { + this.models = models + } + + /** + * Point d'entrée pour l'ajout suppression de favoris + * @param {String} type + * @param {Function} botSay + * @param {String} from + * @param {Object} currentSong + * @param {Array} line + */ + action (type, botSay, from, currentSong, line) { + let value = currentSong[type] + + if (line.length > 3) { + value = '' + for (let i = 3; i < line.length; i += 1) { + value += ' ' + line[i] + } + value = value.replace(' ', '') + } + + const item = { + user: from, + type: type, + value: value + } + + switch (line[2]) { + case 'add': + this.add(botSay, from, item) + break + case 'del': + this.delete(botSay, from, item) + } + } + + /** + * Fonction permettant d'ajouter un favoris + * @param {Function} botSay + * @param {String} from + * @param {Object} item + */ + add (botSay, from, item) { + this.models.Notifications + .findOne(item) + .then(notification => { + if (!notification) { + const newItem = new this.models.Notifications(item) + newItem.save((err, res) => { + if (err) { + console.log('ERR:', err) + } else { + botSay(from, `${item.value} correctement ajouté dans vos favoris`) + } + }) + } else { + botSay(from, `${item.value} est déjà dans tes favoris, c'est moche de vieillir...`) + } + }) + .catch(err => { + console.log('ERR:', err) + }) + } + + /** + * Fonction permettant de supprimer un favoris + * @param {Function} botSay + * @param {String} from + * @param {Object} item + */ + delete (botSay, from, item) { + this.models.Notifications + .findOne(item) + .then(notification => { + if (notification) { + notification.remove((err, res) => { + if (err) { + console.log('ERR:', err) + } else { + botSay(from, `${item.value} correctement retiré dans vos favoris`) + } + }) + } else { + botSay(from, `${item.value} n'est dpas dans tes favoris, c'est moche de vieillir...`) + } + }) + .catch(err => { + console.log('ERR:', err) + }) + } +} + +module.exports = ArtistSong diff --git a/libs/Db.js b/libs/Db.js index b4da2f1..eef6789 100644 --- a/libs/Db.js +++ b/libs/Db.js @@ -4,6 +4,10 @@ class Db { this.models = models } + /** + * Fonction sauvegardant en historique le morceau en cours de lecture + * @param {Object} value + */ saveSong (value) { // Find if previous song was the same (on bot reload) this.models.Histories diff --git a/models/Notifications.js b/models/Notifications.js index bb20680..c7a12a3 100644 --- a/models/Notifications.js +++ b/models/Notifications.js @@ -1,7 +1,7 @@ module.exports = mongoose => { const schema = new mongoose.Schema({ user: String, - property: { + type: { type: String, enum: ['artist', 'title'] }, diff --git a/models/index.js b/models/index.js index 10ec97d..b59cba8 100644 --- a/models/index.js +++ b/models/index.js @@ -8,7 +8,7 @@ mongoose.set('useNewUrlParser', true) mongoose.set('useUnifiedTopology', true) mongoose.set('useFindAndModify', false) mongoose.set('useCreateIndex', true) -mongoose.set('debug', false) +mongoose.set('debug', true) mongoose.connect(process.env.MONGO_URL)