-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathindex.js
218 lines (195 loc) · 7.82 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
require('./settings');
const fs = require('fs');
const pino = require('pino');
const path = require('path');
const axios = require('axios');
const chalk = require('chalk');
const readline = require('readline');
const FileType = require('file-type');
const { exec } = require('child_process');
const { Boom } = require('@hapi/boom');
const NodeCache = require('node-cache');
const PhoneNumber = require('awesome-phonenumber');
const { default: WAConnection, useMultiFileAuthState, Browsers, DisconnectReason, makeInMemoryStore, makeCacheableSignalKeyStore, fetchLatestWaWebVersion, proto, PHONENUMBER_MCC, getAggregateVotesInPollMessage } = require('@whiskeysockets/baileys');
const pairingCode = process.argv.includes('--pairing-code');
const useMobile = process.argv.includes("--mobile")
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
const store = makeInMemoryStore({ logger: pino().child({ level: 'silent', stream: 'store' }) })
const question = (text) => new Promise((resolve) => rl.question(text, resolve))
global.api = (name, path = '/', query = {}, apikeyqueryname) => (name in global.APIs ? global.APIs[name] : name) + path + (query || apikeyqueryname ? '?' + new URLSearchParams(Object.entries({ ...query, ...(apikeyqueryname ? { [apikeyqueryname]: global.APIKeys[name in global.APIs ? global.APIs[name] : name] } : {}) })) : '')
const DataBase = require('./src/database');
const database = new DataBase();
(async () => {
const loadData = await database.read()
if (loadData && Object.keys(loadData).length === 0) {
global.db = {
sticker: {},
users: {},
groups: {},
database: {},
settings: {},
others: {},
...(loadData || {}),
}
await database.write(global.db)
} else {
global.db = loadData
}
setInterval(async () => {
if (global.db) await database.write(global.db)
}, 30000)
})();
const { GroupUpdate, GroupParticipantsUpdate, MessagesUpsert, Solving } = require('./src/message');
const { imageToWebp, videoToWebp, writeExifImg, writeExifVid } = require('./lib/exif');
const { isUrl, generateMessageTag, getBuffer, getSizeMedia, fetchJson, await, sleep } = require('./lib/function');
async function startXeonBot() {
const { state, saveCreds } = await useMultiFileAuthState('session');
const { version } = await fetchLatestWaWebVersion()
const msgRetryCounterCache = new NodeCache()
const level = pino({ level: 'silent' })
const getMessage = async (key) => {
if (store) {
const msg = await store.loadMessage(key.remoteJid, key.id);
return msg?.message
}
return {
conversation: 'Cheems Bot Here!'
}
}
const XeonBotInc = WAConnection({
version,
logger: level,
printQRInTerminal: !pairingCode,
browser: Browsers.ubuntu('Chrome'),
patchMessageBeforeSending: (message) => {
const requiresPatch = !!(
message.buttonsMessage ||
message.templateMessage ||
message.listMessage
);
if (requiresPatch) {
message = {
viewOnceMessage: {
message: {
messageContextInfo: {
deviceListMetadataVersion: 2,
deviceListMetadata: {},
},
...message,
},
},
};
}
return message;
},
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, level),
},
transactionOpts: {
maxCommitRetries: 10,
delayBetweenTriesMs: 10,
},
getMessage,
syncFullHistory: true,
maxMsgRetryCount: 15,
msgRetryCounterCache,
retryRequestDelayMs: 10,
connectTimeoutMs: 60000,
keepAliveIntervalMs: 10000,
defaultQueryTimeoutMs: undefined,
generateHighQualityLinkPreview: true,
})
if (pairingCode && !XeonBotInc.authState.creds.registered) {
let phoneNumber;
async function getPhoneNumber() {
phoneNumber = await question(chalk.bgBlack(chalk.greenBright('Please type your WhatsApp number : ')));
phoneNumber = phoneNumber.replace(/[^0-9]/g, '')
if (!Object.keys(PHONENUMBER_MCC).some(v => phoneNumber.startsWith(v)) && !phoneNumber.length < 6) {
console.log(chalk.bgBlack(chalk.redBright('Start with your Country WhatsApp code') + chalk.whiteBright(',') + chalk.greenBright(' Example : 62xxx')));
await getPhoneNumber()
}
}
setTimeout(async () => {
await getPhoneNumber()
let code = await XeonBotInc.requestPairingCode(phoneNumber);
console.log(chalk.black(chalk.bgGreen(`Your Pairing Code : `)), chalk.black(chalk.white(code)));
}, 3000)
}
store.bind(XeonBotInc.ev)
await Solving(XeonBotInc, store)
XeonBotInc.ev.on('creds.update', saveCreds)
XeonBotInc.ev.on('connection.update', async (update) => {
const { connection, lastDisconnect, receivedPendingNotifications } = update
if (connection === 'close') {
const reason = new Boom(lastDisconnect?.error)?.output.statusCode
if (reason === DisconnectReason.connectionLost) {
console.log('Connection to Server Lost, Attempting to Reconnect...');
startXeonBot()
} else if (reason === DisconnectReason.connectionClosed) {
console.log('Connection closed, Attempting to Reconnect...');
startXeonBot()
} else if (reason === DisconnectReason.restartRequired) {
console.log('Restart Required...');
startXeonBot()
} else if (reason === DisconnectReason.timedOut) {
console.log('Connection Timed Out, Attempting to Reconnect...');
startXeonBot()
} else if (reason === DisconnectReason.badSession) {
console.log('Delete Session and Scan again...');
process.exit(1)
} else if (reason === DisconnectReason.connectionReplaced) {
console.log('Close current Session first...');
XeonBotInc.logout();
} else if (reason === DisconnectReason.loggedOut) {
console.log('Scan again and Run...');
} else if (reason === DisconnectReason.Multidevicemismatch) {
console.log('Scan again...');
} else {
XeonBotInc.end(`Unknown DisconnectReason : ${reason}|${connection}`)
}
}
if (connection == 'open') {
console.log('Connected to : ' + JSON.stringify(XeonBotInc.user, null, 2));
} else if (receivedPendingNotifications == 'true') {
console.log('Please wait About 1 Minute...')
}
});
XeonBotInc.ev.on('contacts.update', (update) => {
for (let contact of update) {
let id = XeonBotInc.decodeJid(contact.id)
if (store && store.contacts) store.contacts[id] = { id, name: contact.notify }
}
});
XeonBotInc.ev.on('call', async (call) => {
let botNumber = await XeonBotInc.decodeJid(XeonBotInc.user.id);
let anticall = global.db.settings[botNumber].anticall
if (anticall) {
for (let id of call) {
if (id.status === 'offer') {
let msg = await XeonBotInc.sendMessage(id.from, { text: `Currently, We Cannot Receive Calls ${id.isVideo ? 'Video' : 'Suara'}.\nIf @${id.from.split('@')[0]} Need Help, Please Contact Owner :)`, mentions: [id.from]});
await XeonBotInc.sendContact(id.from, global.owner, msg);
await XeonBotInc.rejectCall(id.id, id.from)
}
}
}
});
XeonBotInc.ev.on('groups.update', async (update) => {
await GroupUpdate(XeonBotInc, update, store);
});
XeonBotInc.ev.on('group-participants.update', async (update) => {
await GroupParticipantsUpdate(XeonBotInc, update);
});
XeonBotInc.ev.on('messages.upsert', async (message) => {
await MessagesUpsert(XeonBotInc, message, store);
});
return XeonBotInc
}
startXeonBot()
let file = require.resolve(__filename)
fs.watchFile(file, () => {
fs.unwatchFile(file)
console.log(chalk.redBright(`Update ${__filename}`))
delete require.cache[file]
require(file)
});