Skip to content

Instantly share code, notes, and snippets.

@0xdeepmehta
Last active November 25, 2020 05:39
Show Gist options
  • Save 0xdeepmehta/9d231f01f11678476c2dfb8ffa966150 to your computer and use it in GitHub Desktop.
Save 0xdeepmehta/9d231f01f11678476c2dfb8ffa966150 to your computer and use it in GitHub Desktop.
const wa = require("@open-wa/wa-automate");
const { create, decryptMedia, ev } = wa;
const { default: PQueue } = require("p-queue");
const fetch = require("node-fetch");
const wakeBotOn = ["#help", "#menu", "#bot"];
const helpText =
process.env.HELP_TEXT ||
`Commands:
#sticker: caption a image/video/gif to turn it into sticker
#spam: tag everyone in a message in a group
#spam {n}: spam n number of times
#join {link}: joing a group with invite link provided
#google {query}: reply to a message or text with query to help a idot
#leave: i hope you dont use this if you do make sure youre admin
#help: To recive this same message
#pill : get the inspirational pill
Put '#nospam' in group description to stop spam commands
Author: Deep Mehta
Little bit about me: Dreamer, Living in illusion, Doing Miracle
`;
const leaveText =
process.env.LEAVE_TEXT ||
"Ab unko humshe rishta nhi rakhna hai\nto humari taraf se bhi koi zabardasti nhi hai";
const quotUrl = "https://api.quotable.io/random";
// Quote function
async function generateQuote() {
const url = "https://api.quotable.io/random";
try {
let res = await fetch(url);
return await res.json();
} catch (error) {
console.log(error);
}
}
const queue = new PQueue({
concurrency: 4,
autoStart: false,
});
let cl = null;
async function procMess(message) {
if (message.type === "chat") {
if (wakeBotOn.includes(message.body.toLowerCase())) {
await cl.sendText(message.from, helpText);
} else if (message.body.startsWith("#spam")) {
if (
message.chat.groupMetadata.desc &&
message.chat.groupMetadata.desc.includes("#nospam")
) {
await cl.sendText(message.chatId, "Spam protected group");
} else {
const text = `@${
message.author.split("@")[0]
} says hello ${message.chat.groupMetadata.participants.map(
(participant) =>
`\n🌚 @${
typeof participant.id === "string"
? participant.id.split("@")[0]
: participant.user
}`
)}`;
if (message.body === "#spam") {
await cl.sendTextWithMentions(message.chatId, text);
} else {
const n = parseInt(message.body.replace("#spam ", ""));
if (n < 20) {
const messages = [];
for (let i = 0; i < n; i++) {
messages.push(
await cl.sendTextWithMentions(message.chatId, text)
);
}
await Promise.all(messages);
} else {
await cl.sendText(message.chatId, "Spam limit 20");
}
}
}
} else if (message.body.startsWith("#google")) {
const query =
message.body === "#google" && message.quotedMsgObj
? message.quotedMsgObj.body
: message.body.replace("#google ", "");
await cl.sendText(
message.chatId,
`Here's something that'll help https://www.google.com/search?q=${encodeURIComponent(
query
)}`
);
} else if (message.body === "#teachers") {
await cl.sendText(message.chatId, teacherInfo)
}else if(message.body === '#pill') {
// fetch quote
var quote = await generateQuote();
var finalQuote = `"${quote.content}" - ${quote.author}`
await cl.sendText(message.chatId, finalQuote)
}else if (message.body === "#leave") {
const user = message.chat.groupMetadata.participants.find(
(pat) => pat.id === message.author
);
if (user && user.isAdmin) {
await cl.sendText(message.chatId, leaveText);
await cl.leaveGroup(message.chat.id);
} else {
await cl.sendText(message.chatId, "You no admin!");
}
} else if (message.body === "hi") {
await cl.sendText(message.chatId, '👋 Hello!')
}
} else if (
["image", "video"].includes(message.type) &&
message.caption === "#sticker"
) {
await cl.sendText(message.chatId, "wait Public, Processing sticker ...");
const mediaData = await decryptMedia(message);
const dataUrl = `data:${message.mimetype};base64,${mediaData.toString(
"base64"
)}`;
try {
message.type === "image" &&
(await cl.sendImageAsSticker(message.chatId, dataUrl));
message.type === "video" &&
(await cl.sendMp4AsSticker(message.chatId, dataUrl));
await cl.sendText(message.chatId, "Here is your sticker");
} catch (e) {
await cl.sendText(
message.chatId,
e.message || "Sticker size limit is 1Mb"
);
}
}
}
/**
* Add message to process queue
*/
const processMessage = (message) => queue.add(() => procMess(message));
async function start(client) {
cl = client;
queue.start();
const unreadMessages = await client.getAllUnreadMessages();
unreadMessages.forEach(processMessage);
client.onMessage(processMessage);
}
create({
qrTimeout: 0,
cacheEnabled: false,
}).then((client) => start(client));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment