-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
106 lines (88 loc) · 2.9 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require("fs");
var welcome = require("./utils/welcome");
var openai = require("./utils/openai");
var mentions = require("./utils/mentions");
var scheduler = require("./utils/scheduler");
var reddit = require("./utils/reddit");
const { Client, MessageMedia, LocalAuth, Buttons } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
/**
* @param {Client} client The string
*/
global.client = new Client({
authStrategy: new LocalAuth(),
// puppeteer: {
// executablePath: "/usr/bin/google-chrome-stable",
// },
});
client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
client.on("ready", () => {
console.log("Client is ready!");
scheduler.startAgenda();
});
client.initialize();
client.on("message_create", (message) => {
let body = message.body;
if (body.startsWith("!dost")) {
var array = body.split(" ");
var rootCmd = "";
typeof array[1] != "undefined" ? (rootCmd = array[1]) : false;
console.log(rootCmd);
//Commands
switch (rootCmd) {
case "":
case "help":
welcome.displayMenu(client, message);
break;
case "remindme":
var interval = array.slice(2, 5).join(" ");
var task = array.slice(5).join(" ");
scheduler.createReminder(message, interval, task);
break;
case "tagall":
mentions.tagAllGroupMembers(client, message);
break;
case "gpt":
var gptPrompt = array.slice(2).join(" ");
openai.fetchChatGPTReply(message, gptPrompt);
break;
case "dalle":
var gptPrompt = array.slice(2).join(" ");
openai.fetchDalleReply(message, gptPrompt);
break;
case "meme":
reddit.fetchHotPostsFromSubreddit("meme");
// Trying to do something like most used words in a group, analytics etc, but only a fraction of messages are being fetched by this
case "history":
(async () => {
var allMessages = [];
var chat = await message.getChat();
var messages = await chat.fetchMessages({ limit: Number.MAX_VALUE });
messages.forEach((e) => allMessages.push(e.body));
console.log("allMessages");
console.log(allMessages[0]);
console.log(allMessages[1]);
console.log(allMessages[allMessages.length - 1]);
console.log(allMessages.length);
})();
break;
case "song":
(async () => {
console.log("calling");
var chatId = (await message.getChat()).id._serialized;
console.log(chatId);
// var contact = (await message.getContact()).getChat.toString;
var url =
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley";
const media = await MessageMedia.fromUrl(url, {
unsafeMime: true,
});
client.sendMessage(chatId, url);
})();
break;
default:
}
}
});