xmppjs-chat-bot
Version:
Server-side XMPP chat bot
54 lines • 2.18 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlerHello = void 0;
const abstract_1 = require("./abstract");
const reference_1 = require("../reference");
const handlers_directory_1 = require("../handlers_directory");
class HandlerHello extends abstract_1.Handler {
constructor(id, room, options) {
super(id, room, options);
this.lastHellos = new Map();
this.txt ?? (this.txt = 'Hello {{NICK}}!');
this.roomJoined = (user) => {
if (user.isMe()) {
return;
}
if (this.delay !== undefined) {
const jid = user.jid.toString();
const now = new Date();
const lastHello = this.lastHellos.get(jid);
if (lastHello) {
this.logger.debug(`We already helloed the user ${jid}, checking if delay ${this.delay} is over.`);
if ((now.getTime() - lastHello.getTime()) < this.delay * 1000) {
this.logger.debug('Last hello was too recent.');
this.lastHellos.set(jid, now);
return;
}
}
this.lastHellos.set(jid, now);
}
const mention = reference_1.ReferenceMention.mention(this.txt, user.jid, '{{NICK}}');
this.room.sendGroupchat(mention.txt, mention.references).catch((err) => { this.logger.error(err); });
};
}
loadOptions(options) {
if (typeof options !== 'object') {
return;
}
if (('txt' in options) && (typeof options.txt === 'string')) {
this.txt = options.txt;
}
if (('delay' in options) && (options.delay === undefined || (typeof options.delay === 'number'))) {
this.delay = options.delay;
}
}
start() {
this.room.on('room_joined', this.roomJoined);
}
stop() {
this.room.off('room_joined', this.roomJoined);
}
}
exports.HandlerHello = HandlerHello;
handlers_directory_1.HandlersDirectory.singleton().register('hello', HandlerHello);
//# sourceMappingURL=hello.js.map