-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (88 loc) · 3.17 KB
/
Copy pathindex.js
File metadata and controls
108 lines (88 loc) · 3.17 KB
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
107
108
const {
default: makeWASocket,
useMultiFileAuthState,
DisconnectReason,
fetchLatestBaileysVersion,
} = require("@whiskeysockets/baileys");
const fs = require("fs");
const P = require("pino");
const qrcode = require("qrcode-terminal");
const TARGET_GROUP_NAME = "PEDAL TEST";
const SCORES_FILE = "./scores.json";
if (!fs.existsSync(SCORES_FILE)) {
fs.writeFileSync(
SCORES_FILE,
JSON.stringify({ "Team One": 0, "Team Two": 0 }),
);
}
async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState("auth_baileys");
// Fetch latest version to avoid 405 error
const { version, isLatest } = await fetchLatestBaileysVersion();
console.log(`Using WhatsApp v${version.join(".")}, isLatest: ${isLatest}`);
const sock = makeWASocket({
version,
auth: state,
logger: P({ level: "silent" }),
printQRInTerminal: false,
});
sock.ev.on("connection.update", (update) => {
const { connection, lastDisconnect, qr } = update;
if (qr) {
console.log("\nScan this QR Code:");
qrcode.generate(qr, { small: true });
}
if (connection === "close") {
const shouldReconnect =
lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut;
if (shouldReconnect) connectToWhatsApp();
} else if (connection === "open") {
console.log(`Bot is Online and Monitoring Group Name: "${TARGET_GROUP_NAME}"`);
}
});
sock.ev.on("creds.update", saveCreds);
sock.ev.on("messages.upsert", async ({ messages }) => {
const msg = messages[0];
if (!msg.message) return;
const remoteJid = msg.key.remoteJid;
const text = (
msg.message.conversation ||
msg.message.extendedTextMessage?.text ||
""
).trim();
if (remoteJid.endsWith("@g.us")) {
try {
const groupMetadata = await sock.groupMetadata(remoteJid);
// Only proceed if the group name matches exactly
if (groupMetadata.subject !== TARGET_GROUP_NAME) return;
if (text.toLowerCase().startsWith("won by")) {
const parts = text.split(/Won by /i);
let winnerRaw = parts[1];
let winner = winnerRaw ? winnerRaw.trim() : "Unknown";
// Name Normalization
if (winner.toLowerCase().includes("one")) winner = "Team One";
if (winner.toLowerCase().includes("two")) winner = "Team Two";
let scores = JSON.parse(fs.readFileSync(SCORES_FILE));
if (scores[winner] !== undefined) {
scores[winner]++;
fs.writeFileSync(SCORES_FILE, JSON.stringify(scores, null, 2));
const newDesc =
`Weekend Padel\n\n` +
`Current Standings:\n` +
`Team One: ${scores["Team One"]}\n` +
`Team Two: ${scores["Team Two"]}\n`;
await sock.groupUpdateDescription(remoteJid, newDesc);
await sock.sendMessage(remoteJid, {
text: `Recorded! ${winner} now has ${scores[winner]} wins.`,
});
} else {
await sock.sendMessage(remoteJid, {
text: `Unknown Team: "${winner}"`,
});
}
}
} catch (err) { }
}
});
}
connectToWhatsApp();