2019-12-24 11:32:16 +01:00
|
|
|
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')
|
2019-12-24 16:30:45 +01:00
|
|
|
const ArtistSong = require('./libs/ArtistSong')
|
2019-12-25 12:47:09 +01:00
|
|
|
const When = require('./libs/When')
|
2019-12-25 13:10:13 +01:00
|
|
|
const Statistics = require('./libs/Statistics')
|
2019-12-25 13:47:42 +01:00
|
|
|
const List = require('./libs/List')
|
|
|
|
const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list']
|
2019-12-24 11:32:16 +01:00
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
// Init des librairies
|
2019-12-24 11:32:16 +01:00
|
|
|
const db = new Db(models)
|
2019-12-24 16:30:45 +01:00
|
|
|
const artistSong = new ArtistSong(models)
|
2019-12-25 12:47:09 +01:00
|
|
|
const when = new When(models)
|
2019-12-25 13:10:13 +01:00
|
|
|
const stats = new Statistics(models)
|
2019-12-25 13:47:42 +01:00
|
|
|
const list = new List(models)
|
2019-12-24 11:32:16 +01:00
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
// Stockage du morceau en cours de lecture
|
|
|
|
let currentSong = {
|
|
|
|
artist: null,
|
|
|
|
title: null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialisation du bot IRC
|
2019-12-24 11:32:16 +01:00
|
|
|
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]
|
|
|
|
})
|
2019-12-24 16:30:45 +01:00
|
|
|
// Initialisation du stream radion
|
2019-12-24 11:32:16 +01:00
|
|
|
const stream = radio.createReadStream(process.env.STREAM_URL)
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
// Gestion des erreurs du bot IRC
|
2019-12-24 11:32:16 +01:00
|
|
|
bot.addListener('error', function (message) {
|
|
|
|
console.error('ERR:', message)
|
|
|
|
})
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
/**
|
|
|
|
* Fonction permettant de publier un message sur IRC
|
|
|
|
* @param {String} where
|
|
|
|
* @param {String} what
|
|
|
|
*/
|
2019-12-24 11:32:16 +01:00
|
|
|
const botSay = (where, what) => {
|
|
|
|
bot.say(where, what)
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
/**
|
|
|
|
* Gestion des actions
|
|
|
|
* @param {String} where
|
|
|
|
* @param {String} message
|
|
|
|
* @param {String} from
|
|
|
|
*/
|
|
|
|
const actions = (where, message, from) => {
|
2019-12-24 11:32:16 +01:00
|
|
|
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':
|
2019-12-24 16:30:45 +01:00
|
|
|
artistSong.action(exploded[1].replace('!', ''), botSay, from, currentSong, exploded)
|
2019-12-24 11:32:16 +01:00
|
|
|
break
|
|
|
|
case '!help':
|
|
|
|
Help.show(botSay, where, exploded)
|
|
|
|
break
|
2019-12-25 12:47:09 +01:00
|
|
|
case '!when':
|
|
|
|
when.action(botSay, where, exploded)
|
|
|
|
break
|
2019-12-25 13:10:13 +01:00
|
|
|
case '!stats':
|
|
|
|
stats.action(botSay, where, exploded)
|
|
|
|
break
|
2019-12-25 13:47:42 +01:00
|
|
|
case '!list':
|
|
|
|
list.action(botSay, where, exploded)
|
|
|
|
break
|
2019-12-24 11:32:16 +01:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2019-12-25 13:47:42 +01:00
|
|
|
} else {
|
|
|
|
botSay(where, 'Commande inconnue, tape !help si tu es perdu')
|
2019-12-24 11:32:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listener for public message
|
|
|
|
bot.addListener(`message${process.env.IRC_CHANNEL}`, function (from, to, message) {
|
2019-12-24 16:30:45 +01:00
|
|
|
actions(process.env.IRC_CHANNEL, message.args.splice(1).join(' '), from)
|
2019-12-24 11:32:16 +01:00
|
|
|
})
|
|
|
|
// Listener for private message
|
|
|
|
bot.addListener('pm', function (from, to, message) {
|
2019-12-24 16:30:45 +01:00
|
|
|
actions(from, message.args.join(' '), from)
|
2019-12-24 11:32:16 +01:00
|
|
|
})
|
2019-12-25 09:40:18 +01:00
|
|
|
// Say hello!
|
|
|
|
if (process.env.SAY_HELLO && process.env.SAY_HELLO === 'true') {
|
|
|
|
bot.addListener('join', (channel, who) => {
|
|
|
|
// Welcome them in!
|
|
|
|
if (who !== process.env.IRC_NICKNAME) {
|
|
|
|
botSay(process.env.IRC_CHANNEL, 'Hello ' + who + '! Have a metal day! \\m/(-.-)\\m/')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-12-24 11:32:16 +01:00
|
|
|
|
|
|
|
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
|
2019-12-25 12:51:02 +01:00
|
|
|
bot.say(process.env.IRC_CHANNEL, `${process.env.RADIO_ALIAS} : ${song}`)
|
2019-12-24 11:32:16 +01:00
|
|
|
|
|
|
|
// Extract artist = title from song
|
|
|
|
const splitted = song.split(' - ')
|
|
|
|
const artist = splitted[0]
|
|
|
|
const title = splitted[1]
|
|
|
|
|
2019-12-24 16:30:45 +01:00
|
|
|
currentSong = {
|
|
|
|
title: title,
|
|
|
|
artist: artist
|
|
|
|
}
|
|
|
|
|
2019-12-24 11:32:16 +01:00
|
|
|
// Save song for history
|
|
|
|
db.saveSong({
|
|
|
|
radio: process.env.RADIO_ALIAS,
|
|
|
|
artist: artist,
|
|
|
|
title: title
|
|
|
|
})
|
2019-12-24 16:41:18 +01:00
|
|
|
|
|
|
|
// Notify some people ?
|
|
|
|
artistSong.notify(botSay, process.env.IRC_CHANNEL, currentSong)
|
2019-12-24 11:32:16 +01:00
|
|
|
})
|