-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (255 loc) · 8.87 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
require("dotenv").config();
process.env["NTBA_FIX_350"] = 1;
const TelegramBot = require("node-telegram-bot-api");
const ytdl = require("ytdl-core");
const ytpl = require("ytpl");
const MongoDB = require("./Database/database");
const db = new MongoDB();
const bot = new TelegramBot(process.env.BOT_TOKEN, {
polling: true,
filepath: false,
});
const {
validateScURL,
validateTiktTokURL,
getTikTokLink,
validateInstagramURL,
getInstagramID,
} = require("./functions/validateURL");
const { getUser } = require("./Database/models/User");
const { getStats } = require("./Database/models/Stats");
const YouTubeChoose = require("./Sources/YouTube/YouTubeChoose.js");
const TikTokChoose = require("./Sources/TikTok/TikTokChoose.js");
const language = require("./Languages");
const InstagramDownloader = require("./Sources/Instagram/instagramDownloader.js");
const SoundcloudDownloader = require("./Sources/SoundCloud/soundcloudDownloader.js");
const YouTubeVideoQuality = require("./Sources/YouTube/YouTubeVideoQuality.js");
const YouTubeMP3Downloder = require("./Sources/YouTube/YouTubeMP3Downloader.js");
const TiktTokMP3Downloder = require("./Sources/TikTok/TikTokMP3Downloader.js");
const TiktTokMP4Downloder = require("./Sources/TikTok/TikTokMP4Downloader.js");
const YouTubeMP4Downloder = require("./Sources/YouTube/YouTubeMP4Downloader.js");
bot.on("message", async (msg) => {
if (msg.from.is_bot) return;
let text = msg.text;
const chatId = msg.chat.id;
let userData = await getUser(msg.from);
let botUser = await bot.getMe();
let globalData = await getStats(botUser);
let lang = language[userData.language];
if (text.startsWith("/")) return;
if (userData.currentConverting >= 3 && !userData.isPremium) {
return bot.sendMessage(msg.chat.id, lang.premium.max_operation, {
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: lang.buttons.subscribe, callback_data: "subscribe" }],
],
one_time_keyboard: true,
}),
reply_to_message_id: msg.message_id,
});
}
//Youtube Handler
if (ytdl.validateURL(text)) {
const message = await bot.sendMessage(chatId, lang.info_load, {
reply_to_message_id: msg.message_id,
parse_mode: "HTML",
});
globalData.currentConverting += 1;
await globalData.save();
userData.currentConverting += 1;
await userData.save();
YouTubeChoose(bot, message, msg.text, userData, globalData);
}
//TikTok Handler
else if (validateTiktTokURL(text)) {
const tiktok_url = getTikTokLink(text);
let message = await bot.sendMessage(chatId, lang.info_load, {
reply_to_message_id: msg.message_id,
parse_mode: "HTML",
});
globalData.currentConverting += 1;
await globalData.save();
userData.currentConverting += 1;
await userData.save();
TikTokChoose(bot, message, tiktok_url, userData, globalData);
}
//SoundCloud Handler
else if (validateScURL(text)) {
let message = await bot.sendMessage(chatId, lang.info_load, {
reply_to_message_id: msg.message_id,
parse_mode: "HTML",
});
globalData.currentConverting += 1;
await globalData.save();
userData.currentConverting += 1;
await userData.save();
SoundcloudDownloader(bot, message, text, userData, globalData);
}
//YouTube Playlist Handler
else if (ytpl.validateID(text)) {
if (!userData.isPremium) {
return bot.sendMessage(msg.chat.id, lang.premium.playlist, {
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: lang.buttons.subscribe, callback_data: "subscribe" }],
],
one_time_keyboard: true,
}),
reply_to_message_id: msg.message_id,
});
}
const message = await bot.sendMessage(chatId, lang.info_load, {
parse_mode: "HTML",
reply_to_message_id: msg.message_id,
});
let playlistInfo = await ytpl(text);
await bot.editMessageText(
`Playlist Title: *${playlistInfo.title}*\nChannel: (${playlistInfo.author.name})\nVideos: ${playlistInfo.estimatedItemCount}`,
{
chat_id: chatId,
message_id: message.message_id,
reply_markup: JSON.stringify({
inline_keyboard: [
[
{
text: lang.buttons.download_audio,
callback_data: "audio_playlist",
},
],
[
{
text: lang.buttons.download_video,
callback_data: "video_playlist",
},
],
],
one_time_keyboard: true,
}),
}
);
}
//Instagram Handler
else if (validateInstagramURL(text)) {
const postId = getInstagramID(text);
let message = await bot.sendMessage(chatId, lang.info_load, {
reply_to_message_id: msg.message_id,
parse_mode: "HTML",
});
globalData.currentConverting += 1;
await globalData.save();
userData.currentConverting += 1;
await userData.save();
InstagramDownloader(bot, message, postId, userData, globalData);
}
//Invalid Links
else {
bot.sendMessage(chatId, lang.invalid_link, {
parse_mode: "HTML",
});
}
});
bot.on("callback_query", async function onCallbackQuery(callbackQuery) {
const action = callbackQuery.data;
const msg = callbackQuery.message;
let userData = await getUser(callbackQuery.from);
let botUser = await bot.getMe();
let globalData = await getStats(botUser);
let url = msg.reply_to_message.text;
let lang = language[userData.language];
const regexPattern =
/(\d+)_([0-9a-zA-Z]+)_type_([a-zA-Z]+)_size_([\d.]+ [KMGT]?B)/;
const matchResult = action.match(regexPattern);
//YouTube Video Choose Quality
if (action === "video_youtube") {
YouTubeVideoQuality(bot, msg, url, userData, globalData);
}
//YouTube Video Download
else if (matchResult && matchResult[3] == "Video") {
YouTubeMP4Downloder(bot, msg, url, userData, globalData, matchResult);
}
//YouTube Audio Download
else if (action === "audio_youtube") {
YouTubeMP3Downloder(bot, msg, url, userData, globalData);
}
//Change Arabic Language
else if (action === "arabic_lang") {
const opts = {
chat_id: msg.chat.id,
message_id: msg.message_id,
parse_mode: "HTML",
};
bot.editMessageText("*البوت الآن باللغة العربية* 🇵🇸", opts);
userData.language = "Arabic";
userData.activated = true;
await userData.save();
}
//Change English Language
else if (action === "english_lang") {
const opts = {
chat_id: msg.chat.id,
message_id: msg.message_id,
parse_mode: "HTML",
};
bot.editMessageText("*The bot is now in English* 🇬🇧", opts);
userData.language = "English";
userData.activated = true;
await userData.save();
}
//Handle Subscribe System
else if (action === "subscribe") {
return bot.sendMessage(msg.chat.id, lang.soon, {
parse_mode : "HTML",
reply_to_message_id: callbackQuery.message.message_id,
});
}
//TikTok Audio Download
else if (action === "audio_tiktok") {
TiktTokMP3Downloder(bot, msg, url, userData, globalData);
}
//TikTok Video Download
else if (action === "video_tiktok") {
TiktTokMP4Downloder(bot, msg, url, userData, globalData);
}
//YouTube Playlist Download
else if (action === "youtube_playlist_choose") {
return bot.sendMessage(msg.chat.id, lang.soon, {
parse_mode : "HTML",
reply_to_message_id: callbackQuery.message.message_id,
});
}
});
bot.onText(/\/start/, async (msg) => {
const chatId = msg.chat.id;
let userData = await getUser(msg.from);
// Send a message with the introduction and instructions
bot.sendMessage(chatId, language[userData.language].welcome_message, {
parse_mode: "HTML",
});
});
bot.onText(/\/lang/ || /\/language/ || /\/لغة/ || /\/لغه/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(
chatId,
`Hello <b>${msg.from.first_name || msg.from.username}</b>! 🌟
You can change the language by choosing your preferred language: 🌐
مرحبًا <b>${msg.from.first_name || msg.from.username}</b>! 🌟
يمكنك تغيير اللغة عن طريق اختيار لغتك المفضلة: 🌐`,
{
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "عربي 🇵🇸", callback_data: "arabic_lang" }],
[{ text: "English 🇬🇧", callback_data: "english_lang" }],
],
one_time_keyboard: true,
}),
reply_to_message_id: msg.message_id,
}
);
});
bot.getMe().then(async (client) => {
console.log(`Logged as ${client.username}`);
await db.init();
});