-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
425 lines (342 loc) · 11 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"use strict"
process.env["NTBA_FIX_319"] = 1;
const TelegramBot = require("node-telegram-bot-api");
const {sendmsg, edit} = require("./telegram/msgutils");
const Command = require("./telegram/command");
const fs = require("fs");
const path = require("path");
const {
DownloadFolderChecker,
isAuth,
DownloadList,
} = require("./utils/utils");
const {tgToken} = require("./config");
const {getAuthurl} = require("./drive/AuthUrl");
const {CreateToken} = require("./drive/token");
const diskinfo = require("./utils/disk");
const {deleteToken, TokenInsert} = require("./database/dbtoken");
const {MongoDB} = require("./database/userdb");
const Aria2 = require("aria2");
const {ariaListners} = require("./aria/ariahandler");
const ariaAdd = require("./aria/add");
const isTorrent = require("./utils/istorrent");
const {cancelgid} = require("./aria/ariacancel");
const PORT = 6800;
const ariaOptions = {
host: "localhost",
port: 6800,
secure: false,
secret: "",
path: "/jsonrpc",
};
const aria2 = new Aria2(ariaOptions);
aria2
.open()
.then(() => {
console.log("Websocket Opened Port", PORT);
})
.catch(() => {
console.log("WebSocket is closed exiting ..");
process.exit(1);
});
const startTime = Date.now();
//////////////////////////// Do not Touch This part ////////////////////////////////////////////////////////
const command = new Command();
// DownloadFolderChecker();
const token = tgToken;
const bot = new TelegramBot(token, {polling: true});
bot.on("polling_error", (msg) => console.error(msg.message));
//////////////////////////////////////////////////////////////////////////////////////
ariaListners(aria2, bot);
// Start
bot.onText(command.start, async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
const opts = {
reply_markup: {
inline_keyboard: [
[{text: "Join Channel", url: "https://t.me/aryan_bots"}],
],
},parse_mode: "HTML",
};
let Startmsg = `Hey ${msg.from.first_name}
Bot Uptime : <code> ${getuptime()} </code>
Note: <code>Check Uptime If You Think Your Download Is Not in List Or Disappear
It mostly happen Due To Server overload And restart Hope You Can understand 😁</code>
check /help Command For more information
`;
// await sendmsg("Just A Useless Command\nJoin @aryan_bots For Useless Updates😒 ", msg, bot);
await bot.sendMessage(msg.chat.id, Startmsg, opts);
console.log(DownloadList);
});
// callaback Function
bot.on("callback_query", async callbackQuery => {
const data = callbackQuery.data;
const msg = callbackQuery.message;
const opts = {
chat_id: msg.chat.id,
message_id: msg.message_id,
parse_mode: "HTML",
};
if (data.startsWith("cancel")) {
let gid = data.split(" ")[1];
// bot.editMessageText(d, opts);
await cancelgid(aria2, gid, msg, bot, opts);
}
});
// torrent file Handler
bot.on("document", async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
if (!(await isAuth(msg))) {
await sendmsg("You should Login first using /login ", msg, bot);
return;
}
if (msg.document.mime_type === "application/x-bittorrent") {
if(DownloadList[msg.from.id] === true){
await sendmsg("One Download At a Time Please Don't abuse it", msg, bot);
return
}
let uri = await bot.getFileLink(msg.document.file_id);
console.log(uri);
ariaAdd(aria2, uri, msg, bot);
}
});
// Only zip For zip Torrent File
bot.onText(command.doczip, async (msg) => {
// await sendmsg("Zip Temporary disabled", msg, bot);
// return ;
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
if (!(await isAuth(msg))) {
await sendmsg("You should Login first using /login ", msg, bot);
return;
}
if (
msg.reply_to_message &&
msg.reply_to_message.document &&
msg.reply_to_message.document.mime_type === "application/x-bittorrent"
) {
if(DownloadList[msg.from.id] === true){
await sendmsg("One Download At a Time Please Don't abuse it", msg, bot);
return
}
let uri = await bot.getFileLink(msg.reply_to_message.document.file_id);
console.log(uri);
ariaAdd(aria2, uri, msg, bot, true);
}
});
// upload Command
bot.onText(command.magnet, async (msg, match) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
let uri = match[0];
if(DownloadList[msg.from.id] === true){
await sendmsg("One Download At a Time Please Don't abuse it", msg, bot);
return
}
if (uri.startsWith("http")) {
if (!(await isTorrent(uri))) {
await sendmsg("Supports only Torrents 😔 ", msg, bot);
return;
}
}
if (await isAuth(msg)) {
// addTorrent(uri, msg, bot);
ariaAdd(aria2, uri, msg, bot, false);
} else {
await sendmsg("You should Login first using /login ", msg, bot);
}
});
// login
bot.onText(command.login, async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
if (await isAuth(msg)) {
await sendmsg("You Are Already Authorized !!", msg, bot);
} else {
await getAuthurl(msg, bot);
}
});
// Token Handler
bot.onText(command.token, async (msg, match) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
const code = match[0];
if (await isAuth(msg, true)) {
await sendmsg("Your Are Alredy logged in !!", msg, bot);
} else {
CreateToken(code, msg, bot);
setTimeout(() => {
TokenInsert(msg.from.id);
}, 2000);
}
});
//logout
bot.onText(command.logout, async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
// If Error That mean There is no file
const userId = msg.from.id.toString();
const credsPath = path.join(__dirname, `./creds/${userId}.json`);
if (await isAuth(msg, true)) {
fs.unlink(credsPath, (err) => {
if (!err) {
sendmsg("Logout Successfully !!", msg, bot);
console.log("logout success", userId);
} else {
console.error("deleting creds file", err);
}
});
await deleteToken(userId);
} else {
await sendmsg("You Are not Logged In Use /login !!", msg, bot);
}
});
// Zip
bot.onText(command.zip, async (msg, match) => {
// await sendmsg("Zip Temporary disabled", msg, bot);
// return;
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
const uri = match[1];
if(DownloadList[msg.from.id] === true){
await sendmsg("One Download At a Time Please Don't abuse it", msg, bot);
return
}
if (uri.startsWith("http")) {
if (!(await isTorrent(uri))) {
await sendmsg("Supports only Torrents 😔 ", msg, bot);
return;
}
}
if (await isAuth(msg)) {
ariaAdd(aria2, uri, msg, bot, true);
} else {
await sendmsg("You should Login first using /login ", msg, bot);
}
});
// cancel
bot.onText(command.cancel, async (msg, match) => {
await sendmsg("This command is deprecated", msg, bot);
return
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
await cancelgid(aria2, match[1], msg, bot);
// cancel(gid, msg, bot)
});
// help
bot.onText(command.help, async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
let helptext =
"⭐️ I can Download Torrent In Your Google Drive Account ⭐️ \n\n";
helptext += "/start - Start Command \n\n";
helptext += "/login - Login To Your Account \n\n";
helptext += "/logout - Logout Your Account \n\n";
helptext +=
"Upload (no need any command): <code>Just Send Your Magnet ,Torrent Direct Url or Torrent File: </code>\n\n";
helptext +=
"/zip - Upload compressed Version in your Drive ,same as Up command \n\n";
helptext +=
"/addtd - Add Your Teamdrive Id eg:<code>/addtd Xyz**** </code> \n\n";
helptext += "/rmtd - Will Remove Teamdrive from database\n\n";
helptext += "/disk - Server Disk Info and Uptime\n\n";
helptext += "Limit : <code>Max Size limit 20GB</code>\n";
await sendmsg(helptext, msg, bot);
});
// Disk Info
bot.onText(command.server, async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
await diskinfo(getuptime(), msg, bot);
});
// set Teamdrive Info
bot.onText(command.setTD, async (msg, match) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
let tdId = match[1].trim();
const mongo = await new MongoDB(msg.from.id)
try {
const res = await mongo.setTeamdrive(tdId)
if (res) {
await sendmsg(
`Teamdrive added ${await mongo.getTeamdrive()} `,
msg,
bot
);
} else {
await sendmsg(" Failed To Add Teamdrive", msg, bot);
}
}catch (e) {
console.error(e.message)
}
});
// set Teamdrive Info
bot.onText(command.removeTD, async (msg) => {
if (!(await userinChannel(msg))) {
await sendJoinChannel(msg);
return;
}
const mongo = await new MongoDB(msg.from.id)
await mongo.removeTeamdrive()
await sendmsg(`Teamdrive Removed from Database`, msg, bot);
});
const userinChannel = async (msg) => {
try {
let user = await bot.getChatMember("@aryan_bots", msg.from.id);
if (user.status === "member" || user.status === "administrator") {
console.log(user.status, msg.from.id);
return true;
} else {
console.log(user.status, msg.from.id);
return false;
}
} catch (e) {
console.log("not a member of channel: ", e.message);
return false;
}
};
const sendJoinChannel = async (msg) => {
const opts = {
reply_markup: {
inline_keyboard: [
[{text: "Join Channel", url: "https://t.me/aryan_bots"}],
],
},
};
// await sendmsg("Just A Useless Command\nJoin @aryan_bots For Useless Updates😒 ", msg, bot);
await bot.sendMessage(msg.chat.id, "Join Channel To access Bot", opts);
};
function getuptime() {
const currentTime = Date.now() - startTime;
const timestamp = Math.floor(currentTime / 1000);
const hours = Math.floor(timestamp / 60 / 60);
const minutes = Math.floor(timestamp / 60) - hours * 60;
const seconds = timestamp % 60;
return hours + ":" + minutes + ":" + seconds
}