|
|
@ -0,0 +1,59 @@ |
|
|
|
const { client, xml } = require('@xmpp/client'), |
|
|
|
config = require('./config'), |
|
|
|
xmpp = client({ |
|
|
|
service: config.service, |
|
|
|
username: config.username, |
|
|
|
password: config.password |
|
|
|
}), |
|
|
|
sendMessage = async message => { |
|
|
|
await xmpp.send(xml( |
|
|
|
'message', |
|
|
|
{ type: 'groupchat', to: room }, |
|
|
|
xml('body', {}, message) |
|
|
|
)) |
|
|
|
}, |
|
|
|
room = config.room, |
|
|
|
nick = config.nick, |
|
|
|
admins = config.admins, |
|
|
|
commands = config.commands, |
|
|
|
events = [] |
|
|
|
|
|
|
|
let eventInit = false |
|
|
|
|
|
|
|
xmpp.on('error', err => { |
|
|
|
console.error(err) |
|
|
|
}) |
|
|
|
|
|
|
|
xmpp.on('offline', () => { |
|
|
|
console.log('Go to offline') |
|
|
|
}) |
|
|
|
|
|
|
|
xmpp.on('stanza', async stanza => { |
|
|
|
if (stanza.is('message') && !stanza.getChild('delay')) { |
|
|
|
const user = stanza.attrs.from.replace(/.*\//, '') |
|
|
|
if (admins.some(admin => user === admin)) { |
|
|
|
const message = stanza.getChildText('body') |
|
|
|
if (eventInit) { |
|
|
|
|
|
|
|
} else if (message === '.reiniciarbot') { |
|
|
|
sendMessage('Reiniciando bot por ' + user + '...') |
|
|
|
await xmpp.stop() |
|
|
|
} else if (message === '.help') { |
|
|
|
sendMessage('\nAyuda ' + nick + ': \n' + commands.map((c, i) => (i + 1) + ': ' + c).join('\n')) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
xmpp.on('online', async address => { |
|
|
|
await xmpp.send(xml( |
|
|
|
'presence', |
|
|
|
{ to: room + '/' + nick }, |
|
|
|
xml('x', |
|
|
|
{ xmlns: 'http://jabber.org/protocol/muc' } |
|
|
|
) |
|
|
|
)) |
|
|
|
console.log('Connected like: ' + address) |
|
|
|
}) |
|
|
|
|
|
|
|
xmpp.start().catch(console.error) |