From 147812a3e24c505d7548beaec43e8baa1414d586 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Tue, 24 Dec 2019 11:32:16 +0100 Subject: [PATCH] Added index file --- index.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..c68b21f --- /dev/null +++ b/index.js @@ -0,0 +1,84 @@ +const radio = require('radio-stream') +const irc = require('irc-upd') +const Db = require('./libs/Db') +const models = require('./models').models +const Help = require('./libs/Help') +const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications'] + +const db = new Db(models) + +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] +}) +const stream = radio.createReadStream(process.env.STREAM_URL) + +// Error events +bot.addListener('error', function (message) { + console.error('ERR:', message) +}) + +const botSay = (where, what) => { + bot.say(where, what) +} + +const actions = (where, message) => { + const exploded = message.split(' ') + + // Le message publiƩ est pour le bot + if (exploded[0].indexOf(process.env.IRC_NICKNAME) === 0) { + // Le message contient une commande + if (allowedCommands.indexOf(exploded[1]) !== -1) { + switch (exploded[1]) { + case '!artist': + case '!song': + break + case '!help': + Help.show(botSay, where, exploded) + break + default: + break + } + } + } +} + +// 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(' ')) +}) +// Listener for private message +bot.addListener('pm', function (from, to, message) { + actions(from, message.args.join(' ')) +}) + +stream.on('connect', function () { + console.info('Radio Stream connected!') + console.info(stream.headers) +}) + +// Listener on new song +stream.on('metadata', function (data) { + let song = data.replace(/[^A-Za-z 0-9 .,?""!@#$%^&*()-_=+;:<>/\\|}{[\]`~]*/g, '') + song = song.substring(12, song.length - 1) + + // Just for debug + console.info(`Now playing: ${song}`) + + // Publish song on IRC channel + bot.say(process.env.IRC_CHANNEL, `${process.env.RADIO_ALIAS} - ${song}`) + + // Extract artist = title from song + const splitted = song.split(' - ') + const artist = splitted[0] + const title = splitted[1] + + // Save song for history + db.saveSong({ + radio: process.env.RADIO_ALIAS, + artist: artist, + title: title + }) +})