@seaavey/bot
Version:
The Library for Seaavey Bot
73 lines (72 loc) • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerEvents = registerEvents;
const boom_1 = require("@hapi/boom");
const baileys_1 = require("@seaavey/baileys");
const Serialize_1 = __importDefault(require("./Serialize"));
const Utils_1 = require("../Utils");
function registerEvents(sock, store, emit, config, attemptRef) {
sock.ev.on('connection.update', async ({ connection, lastDisconnect }) => {
var _a, _b;
const reason = (_b = (_a = new boom_1.Boom(lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error)) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.statusCode;
emit('Reason', reason);
if (connection === 'open') {
emit('Authenticated');
}
else if (connection === 'close') {
if (reason === baileys_1.DisconnectReason.loggedOut || reason === baileys_1.DisconnectReason.badSession) {
console.error(`Logged out or bad session.`);
return;
}
if (attemptRef.value < 5) {
console.warn(`Reconnect attempt ${attemptRef.value} - ${reason}. Retrying...`);
await (0, Utils_1.delay)(3000);
attemptRef.value++;
emit('Reconnect');
}
else {
console.error(`Max retry reached.`);
process.exit(1);
}
}
});
sock.ev.on('contacts.update', (updates) => {
updates.forEach((contact) => {
const id = (0, baileys_1.jidNormalizedUser)(contact.id);
store.contacts[id] = {
id,
name: contact.name || contact.notify,
};
});
});
sock.ev.on('contacts.upsert', (contacts) => {
contacts.forEach((contact) => {
const id = (0, baileys_1.jidNormalizedUser)(contact.id);
store.contacts[id] = {
id,
name: contact.name || contact.notify,
};
});
});
sock.ev.on('group-participants.update', (update) => {
emit('group:action', update);
});
sock.ev.on('messages.upsert', async ({ messages }) => {
var _a;
if (!((_a = messages[0]) === null || _a === void 0 ? void 0 : _a.message))
return;
emit('messages.upsert', {
messages,
});
try {
const m = (await (0, Serialize_1.default)(sock, messages[0], store, config));
emit('message', m);
}
catch (e) {
console.error('Serialize error:', e);
}
});
}