diff --git a/index.js b/index.js index 69836ed..08bdba8 100644 --- a/index.js +++ b/index.js @@ -16,10 +16,9 @@ const { client, xml } = require('@xmpp/client'), nick = config.nick, admins = config.admins, commands = config.commands, + secondsInterval = 30, events = [] -let eventInit = false - xmpp.on('error', err => { console.error(err) }) @@ -33,8 +32,28 @@ xmpp.on('stanza', async stanza => { const user = stanza.attrs.from.replace(/.*\//, '') if (admins.some(admin => user === admin)) { const message = stanza.getChildText('body') - if (eventInit) { - + if (commands.some(command => message.match(new RegExp('^' + command)))) { + if (message.match(/^.nuevoevento/) && message.split(' ').length > 3 && + message.split(' ')[1].match(/^[0-9]{1,2}[\/]{1}[0-9]{1,2}[\/]{1}[0-9]{4}$/g) && + message.split(' ')[2].match(/^[0-9]{1,2}[:]{1}[0-9]{1,2}$/g)) { + const msg = message.split(' ') + msg.shift() + const fecha = msg.shift(), + hora = msg.shift(), + date = new Date(fecha.split('/')[2] + '-' + fecha.split('/')[1] + '-' + fecha.split('/')[0] + 'T' + hora) + if (date.getTime() > Date.now()) { + events.push({ + message: msg.join(' '), + fecha: date, + reported: false + }) + sendMessage('Nuevo evento añadido el ' + fecha + ' a las ' + hora + ' horas: ' + msg.join(' ')) + } else { + sendMessage('Lo siento pero el evento es anterior a la fecha y hora actual, intentelo de nuevo...') + } + } else { + sendMessage('Comando incorrecto, inténtelo otra vez.') + } } else if (message === '.reiniciarbot') { sendMessage('Reiniciando bot por ' + user + '...') await xmpp.stop() @@ -56,4 +75,13 @@ xmpp.on('online', async address => { console.log('Connected like: ' + address) }) -xmpp.start().catch(console.error) \ No newline at end of file +xmpp.start().catch(console.error) + +setInterval(() => { + events.map(event => { + if (!event.reported && event.fecha.getTime() < Date.now()) { + sendMessage('Atención el evento: ' + event.message + ' comienza Ya!!') + event.reported = true + } + }) +}, secondsInterval * 1000) \ No newline at end of file