Skip to content

Commit ccaddd1

Browse files
committed
chore: move client promise to fire and forget & throw an error when client disconnected
1 parent 59cf651 commit ccaddd1

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/components/client.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
LocalAuth,
77
Message,
88
Reaction,
9+
WAState,
910
} from "whatsapp-web.js";
1011
import qrcode from "qrcode-terminal";
1112
import LoadingBar from "./utils/loadingBar";
@@ -20,6 +21,7 @@ import callEvent from "./events/call";
2021
import DownloadMedia from "./utils/message/download";
2122
import CheckSpamLink from "./utils/phishtank/checkSpam";
2223
import queue from "./queue";
24+
import groupAdminChanged from "./events/groups/groupAdminChanged";
2325

2426
let instance: Client | null = null;
2527
let isLoadingBarStarted = false;
@@ -50,6 +52,7 @@ async function client(): Promise<Client> {
5052
"--metrics-recording-only",
5153
"--disable-hang-monitor",
5254
],
55+
defaultViewport: { width: 1366, height: 768 },
5356
executablePath:
5457
process.env.PUPPETEER_EXEC_PATH || "/opt/google/chrome/google-chrome",
5558
},
@@ -86,22 +89,21 @@ function registerEvents(client: Client): void {
8689

8790
client.on("message_reaction", (react: Reaction) => reaction(client, react));
8891

89-
client.on("message_create", async (msg: Message) => {
90-
await Promise.allSettled([
91-
CheckSpamLink(msg),
92-
queue.add(() => DownloadMedia(msg)),
93-
messageEvent(msg, "create"),
94-
]);
92+
client.on("message_create", (msg: Message) => {
93+
CheckSpamLink(msg);
94+
queue.add(() => DownloadMedia(msg));
95+
messageEvent(msg, "create");
9596
});
97+
client.on("media_uploaded", (msg: Message) =>
98+
queue.add(() => DownloadMedia(msg)),
99+
);
96100

97101
client.on(
98102
"message_edit",
99-
async (msg: Message, newBody: string, prevBody: string) => {
103+
(msg: Message, newBody: string, prevBody: string) => {
100104
msg.body = newBody;
101-
await Promise.allSettled([
102-
messageEdit(msg, newBody, prevBody),
103-
messageEvent(msg, "edit"),
104-
]);
105+
messageEdit(msg, newBody, prevBody);
106+
messageEvent(msg, "edit");
105107
},
106108
);
107109

@@ -113,11 +115,18 @@ function registerEvents(client: Client): void {
113115

114116
client.on("group_join", (notif: GroupNotification) => groupJoin(notif));
115117
client.on("group_leave", (notif: GroupNotification) => groupLeave(notif));
118+
client.on("group_admin_changed", (notif: GroupNotification) =>
119+
groupAdminChanged(notif),
120+
);
116121

117122
client.on("auth_failure", () => {
118123
loadingBar.stop();
119124
log.error("Auth", "Authentication failed. Please try again.");
120125
});
126+
127+
client.on("disconnected", (reason: WAState | "LOGOUT") => {
128+
throw Error(`Client has been disconnected reason: ${reason}`);
129+
});
121130
}
122131

123132
export { client };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { GroupNotification } from "whatsapp-web.js";
2+
import log from "../../utils/log";
3+
import sleep from "../../utils/sleep";
4+
import { client } from "../../client";
5+
import { getMessage } from "../../../data/group";
6+
import * as Sentry from "@sentry/node";
7+
8+
export default async function (notif: GroupNotification): Promise<void> {
9+
try {
10+
if (notif.timestamp < Date.now() / 1000 - 10) return;
11+
} catch (err) {
12+
Sentry.captureException(err);
13+
log.error("GroupJoin", "Failed to process group admin changed event:", err);
14+
}
15+
}

0 commit comments

Comments
 (0)