index.js

Open WA 4.4.0
mail@pastecode.io avatar
unknown
javascript
3 years ago
3.5 kB
5
Indexable
Never
import { create, ev, Client } from "@open-wa/wa-automate";
import { existsSync, mkdir, stat } from "fs";
import PQueue from 'p-queue';
// import

const queue = new PQueue({
	intervalCap: 6,
	interval: 3000,
	concurrency: 3,
	timeout: 100,
	carryoverConcurrencyCount: true,
});


function createSession() {
	const sessionData = new URL('session', import.meta.url);
	
	if (existsSync(sessionData)) {
		console.log("Session sudah ada!");
	} else {
		mkdir(sessionData, { recursive: true }, (err) => {
			if (err) throw err;
		});
	}
	ev.on("sessionDataBase64.**", async (sessionDatastring, sessionId) => {
		console.log(sessionId, sessionDatastring);
	  });
}

function config(start, force = false) {
	const config = {
		authTimeout: 0,
		blockAssets: true,
		disableSpins: true,
		headless: false,
		killProcessOnBrowserClose: true,
		qrTimeout: 0,
		resizable: true,
		restartOnCrash: start,
		sessionId: 'P_BOT',
		sessionDataPath: 'session',
		skipUpdateCheck: true,
	};
	if (process.env.USER == 'iqbal' || force) {
		config.licenseKey = '';
		config.useChrome = true;
		config.chromiumArgs = [
			'--no-sandbox',
			'--disable-setuid-sandbox',
			'--aggressive-cache-discard',
			'--disable-cache',
			'--disable-application-cache',
			'--disable-offline-load-stale-cache',
			'--disk-cache-size=0',
			'--disable-gl-drawing-for-tests',
			'--disable-2d-canvas-clip-aa',
			'--disable-back-forward-cache',
			'--disable-canvas-aa',
			'--disable-component-cloud-policy',
			'--disable-composited-antialiasing',
			'--disable-dinosaur-easter-egg',
			'--disable-extensions',
			'--disable-login-animations',
			'--disable-machine-cert-request',
			'--disable-gpu-shader-disk-cache',
			'--disable-gpu-program-cache',
			'--v8-cache-options=off',
		];
	}
	return config;
}

const start = async (client = new Client()) => {
	await createSession(); 

	const status = await client.getLicenseType();
	const host = await client.getHostNumber();
	const unread = await client.getAllUnreadMessages();

	console.log("STATUS === "  + status);
	console.log('HOST === ' + host);

	client.onAnyMessage(message => queue.add(() => action(message)));
	unread.forEach(message => queue.add(() => action(message)));
	setInterval(() => client.setPresence(true), 10000);

	async function action(message) {
		const { caption, body, isMedia, sender, from, id } = message;
		client.sendSeen(from);
		const pure = caption || body || '';
		if (!pure.startsWith('/') || (isMedia && !caption)) return;
		const arg = pure.trim().split(/\s+/g);
		const command = arg.shift().slice(1).toLowerCase();
		const query = arg.join(' ');

		switch (command) {
			case 'b':
			case 'brain':
			case 'brainly': {
				
				// brainly(client, from, id, query, sender.id);
				break;
			}
			case 'clear': {
				// clear(client, sender.id, status);
				break;
			}
			case 'help':
			case 'info':
			case 'menu':
			case 'h': {
				client.sendText(from, 'Hai sayang')
				// help(client, from);
				break;
			}
			case 'i':
			case 'img': {
				// image(client, query, from, id);
				break;
			}
			case 'kick': {
				// kick();
				break;
			}
			case 'p':
			case 'pdf': {
				// pdf(client, from, sender);
				break;
			}
			case 'sticker':
			case 'stiker':
			case 's': {
				// stiker(client, message, host, status);
				break;
			}
			case 'yt':
			case 'youtube': {
				// youtube(client, query, id, from, sender.id);
				break;
			}
		}
	}
}

create(config(start)).then((client) => start(client));