@mengkodingan/ckptw
Version:
Create powerful WhatsApp bots easily
57 lines (56 loc) • 3.21 kB
JavaScript
;
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());
});
};
const Functions_1 = require("../Common/Functions");
const Ctx_1 = require("../Classes/Ctx");
module.exports = (self, runMiddlewares) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
let { cmd, prefix, m } = self;
if (!(m === null || m === void 0 ? void 0 : m.message) || ((_a = m.key) === null || _a === void 0 ? void 0 : _a.remoteJid) === "status@broadcast")
return;
if (!self.selfReply && m.key.fromMe)
return;
const hasHears = Array.from(self.hearsMap.values()).filter((x) => x.name === m.content ||
x.name === m.messageType ||
new RegExp(x.name).test(m.content) ||
(Array.isArray(x.name) && x.name.includes(m.content)));
if (hasHears.length)
return hasHears.forEach((x) => x.code(new Ctx_1.Ctx({ used: { hears: m.content }, args: [], self, client: self.core })));
let commandsList = Array.from((_b = cmd === null || cmd === void 0 ? void 0 : cmd.values()) !== null && _b !== void 0 ? _b : []);
let selectedPrefix;
if (Array.isArray(prefix)) {
if (prefix[0] == "") {
const emptyIndex = prefix.findIndex((x) => x.includes(""));
prefix = (0, Functions_1.arrayMove)(prefix, emptyIndex - 1, prefix.length - 1);
}
else {
selectedPrefix = prefix.find((p) => { var _a; return (_a = m.content) === null || _a === void 0 ? void 0 : _a.startsWith(p); });
}
}
else if (prefix instanceof RegExp) {
const match = (_c = m.content) === null || _c === void 0 ? void 0 : _c.match(prefix);
if (match)
selectedPrefix = match[0];
}
if (!selectedPrefix)
return;
let args = ((_d = m.content) === null || _d === void 0 ? void 0 : _d.slice(selectedPrefix.length).trim().split(/\s+/)) || [];
let commandName = (_e = args === null || args === void 0 ? void 0 : args.shift()) === null || _e === void 0 ? void 0 : _e.toLowerCase();
if (!commandName)
return;
const matchedCommands = commandsList.filter((c) => c.name.toLowerCase() === commandName ||
(Array.isArray(c.aliases) ? c.aliases.includes(commandName) : c.aliases === commandName));
if (!matchedCommands.length)
return;
let ctx = new Ctx_1.Ctx({ used: { prefix: selectedPrefix, command: commandName }, args, self, client: self.core });
if (!(yield runMiddlewares(ctx)))
return;
matchedCommands.forEach((cmd) => cmd.code(ctx));
});