forked from kjeymax/File-Store-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
360 lines (295 loc) · 11.8 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
require('dotenv').config()
const { Telegraf } = require('telegraf')
const bot = new Telegraf(process.env.TOKEN)
//database
const db = require('./config/connection')
const collection = require('./config/collection')
const saver = require('./database/fstorebot')
//DATABASE CONNECTION
db.connect((err) => {
if (err) { console.log('Error Connection DB' + err); } else { console.log('DB Connected'); }
})
//BOT
bot.start(async(ctx) => {
msg = ctx.message.text
let msgArray = msg.split(' ')
console.log(msgArray.length);
let length = msgArray.length
msgArray.shift()
let query = msgArray.join(' ')
user = {
first_name: ctx.from.first_name,
userId: ctx.from.id
}
//welcoming message on /start and if there is a query available we can send files
if (length == 1) {
ctx.reply(`<b> I will store files for you and give sharable links ⚡ \n\nI can also make the files available for all users.\n\n 〽️ Powered by@GroupDcBots</b>`, {
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[{ text: 'Search 🔎', switch_inline_query: '' }, { text: 'Link', callback_data: 'POP' }]
]
}
})
} else {
file = await saver.getFile(query).then((res) => {
console.log(res);
if (res.type == 'video') {
ctx.replyWithVideo(res.file_id, {
caption: `<b>${res.caption}</b>`,
parse_mode: 'HTML'
})
} else {
ctx.replyWithDocument(res.file_id, {
caption: `<b>${res.caption}</b>`,
parse_mode: 'HTML'
})
}
})
}
//saving user details to the database
saver.saveUser(user)
})
//DEFINING POP CALLBACK
bot.action('POP', (ctx) => {
ctx.deleteMessage()
ctx.reply('Send me a file 🙂')
})
//help
bot.command('/help', (ctx) => {
ctx.reply(`Hello <b>${ctx.from.first_name}</b> you can send me 📁 files and i will store and share link for that file to be used inside telegram\nYou can also use me for searching files contributed by various users\n\n(<code>Consider this as an initial version after fixing certain bugs we will make this bot opensource</code>)\n\n〽️ Powered by@GroupDcBots`, {
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[{ text: '❗Join Channel❗', url: 't.me/groupdcbots' }]
]
}
})
if (ctx.from.id == process.env.ADMIN) {
ctx.reply('https://telegra.ph/Vijay-02-12')
}
})
//remove files with file_id
bot.command('rem', (ctx) => {
msg = ctx.message.text
let msgArray = msg.split(' ')
msgArray.shift()
let text = msgArray.join(' ')
console.log(text);
if (ctx.from.id == process.env.ADMIN) {
saver.removeFile(text)
ctx.reply('Removed ✅')
}
})
//remove whole collection(remove all files)
bot.command('clear', (ctx) => {
if (ctx.from.id == process.env.ADMIN) {
saver.deleteCollection()
ctx.reply('Removed ✅')
}
})
//removing all files sent by a user
bot.command('remall', (ctx) => {
msg = ctx.message.text
let msgArray = msg.split(' ')
msgArray.shift()
let text = msgArray.join(' ')
console.log(text);
let id = parseInt(text)
if (ctx.from.id == process.env.ADMIN || ctx.from.id == process.env.ADMIN1 || ctx.from.id == process.env.ADMIN2) {
saver.removeUserFile(id)
ctx.reply('Removed ✅')
}
})
//broadcasting message to bot users(from last joined to first)
bot.command('send', async(ctx) => {
msg = ctx.message.text
let msgArray = msg.split(' ')
msgArray.shift()
let text = msgArray.join(' ')
userDetails = await saver.getUser().then((res) => {
n = res.length
userId = []
for (i = n - 1; i >= 0; i--) {
userId.push(res[i].userId)
}
//broadcasting
totalBroadCast = 0
totalFail = []
//creating function for broadcasting and to know bot user status
async function broadcast(text) {
for (const users of userId) {
try {
await bot.telegram.sendMessage(users, String(text))
} catch (err) {
saver.updateUser(users)
totalFail.push(users)
}
}
ctx.reply(`<b>✅ Total active users :</b>${userId.length - totalFail.length}\n❌<b>Total failed broadcast:</b>${totalFail.length}`, {
parse_mode: 'HTML'
})
}
if (ctx.from.id == process.env.ADMIN) {
broadcast(text)
ctx.reply('Broadcasting started -(Message is broadcasted from last joined to first)')
} else {
ctx.replyWithAnimation('https://giphy.com/gifs/dog-working-typing-jkSvCVEXWlOla')
}
})
})
//ban user with user id
bot.command('ban', (ctx) => {
msg = ctx.message.text
let msgArray = msg.split(' ')
msgArray.shift()
let text = msgArray.join(' ')
console.log(text)
userId = {
id: text
}
if (ctx.from.id == process.env.ADMIN) {
saver.banUser(userId).then((res) => {
ctx.reply('banned')
})
}
})
//unban user with user id
bot.command('unban', (ctx) => {
msg = ctx.message.text
let msgArray = msg.split(' ')
msgArray.shift()
let text = msgArray.join(' ')
console.log(text)
userId = {
id: text
}
if (ctx.from.id == process.env.ADMIN) {
saver.unBan(userId).then((res) => {
ctx.reply('✅Done')
})
}
})
//saving documents to db and generating link
bot.on('document', async(ctx) => {
document = ctx.message.document
console.log(ctx);
fileDetails = {
file_name: document.file_name,
userId: ctx.from.id,
file_id: document.file_id,
caption: ctx.message.caption,
file_size: document.file_size,
uniqueId: document.file_unique_id
}
await saver.checkBan(`${ctx.from.id}`).then((res) => {
console.log(res);
if (res == true) {
ctx.reply('⚠ YOU ARE BANNED FOR MISUSING BOT, CONTACT ADMIN')
} else {
saver.saveFile(fileDetails)
ctx.reply(`https://t.me/${process.env.BOTUSERNAME}?start=${document.file_unique_id}`)
ctx.replyWithDocument(document.file_id, {
chat_id: process.env.LOG_CHANNEL,
caption: `${ctx.message.caption}\n\n\nfrom:${ctx.from.id}\nfirstName:${ctx.from.first_name}\nfile_id:${document.file_id}`
})
}
})
})
//video files
bot.on('video', async(ctx) => {
video = ctx.message.video
console.log(ctx);
fileDetails = {
file_name: video.file_name,
userId: ctx.from.id,
file_id: video.file_id,
caption: ctx.message.caption,
file_size: video.file_size,
uniqueId: video.file_unique_id,
type: 'video'
}
console.log(fileDetails.caption);
await saver.checkBan(`${ctx.from.id}`).then((res) => {
console.log(res);
if (res == true) {
ctx.reply('⚠YOU ARE BANNED FOR MISUSING BOT, CONTACT ADMIN')
} else {
saver.saveFile(fileDetails)
ctx.reply(`https://t.me/${process.env.BOTUSERNAME}?start=${video.file_unique_id}`)
ctx.replyWithVideo(video.file_id, {
chat_id: process.env.LOG_CHANNEL,
caption: `${ctx.message.caption}\n\n\nfrom:${ctx.from.id}\nfirstName:${ctx.from.first_name}\nfile_id:${document.file_id}`
})
}
})
})
//audio files
bot.on('audio', async(ctx) => {
audio = ctx.message.audio
console.log(ctx);
fileDetails = {
file_name: audio.file_name,
userId: ctx.from.id,
file_id: audio.file_id,
caption: ctx.message.caption,
file_size: audio.file_size,
uniqueId: audio.file_unique_id,
type: 'audio'
}
console.log(fileDetails.caption);
await saver.checkBan(`${ctx.from.id}`).then((res) => {
console.log(res);
if (res == true) {
ctx.reply('⚠YOU ARE BANNED FOR MISUSING BOT, CONTACT ADMIN')
} else {
saver.saveFile(fileDetails)
ctx.reply(`https://t.me/${process.env.BOTUSERNAME}?start=${audio.file_unique_id}`)
ctx.replyWithDocument(audio.file_id, {
chat_id: process.env.LOG_CHANNEL,
caption: `${ctx.message.caption}\n\n\nfrom:${ctx.from.id}\nfirstName:${ctx.from.first_name}\nfile_id:${document.file_id}`
})
}
})
})
//checking bot status only for admins
bot.command('stats', async(ctx) => {
stats = await saver.getUser().then((res) => {
if (ctx.from.id == process.env.ADMIN) {
ctx.reply(`📊Total user: <b> ${res.length}</b>`, { parse_mode: 'HTML' })
}
})
})
//getting files as inline result
bot.on('inline_query', async(ctx) => {
query = ctx.inlineQuery.query
if (query.length > 0) {
let searchResult = saver.getfileInline(query).then((res) => {
let result = res.map((item, index) => {
return {
type: 'document',
id: item._id,
title: item.file_name,
document_file_id: item.file_id,
caption: item.caption,
reply_markup: {
inline_keyboard: [
[{ text: "Search again 💫", switch_inline_query: '' }]
]
}
}
})
ctx.answerInlineQuery(result)
})
} else {
console.log('query not found');
}
})
//heroku config
domain = `${process.env.DOMAIN}.herokuapp.com`
bot.launch({
webhook: {
domain: domain,
port: Number(process.env.PORT)
}
})