UNPKG

@seaavey/bot

Version:

The Library for Seaavey Bot

160 lines (159 loc) 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseMessage = exports.getContentType = exports.escapeRegExp = void 0; exports.default = Serialize; const baileys_1 = require("@seaavey/baileys"); const Utils_1 = require("../Utils"); async function Serialize(sock, msg, store, options) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8; if (!(msg === null || msg === void 0 ? void 0 : msg.message)) return; const m = { key: msg.key, message: {}, from: '', fromMe: false, id: '', device: 'android', isGroup: false, participant: '', sender: '', pushName: (_a = msg.pushName) !== null && _a !== void 0 ? _a : 'Unknown', isOwner: false, type: '', msg: {}, mentions: [], body: '', prefix: '', command: '', args: [], text: '', isQuoted: false, reply: async () => Promise.reject('reply not initialized'), react: async () => Promise.reject('react not initialized'), }; m.message = (0, exports.parseMessage)(msg.message); m.from = ((_b = msg.key.remoteJid) === null || _b === void 0 ? void 0 : _b.startsWith('status')) ? (0, baileys_1.jidNormalizedUser)(msg.participant || '') : (0, baileys_1.jidNormalizedUser)(msg.key.remoteJid || ''); m.fromMe = !!msg.key.fromMe; m.id = msg.key.id || ''; m.device = /^3A/.test(m.id) ? 'ios' : m.id.startsWith('3EB') ? 'web' : /^.{21}/.test(m.id) ? 'android' : /^.{18}/.test(m.id) ? 'desktop' : 'unknown'; m.isGroup = m.from.endsWith('@g.us'); m.participant = (0, baileys_1.jidNormalizedUser)(msg.participant || msg.key.participant || ''); m.sender = (0, baileys_1.jidNormalizedUser)(m.fromMe ? ((_c = sock.user) === null || _c === void 0 ? void 0 : _c.id) || '' : m.isGroup ? m.participant : m.from); if (m.isGroup) { if (!(m.from in store.groupMetadata)) { store.groupMetadata[m.from] = await sock.groupMetadata(m.from); } m.metadata = store.groupMetadata[m.from]; m.groupAdmins = m.metadata.participants.reduce((admins, p) => { if (p.admin) admins.push({ id: p.id, admin: p.admin }); return admins; }, []); m.isAdmin = (_d = m.groupAdmins) === null || _d === void 0 ? void 0 : _d.some((a) => a.id === m.sender); m.isBotAdmin = (_e = m.groupAdmins) === null || _e === void 0 ? void 0 : _e.some((a) => { var _a; return a.id === (0, baileys_1.jidNormalizedUser)(((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id) || ''); }); } m.isOwner = options.Owners.map((v) => v.replace(/\D/g, '') + '@s.whatsapp.net').includes(m.sender); if (m.message) { m.type = (0, exports.getContentType)(m.message) || Object.keys(m.message)[0]; m.msg = (0, exports.parseMessage)(m.message[m.type]) || m.message[m.type]; m.mentions = [...(((_g = (_f = m.msg) === null || _f === void 0 ? void 0 : _f.contextInfo) === null || _g === void 0 ? void 0 : _g.mentionedJid) || []), ...(((_j = (_h = m.msg) === null || _h === void 0 ? void 0 : _h.contextInfo) === null || _j === void 0 ? void 0 : _j.groupMentions) || []).map((v) => v.groupJid).filter((id) => typeof id === 'string')]; m.body = ((_k = m.msg) === null || _k === void 0 ? void 0 : _k.caption) || ((_l = m.message) === null || _l === void 0 ? void 0 : _l.conversation); m.prefix = /^[°•π÷×¶∆£¢€¥®™+✓=|/~!?../..#%^&.©^]/gi.test(m.body) ? ((_m = m.body.match(/^[°•π÷×¶∆£¢€¥®™+✓=|/~!?../..#%^&.©^]/gi)) === null || _m === void 0 ? void 0 : _m[0]) || '' : ''; m.command = ((_p = (_o = m.body) === null || _o === void 0 ? void 0 : _o.trim().replace(m.prefix, '').split(/ +/).shift()) === null || _p === void 0 ? void 0 : _p.toLowerCase()) || ''; m.args = ((_q = m.body) === null || _q === void 0 ? void 0 : _q.trim().replace(new RegExp('^' + (0, exports.escapeRegExp)(m.prefix), 'i'), '').replace(m.command, '').split(/ +/).filter(Boolean)) || []; m.text = m.args.join(' ').trim(); m.expiration = ((_s = (_r = m.msg) === null || _r === void 0 ? void 0 : _r.contextInfo) === null || _s === void 0 ? void 0 : _s.expiration) || 0; } if ((_u = (_t = m.msg) === null || _t === void 0 ? void 0 : _t.contextInfo) === null || _u === void 0 ? void 0 : _u.quotedMessage) { m.isQuoted = true; const quotedMsg = (0, exports.parseMessage)(m.msg.contextInfo.quotedMessage); const stanzaId = m.msg.contextInfo.stanzaId || ''; const participant = (0, baileys_1.jidNormalizedUser)(m.msg.contextInfo.participant || ''); m.quoted = { message: quotedMsg, type: (0, exports.getContentType)(quotedMsg) || Object.keys(quotedMsg)[0], msg: (0, exports.parseMessage)(quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg[(0, exports.getContentType)(quotedMsg) || '']) || {}, key: { remoteJid: m.msg.contextInfo.remoteJid || m.from, participant, fromMe: (0, baileys_1.areJidsSameUser)(participant, (0, baileys_1.jidNormalizedUser)(((_v = sock.user) === null || _v === void 0 ? void 0 : _v.id) || '')), id: stanzaId, }, from: /g\.us|status/.test(m.msg.contextInfo.remoteJid || '') ? participant : m.msg.contextInfo.remoteJid || '', fromMe: (0, baileys_1.areJidsSameUser)(participant, (0, baileys_1.jidNormalizedUser)(((_w = sock.user) === null || _w === void 0 ? void 0 : _w.id) || '')), id: stanzaId, device: /^3A/.test(stanzaId) ? 'ios' : /^3E/.test(stanzaId) ? 'web' : /^.{21}/.test(stanzaId) ? 'android' : /^.{18}/.test(stanzaId) ? 'desktop' : 'unknown', participant, sender: participant, mentions: [...(((_x = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.contextInfo) === null || _x === void 0 ? void 0 : _x.mentionedJid) || []), ...(((_y = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.contextInfo) === null || _y === void 0 ? void 0 : _y.groupMentions) || []).map((v) => v.groupJid).filter(Boolean)], body: (quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.conversation) || ((_z = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.extendedTextMessage) === null || _z === void 0 ? void 0 : _z.text) || ((_0 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.imageMessage) === null || _0 === void 0 ? void 0 : _0.caption) || ((_1 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.videoMessage) === null || _1 === void 0 ? void 0 : _1.caption) || ((_2 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.buttonsResponseMessage) === null || _2 === void 0 ? void 0 : _2.selectedButtonId) || ((_4 = (_3 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.listResponseMessage) === null || _3 === void 0 ? void 0 : _3.singleSelectReply) === null || _4 === void 0 ? void 0 : _4.selectedRowId) || ((_5 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.buttonsResponseMessage) === null || _5 === void 0 ? void 0 : _5.selectedDisplayText) || ((_6 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.documentMessage) === null || _6 === void 0 ? void 0 : _6.title) || ((_7 = quotedMsg === null || quotedMsg === void 0 ? void 0 : quotedMsg.contactMessage) === null || _7 === void 0 ? void 0 : _7.displayName) || '', prefix: '', command: '', args: [], text: '', }; if (m.quoted.body) { m.quoted.prefix = ((_8 = m.quoted.body.match(/^[°•π÷×¶∆£¢€¥®™+✓=|/~!?../..#%^&.©^]/)) === null || _8 === void 0 ? void 0 : _8[0]) || ''; m.quoted.command = m.quoted.body.replace(m.quoted.prefix, '').split(/ +/).shift() || ''; m.quoted.args = m.quoted.body .trim() .replace(new RegExp('^' + (0, exports.escapeRegExp)(m.quoted.prefix), 'i'), '') .replace(m.quoted.command, '') .split(/ +/) .filter(Boolean); m.quoted.text = m.quoted.args.join(' ') || m.quoted.body; } } m.reply = async (text, options = {}) => { try { await sock.presenceSubscribe(m.from); await sock.sendPresenceUpdate('composing', m.from); await sock.readMessages([m.key]); await (0, Utils_1.delay)(1000); const res = await sock.sendMessage(m.from, typeof text === 'string' ? { text, ...options } : { ...text, ...options }, { quoted: m, ephemeralExpiration: m.expiration, ...options }); if (!res) throw new Error('sendMessage failed and returned undefined'); return res; } catch (err) { throw new Error(`Gagal kirim pesan: ${err}`); } }; m.react = async (text) => { const res = await sock.sendMessage(m.from, { react: { text, key: m.key }, }); if (!res) throw new Error('sendMessage failed and returned undefined'); return res; }; return m; } const escapeRegExp = (str) => str.replace(/[.*=+:\-?^${}()|[\]\\]|\s/g, '\\$&'); exports.escapeRegExp = escapeRegExp; const getContentType = (content) => { if (!content) return ''; const keys = Object.keys(content); return keys.find((k) => (k === 'conversation' || k.endsWith('Message') || k.includes('V2') || k.includes('V3')) && k !== 'senderKeyDistributionMessage'); }; exports.getContentType = getContentType; // eslint-disable-next-line @typescript-eslint/no-explicit-any const parseMessage = (content) => { var _a; content = (0, baileys_1.extractMessageContent)(content); if (content === null || content === void 0 ? void 0 : content.viewOnceMessageV2Extension) content = content.viewOnceMessageV2Extension.message; if (((_a = content === null || content === void 0 ? void 0 : content.protocolMessage) === null || _a === void 0 ? void 0 : _a.type) == 14) { const type = (0, exports.getContentType)(content.protocolMessage); content = content.protocolMessage[type || 'ephemeralMessage']; } if (content === null || content === void 0 ? void 0 : content.message) { const type = (0, exports.getContentType)(content.message); content = content.message[type || '']; } return content; }; exports.parseMessage = parseMessage;