Skip to content

Commit

Permalink
Add birthday feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 13, 2024
1 parent c7650a4 commit 182e9d1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
39 changes: 39 additions & 0 deletions handlers/birthday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const BirthdayService = require('../services/birthday')

const monthTexts = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Setiembre","Octubre","Noviembre","Diciembre"]

class Birthday {
async addBirthday (target, text, bot, name) {
const day = this._getDay(text)
const month = this._getMonth(text)
const nick = name.toLowerCase()

const result = await BirthdayService.addBirthday(nick, day, month)
if (result) {
bot.say(target, this._getText(result))
}
}

async getBirthday (target, text, bot) {
const nick = text.trim().replace('@', '').toLowerCase()
const result = await BirthdayService.getBirthday(nick)

if (result) {
bot.say(target, this._getText(result))
}
}

_getText(model) {
return `@${model.nick} cumple el dia ${model.day} de ${monthTexts[model.month -1]}`
}

_getDay (text) {
return parseInt(text.split('-')[0].trim())
}

_getMonth (text) {
return parseInt(text.split('-')[1].trim())
}
}

module.exports = Birthday
4 changes: 3 additions & 1 deletion handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const Generic = require('./generic')
const Weather = require('./Weather')
const Train = require('./Train')
const Stream = require('./Stream')
const Birthday = require('./Birthday')

module.exports = {
generic: new Generic(),
weather: new Weather(),
train: new Train(),
stream: new Stream()
stream: new Stream(),
birthday: new Birthday()
}
16 changes: 15 additions & 1 deletion helpers/dbmanager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Token = require('../models/token')
const Muncipio = require('../models/municipio')
const Channel = require('../models/channel')
const Birthday = require('../models/birthday')

function getToken (userId) {
return Token.findOne({userId: userId})
Expand Down Expand Up @@ -34,6 +35,17 @@ async function getMunicipio (name) {
return Muncipio.findOne({nombre: { "$regex": "^" + name + "$", "$options": "i"}}).lean()
}

async function getBirthday (nick) {
return Birthday.findOne({nick: nick}).lean()
}

async function updateBirthday (nick, update) {
return Birthday.findOneAndUpdate({nick: nick}, update, {
new: true,
upsert: true
}).lean()
}

module.exports = {
getToken,
updateToken,
Expand All @@ -42,5 +54,7 @@ module.exports = {
getMuncipioEndsWith,
getMuncipioContains,
getChannel,
updateChannel
updateChannel,
updateBirthday,
getBirthday
}
4 changes: 4 additions & 0 deletions lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class InputParser {
isAskingForF5 (text) {
return text.toLowerCase().startsWith('!f5')
}

isAskingForBirthday (text) {
return text.toLowerCase().startsWith('!cumple')
}
}

module.exports = InputParser
8 changes: 8 additions & 0 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ class Messenger {

if (textSplit.length > 0 && inputParser.isAskingForF5(textSplit[0]) && (context['mod'] || context['vip']))
return handlers.stream.refreshPage(target, this.bot, this.notifier.bot)

if (textSplit.length === 2 && inputParser.isAskingForBirthday(textSplit[0]) && textSplit[1].includes('-')) {
return handlers.birthday.addBirthday(target, textSplit.slice(1).join(' '), this.bot, context['display-name'])
}

if (textSplit.length === 2 && inputParser.isAskingForBirthday(textSplit[0]) && !textSplit[1].includes('-')) {
return handlers.birthday.getBirthday(target, textSplit[1], this.bot)
}
}

handleHosting (channel, target, viewers) {
Expand Down
20 changes: 20 additions & 0 deletions models/birthday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema

/* Channel Schema */
const BirthdaySchema = new Schema({
nick: {
type: String,
required: true
},
day: {
type: Number,
required: true
},
month: {
type: Number,
required: true
}
})

module.exports = mongoose.model('birthday', BirthdaySchema, 'birthdays')
17 changes: 17 additions & 0 deletions services/birthday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const dbManager = require('../helpers/dbmanager')

async function getBirthday(nick) {
let result = null
result = await dbManager.getBirthday(nick)
return result
}

async function addBirthday(nick, day, month) {
return await dbManager.updateBirthday(nick, {day: day, month: month})
}


module.exports = {
getBirthday,
addBirthday
}

0 comments on commit 182e9d1

Please sign in to comment.