@kamuridesu/whatframework
Version:
A simple WhatsApp Bot Framework on top of Baileys
69 lines (68 loc) • 3.02 kB
JavaScript
import { parseMessage } from '../funcs/parser.js';
import { pollParser } from '../funcs/updatesParsers.js';
import { colors } from '../../libs/std.js';
class WAMessageHandler {
constructor(entrypoint) {
this.isModule = !!entrypoint;
if (this.isModule) {
this.entryPointHandler = entrypoint;
}
}
async handle(message, bot) {
var _a, _b, _c, _d, _e;
if (!message.message ||
(message.key && message.key.remoteJid === 'status@broadcast') ||
(message.key && ((_a = message.key.id) === null || _a === void 0 ? void 0 : _a.startsWith('BAE5')) && ((_b = message.key.id) === null || _b === void 0 ? void 0 : _b.length) === 16))
return;
message.message =
Object.keys(message.message)[0] === 'ephemeralMessage'
? (_c = message.message.ephemeralMessage) === null || _c === void 0 ? void 0 : _c.message
: message.message;
const messageData = await parseMessage(message, bot);
if (!messageData) {
return;
}
if (this.isModule) {
const messageBody = messageData.body ? messageData.body : "";
if (messageBody.startsWith(bot.prefix)) {
const command = messageBody.split(bot.prefix)[1].split(' ')[0].toLowerCase();
colors.paint(`Command ${command} from ${messageData.author.name}`, colors.FgCyan, undefined, colors.Bright);
if (command.length === 0)
return;
const args = messageBody.split(' ').slice(1);
(_d = this.entryPointHandler) === null || _d === void 0 ? void 0 : _d.commandHandlers(bot, command, args, messageData);
}
else {
(_e = this.entryPointHandler) === null || _e === void 0 ? void 0 : _e.chatHandlers(bot, messageBody, messageData);
}
}
}
async handleUpdate(key, updates, bot) {
if (key) {
if (updates.pollUpdates) {
const pollData = await pollParser(key, updates, bot);
}
}
}
async handleNewMember(data, bot) {
var _a, _b;
const newData = {
id: data.id,
author: data.author,
participants: data.participants,
};
if ((_a = this.entryPointHandler) === null || _a === void 0 ? void 0 : _a.addMemberHandlers)
(_b = this.entryPointHandler) === null || _b === void 0 ? void 0 : _b.addMemberHandlers(bot, newData);
}
async handleRemoveMember(data, bot) {
var _a, _b;
const newData = {
id: data.id,
author: data.author,
participants: data.participants,
};
if ((_a = this.entryPointHandler) === null || _a === void 0 ? void 0 : _a.removeMemberHandlers)
(_b = this.entryPointHandler) === null || _b === void 0 ? void 0 : _b.removeMemberHandlers(bot, newData);
}
}
export { WAMessageHandler as MessageHandler };