diff --git a/app.js b/app.js index 7c10c56..5ae7b22 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,6 @@ const Messenger = require('./lib/messenger') const mongoose = require('mongoose') const config = require('./config') const express = require("express") -const Notifier = require('./lib/notifier') mongoose.connect(config.database).then(() => { const messenger = new Messenger() diff --git a/handlers/birthday.js b/handlers/birthday.js index 8ce9d9d..19d364b 100644 --- a/handlers/birthday.js +++ b/handlers/birthday.js @@ -24,7 +24,7 @@ class Birthday { } _getText(model) { - return `@${model.nick} cumple el dia ${model.day} de ${monthTexts[model.month -1]}` + return `${model.nick} cumple el dia ${model.day} de ${monthTexts[model.month -1]}` } _getDay (text) { diff --git a/handlers/stream.js b/handlers/stream.js index 9990da4..7b6869d 100644 --- a/handlers/stream.js +++ b/handlers/stream.js @@ -1,5 +1,6 @@ const TwitchService = require('../services/twitch') const BrowserService = require('../services/browser') +const BirthdayService = require('../services/birthday') const config = require("../config") const moment = require('moment') require('moment-precise-range-plugin') @@ -25,24 +26,21 @@ class Stream { } } - async catchStream (bot) { + async catchStream (telegramBot, twitchBot, target) { const result = await TwitchService.getStream() if (result && result.type === 'live' ) { + await this.sendTodayBirthday(twitchBot, target) const text = this._getText(result) - - const options = { - parse_mode: 'Markdown' - } - - const msg = await bot.sendMessage(config.telegram.chatId, text, options) - await bot.pinChatMessage(config.telegram.chatId, msg.message_id).catch(() => {}) + const options = { parse_mode: 'Markdown' } + const msg = await telegramBot.sendMessage(config.telegram.chatId, text, options) + await telegramBot.pinChatMessage(config.telegram.chatId, msg.message_id).catch((err) => { console.error(`cannot pin chat: ${err}`)}) await TwitchService.saveLastMessage(msg) await TwitchService.saveTitle(result.title) - await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on live')}) + await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on live')}) } else if (result && result.type === 'finished' && result.messageId) { - await bot.deleteMessage(config.telegram.chatId, result.messageId) + await telegramBot.deleteMessage(config.telegram.chatId, result.messageId) await TwitchService.deleteLastMessage() await TwitchService.saveTitle(result.title) await BrowserService.closeBrowser().catch(() => { console.error('closeBrowser on finished')}) @@ -52,7 +50,7 @@ class Stream { message_id: result.messageId, parse_mode: 'Markdown' } - await bot.editMessageText(this._getText(result), options).catch(() => {}) + await telegramBot.editMessageText(this._getText(result), options).catch(() => {}) await TwitchService.saveTitle(result.title) await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on stillLive')}) } else if (result && result.type === 'notLive') { @@ -74,6 +72,28 @@ class Stream { const title = `🔴 *¡EN DIRECTO!*` return `${image} ${title} ${link} \n _${stream.title}_ (${duration})` } + + async sendTodayBirthday(twitchBot, target) { + const bdays = await BirthdayService.getTodayBirthdays() + if (bdays && bdays.length > 0) { + let text + if (bdays.length === 1){ + text = this._getTodayBdayText(`${bdays[0].nick}`) + } else if (bdays.length > 1) { + const nicks = bdays.map(bday => `${bday.nick}`).join(', ').replace(/, ([^,]*)$/, ' y $1') + text = this._getTodayBdaysText(nicks) + } + twitchBot.say(target, text) + } + } + + _getTodayBdaysText(nicks) { + return `¡${nicks} cumplen años hoy!` + } + + _getTodayBdayText(nick) { + return `¡${nick} cumple años hoy!` + } } module.exports = Stream diff --git a/helpers/dbmanager.js b/helpers/dbmanager.js index 6891587..38eb7d0 100644 --- a/helpers/dbmanager.js +++ b/helpers/dbmanager.js @@ -2,6 +2,8 @@ const Token = require('../models/token') const Muncipio = require('../models/municipio') const Channel = require('../models/channel') const Birthday = require('../models/birthday') +const moment = require('moment') + function getToken (userId) { return Token.findOne({userId: userId}) @@ -46,6 +48,10 @@ async function updateBirthday (nick, update) { }).lean() } +async function getBirthdayFromDate(day, month) { + return Birthday.find({day: day, month: month}).lean() +} + module.exports = { getToken, updateToken, @@ -56,5 +62,6 @@ module.exports = { getChannel, updateChannel, updateBirthday, - getBirthday + getBirthday, + getBirthdayFromDate } diff --git a/lib/messenger.js b/lib/messenger.js index 304b719..ba458b4 100644 --- a/lib/messenger.js +++ b/lib/messenger.js @@ -5,8 +5,8 @@ const InputParser = require('./inputParser') const inputParser = new InputParser() const tmi = require('@twurple/auth-tmi') const { RefreshingAuthProvider } = require('@twurple/auth') -const TokenService = require("../services/token"); -const Notifier = require("./notifier"); +const TokenService = require("../services/token") +const Notifier = require("./notifier") class Messenger { constructor () {} @@ -21,7 +21,7 @@ class Messenger { } this.bot = new tmi.client(opts) - this.notifier = new Notifier() + this.notifier = new Notifier(this.bot) await this.notifier.notify() return this.listen() @@ -50,6 +50,8 @@ class Messenger { this.bot.on('hosting', this.handleHosting.bind(this)); return this.bot.connect().catch(console.error) + + } handleText (target, context, msg, self) { @@ -101,7 +103,7 @@ class Messenger { } handleConnect (addr, port) { - console.log(`* Connected to ${addr}:${port}`); + console.log(`* Connected to ${addr}:${port}`) } } diff --git a/lib/notifier.js b/lib/notifier.js index ca36edc..6c18dab 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -4,15 +4,22 @@ const cron = require('node-cron') const handlers = require('../handlers') class Notifier { - constructor () { + constructor (twitchBot) { + this.target = `#${config.twitch.channels}` + this.twitchBot = twitchBot this.bot = new TelegramBot(config.telegram.apiKey) } notify () { cron.schedule('*/1 * * * *', () => { - handlers.stream.catchStream(this.bot) + handlers.stream.catchStream(this.bot, this.twitchBot, this.target) }) + cron.schedule('0 * * * *', () => { + handlers.stream.sendTodayBirthday(this.twitchBot, this.target) + }) + + return Promise.resolve() } diff --git a/services/birthday.js b/services/birthday.js index bc725d3..a952883 100644 --- a/services/birthday.js +++ b/services/birthday.js @@ -1,4 +1,5 @@ const dbManager = require('../helpers/dbmanager') +const moment = require('moment') async function getBirthday(nick) { let result = null @@ -10,8 +11,16 @@ async function addBirthday(nick, day, month) { return await dbManager.updateBirthday(nick, {day: day, month: month}) } +async function getTodayBirthdays() { + let result = null + const today = moment() + result = await dbManager.getBirthdayFromDate(today.date(), today.month() + 1) + return result +} + module.exports = { getBirthday, - addBirthday + addBirthday, + getTodayBirthdays }