UNPKG

@mengkodingan/ckptw

Version:

Create powerful WhatsApp bots easily

342 lines (341 loc) 14.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Ctx = void 0; const Functions_1 = require("../Common/Functions"); const baileys_1 = require("@whiskeysockets/baileys"); const MessageCollector_1 = require("./Collector/MessageCollector"); const GroupData_1 = require("./Group/GroupData"); const Group_1 = require("./Group/Group"); class Ctx { constructor(options) { this._used = options.used; this._args = options.args; this._self = options.self; this._client = options.client; this._msg = this._self.m; this._sender = { jid: (0, Functions_1.getSender)(this._msg, this._client), decodedJid: null, pushName: this._msg.pushName, }; if (this._sender.jid) this._sender.decodedJid = (0, Functions_1.decodeJid)(this._sender.jid); this._config = { prefix: this._self.prefix, cmd: this._self.cmd, }; } get id() { return this._msg.key.remoteJid; } get used() { return this._used; } get bot() { return this._self; } get me() { let user = this.core.user; user['decodedId'] = (0, Functions_1.decodeJid)(user === null || user === void 0 ? void 0 : user.id); user['readyAt'] = this.bot.readyAt; return user; } get core() { return this._client; } get decodedId() { if (this._msg.key.remoteJid) return (0, Functions_1.decodeJid)(this._msg.key.remoteJid); } get args() { return this._args; } get msg() { return Object.assign(Object.assign({}, this._msg), { media: { toBuffer: () => this.getMediaMessage(this._msg, 'buffer'), toStream: () => this.getMediaMessage(this._msg, 'stream') } }); } get sender() { return this._sender; } sendMessage(jid, content, options = {}) { var _a; return __awaiter(this, void 0, void 0, function* () { if (this._self.autoMention) { let matchMention = (_a = content.text) === null || _a === void 0 ? void 0 : _a.match(/(@[^](?![a-zA-Z]).\d*[$]*)/gm); if (matchMention) { for (let i = 0; i < matchMention.length; i++) { if (matchMention[i].match(/^@\d/gm)) { const num = matchMention[i].slice(1); if (content.mentions) { content.mentions.push(`${num}@s.whatsapp.net`); } else { content.mentions = []; content.mentions.push(`${num}@s.whatsapp.net`); } } } } } return this._client.sendMessage(jid, content, options); }); } reply(content, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (typeof content === 'string') content = { text: content }; return this.sendMessage(this.id, content, Object.assign({ quoted: this._msg }, options)); }); } replyWithJid(jid, content, options = {}) { return __awaiter(this, void 0, void 0, function* () { return this.sendMessage(jid, content, Object.assign({ quoted: this._msg }, options)); }); } react(jid, emoji, key) { return __awaiter(this, void 0, void 0, function* () { return this._client.sendMessage(jid, { react: { text: emoji, key: key ? key : this._msg.key }, }); }); } MessageCollector(args = { filter: function (args, collector) { throw new Error("Function not implemented."); }, time: 0, max: 0, maxProcessed: 0 }) { return new MessageCollector_1.MessageCollector({ self: this._self, msg: this._msg }, args); } awaitMessages(args = { filter: function (args, collector) { throw new Error("Function not implemented."); }, time: 0, max: 0, maxProcessed: 0 }) { return new Promise((resolve, reject) => { const col = this.MessageCollector(args); col.once("end", (collected, r) => { var _a; if ((_a = args.endReason) === null || _a === void 0 ? void 0 : _a.includes(r)) { reject(collected); } else { resolve(collected); } }); }); } getMessageType() { return this._msg.messageType; } getMediaMessage(msg, type) { return __awaiter(this, void 0, void 0, function* () { try { let buffer = yield (0, baileys_1.downloadMediaMessage)(msg, type, {}, { logger: this._self.logger, reuploadRequest: this._client.updateMediaMessage }); return buffer; } catch (_a) { return null; } }); } read() { let m = this._msg; this._client.readMessages([ { remoteJid: m.key.remoteJid, id: m.key.id, participant: m.key.participant }, ]); } simulateTyping() { this._client.sendPresenceUpdate('composing', this.id); } deleteMessage(key) { return __awaiter(this, void 0, void 0, function* () { return this._client.sendMessage(this.id, { delete: key }); }); } simulateRecording() { this._client.sendPresenceUpdate('recording', this.id); } editMessage(key, newText) { return __awaiter(this, void 0, void 0, function* () { return yield this.sendMessage(this.id, { text: newText, edit: key, }); }); } sendPoll(jid, args) { return __awaiter(this, void 0, void 0, function* () { args.selectableCount = args.singleSelect ? true : false; return this._client.sendMessage(jid, { poll: args }); }); } getMentioned() { var _a, _b; return ((_a = this._msg.message) === null || _a === void 0 ? void 0 : _a.extendedTextMessage) ? (_b = this._msg.message.extendedTextMessage.contextInfo) === null || _b === void 0 ? void 0 : _b.mentionedJid : []; } getDevice(id) { return (0, baileys_1.getDevice)(id ? id : this._msg.key.id); } isGroup() { var _a; return (_a = this.id) === null || _a === void 0 ? void 0 : _a.endsWith("@g.us"); } block(jid) { return __awaiter(this, void 0, void 0, function* () { if (jid) { yield this._client.updateBlockStatus((0, Functions_1.decodeJid)(jid), "block"); } else { yield this._client.updateBlockStatus((0, Functions_1.decodeJid)(this.id), "block"); } }); } unblock(jid) { return __awaiter(this, void 0, void 0, function* () { if (jid) { yield this._client.updateBlockStatus((0, Functions_1.decodeJid)(jid), "unblock"); } else { yield this._client.updateBlockStatus((0, Functions_1.decodeJid)(this.id), "unblock"); } }); } get groups() { return new Group_1.Group(this); } group(jid) { return new GroupData_1.GroupData(this, jid ? jid : this.id); } get quoted() { var _a, _b; let msgContext = (_b = (_a = this._msg.message) === null || _a === void 0 ? void 0 : _a.extendedTextMessage) === null || _b === void 0 ? void 0 : _b.contextInfo; let quotedMessage = msgContext === null || msgContext === void 0 ? void 0 : msgContext.quotedMessage; return Object.assign(Object.assign({}, quotedMessage), { senderJid: msgContext === null || msgContext === void 0 ? void 0 : msgContext.participant, decodedSenderJid: (0, Functions_1.decodeJid)(msgContext === null || msgContext === void 0 ? void 0 : msgContext.participant), media: { toBuffer: () => __awaiter(this, void 0, void 0, function* () { var _c, e_1, _d, _e; try { let type = this.getContentType(quotedMessage); let stream = yield this.downloadContentFromMessage(quotedMessage === null || quotedMessage === void 0 ? void 0 : quotedMessage[type], type.slice(0, -7)); let buffer = Buffer.from([]); try { for (var _f = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _c = stream_1_1.done, !_c;) { _e = stream_1_1.value; _f = false; try { const chunk = _e; buffer = Buffer.concat([buffer, chunk]); } finally { _f = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_f && !_c && (_d = stream_1.return)) yield _d.call(stream_1); } finally { if (e_1) throw e_1.error; } } return buffer; } catch (_g) { return null; } }), toStream: () => __awaiter(this, void 0, void 0, function* () { try { let type = this.getContentType(quotedMessage); let stream = yield this.downloadContentFromMessage(quotedMessage === null || quotedMessage === void 0 ? void 0 : quotedMessage[type], type.slice(0, -7)); return stream; } catch (_h) { return null; } }) } }); } getContentType(content) { return this._self.getContentType(content); } downloadContentFromMessage(downloadable, type, opts) { return this._self.downloadContentFromMessage(downloadable, type, opts); } sendInteractiveMessage(jid, content, options = {}) { var _a, _b, _c, _d, _e; let contentReal = (0, Functions_1.makeRealInteractiveMessage)(content); if (this._self.autoMention) { let matchMention = (_a = content.body) === null || _a === void 0 ? void 0 : _a.match(/(@[^](?![a-zA-Z]).\d*[$]*)/gm); if (matchMention) { for (let i = 0; i < matchMention.length; i++) { if (matchMention[i].match(/^@\d/gm)) { const num = matchMention[i].slice(1); if ((_b = contentReal.contextInfo) === null || _b === void 0 ? void 0 : _b.mentionedJid) { (_c = contentReal.contextInfo) === null || _c === void 0 ? void 0 : _c.mentionedJid.push(`${num}@s.whatsapp.net`); } else { contentReal.contextInfo = Object.assign(Object.assign({}, contentReal.contextInfo), { mentionedJid: [] }); (_e = (_d = contentReal.contextInfo) === null || _d === void 0 ? void 0 : _d.mentionedJid) === null || _e === void 0 ? void 0 : _e.push(`${num}@s.whatsapp.net`); } } } } } let msg = (0, baileys_1.generateWAMessageFromContent)(jid, { viewOnceMessage: { message: { "messageContextInfo": { "deviceListMetadata": {}, "deviceListMetadataVersion": 2 }, interactiveMessage: baileys_1.proto.Message.InteractiveMessage.create(contentReal) } } }, options); this._client.relayMessage(jid, msg.message, { messageId: msg.key.id }); } replyInteractiveMessage(content, options = {}) { return __awaiter(this, void 0, void 0, function* () { return this.sendInteractiveMessage(this.id, content, Object.assign({ quoted: this._msg }, options)); }); } prepareWAMessageMedia(message, options) { return __awaiter(this, void 0, void 0, function* () { return (0, baileys_1.prepareWAMessageMedia)(message, options); }); } decodeJid(jid) { return (0, Functions_1.decodeJid)(jid); } } exports.Ctx = Ctx;