-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpair.js
More file actions
156 lines (126 loc) Β· 5.55 KB
/
pair.js
File metadata and controls
156 lines (126 loc) Β· 5.55 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
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
const { macoderid } = require('./id');
const express = require('express');
const fs = require('fs');
let router = express.Router();
const pino = require("pino");
const { Storage } = require("megajs");
const {
default: macoder-tech,
useMultiFileAuthState,
delay,
makeCacheableSignalKeyStore,
Browsers
} = require("@whiskeysockets/baileys");
// Function to generate a random Mega ID
function randomMegaId(length = 6, numberLength = 4) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
const number = Math.floor(Math.random() * Math.pow(10, numberLength));
return `${result}${number}`;
}
// Function to upload credentials to Mega
async function uploadCredsToMega(credsPath) {
try {
const storage = await new Storage({
email: 'manyakaontiretse@gmail.com', // Your Mega A/c Email Here
password: 'macoder266' // Your Mega A/c Password Here
}).ready;
console.log('Mega storage initialized.');
if (!fs.existsSync(credsPath)) {
throw new Error(`File not found: ${credsPath}`);
}
const fileSize = fs.statSync(credsPath).size;
const uploadResult = await storage.upload({
name: `${randomMegaId()}.json`,
size: fileSize
}, fs.createReadStream(credsPath)).complete;
console.log('Session successfully uploaded to Mega.');
const fileNode = storage.files[uploadResult.nodeId];
const megaUrl = await fileNode.link();
console.log(`Session Url: ${megaUrl}`);
return megaUrl;
} catch (error) {
console.error('Error uploading to Mega:', error);
throw error;
}
}
// Function to remove a file
function removeFile(FilePath) {
if (!fs.existsSync(FilePath)) return false;
fs.rmSync(FilePath, { recursive: true, force: true });
}
// Router to handle pairing code generation
router.get('/', async (req, res) => {
const id = macoderid();
let num = req.query.number;
async function MACODER_PAIR_CODE() {
const { state, saveCreds } = await useMultiFileAuthState('./temp/' + id);
try {
let Macoder = Macoder_tech({
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, pino({ level: "fatal" }).child({ level: "fatal" })),
},
printQRInTerminal: false,
logger: pino({ level: "fatal" }).child({ level: "fatal" }),
browser: Browsers.macOS("Safari")
});
if (!Macoder.authState.creds.registered) {
await delay(1500);
num = num.replace(/[^0-9]/g, '');
const code = await Macoder.requestPairingCode(num);
console.log(`Your Code: ${code}`);
if (!res.headersSent) {
res.send({ code });
}
}
Maacoder.ev.on('creds.update', saveCreds);
Macoder.ev.on("connection.update", async (s) => {
const { connection, lastDisconnect } = s;
if (connection === "open") {
await delay(5000);
const filePath = __dirname + `/temp/${id}/creds.json`;
if (!fs.existsSync(filePath)) {
console.error("File not found:", filePath);
return;
}
const megaUrl = await uploadCredsToMega(filePath);
const sid = megaUrl.includes("https://mega.nz/file/")
? 'MacoX-MD~' + megaUrl.split("https://mega.nz/file/")[1]
: 'Error: Invalid URL';
console.log(`Session ID: ${sid}`);
const session = await Macoder.sendMessage(Macoder.user.id, { text: sid });
const MACODER_TEXT = `
π *Welcome to MacoX-MD!* π
π *Your Session ID* is ready! β οΈ _Keep it private and secure β dont share it with anyone._
π *Copy & Paste the SESSION_ID Above*π οΈ Add it to your environment variable: *SESSION_ID*.
π‘ *Whats Next?*
1οΈβ£ Explore all the cool features of botname.
2οΈβ£ Stay updated with our latest releases and support.
3οΈβ£ Enjoy seamless WhatsApp automation! π€
π *Join Our Support Channel:* π [Click Here to Join](https://whatsapp.com/channel/0029Vac8SosLY6d7CAFndv3Z)
β *Show Some Love!* Give us a β on GitHub and support the developer of: π [macoder67 GitHub Repo](https://github.com/macoder67/)
π _Thanks for choosing MacoX-MD β Let the automation begin!_ β¨`;
await Macoder.sendMessage(Macoder.user.id, { text: MACODER_TEXT }, { quoted: session });
await delay(100);
await Macoder.ws.close();
return removeFile('./temp/' + id);
} else if (connection === "close" && lastDisconnect && lastDisconnect.error && lastDisconnect.error.output.statusCode !== 401) {
await delay(10000);
MACODER_PAIR_CODE();
}
});
} catch (err) {
console.error("Service Has Been Restarted:", err);
removeFile('./temp/' + id);
if (!res.headersSent) {
res.send({ code: "Service is Currently Unavailable" });
}
}
}
await MacoX-MD_PAIR_CODE();
});
module.exports = router;