This repository was archived by the owner on Jan 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexBot.js
More file actions
81 lines (68 loc) · 3.42 KB
/
indexBot.js
File metadata and controls
81 lines (68 loc) · 3.42 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
const Discord = require('discord.js')
const fetch = require('node-fetch')
const prefix = 'https://cdn.discordapp.com/attachments/838682121975234571/'
// Connect to the Database file
const db = require('knex')({
client: 'sqlite3',
connection: {
filename: './data.sqlite',
},
useNullAsDefault: true
})
const config = require('./config.json')
const client = new Discord.Client()
client.on('ready', async () => {
console.log('Logged in as ' + client.user.tag)
})
let tempStorage = {};
client.on('messageUpdate', (oldMessage, newMessage) => {
if (!newMessage.author.bot) return;
if (newMessage.author.id != config.user) return;
if (oldMessage.content === newMessage.content) return;
// Collect Videos
if (newMessage.content.includes(' (100%) ')) {
const videoURL = newMessage.content.split('\n').filter(line => line.includes('.mp4')).pop();
if (!videoURL) return;
const fileName = videoURL.split('/').pop()
const fileEnding = fileName.split('.').pop()
const fileWithoutEnding = fileName.replace('.' + fileEnding, '')
if (!fileWithoutEnding || !tempStorage[fileWithoutEnding]) return
const vidKey = videoURL.replace(prefix, '').split('/')[0];
tempStorage[fileWithoutEnding].vidKey = vidKey;
tempStorage[fileWithoutEnding].name = fileWithoutEnding;
tempStorage[fileWithoutEnding].createdAt = Date.now()
console.log('Vid', fileWithoutEnding, vidKey)
setTimeout(async () => {
const duplicate = await db('data').where('vidKey', vidKey).select('*')
const imgResponse = await fetch(`${prefix}${tempStorage[fileWithoutEnding].imgKey}/${tempStorage[fileWithoutEnding].name}.jpg`, { method: 'HEAD' })
if (imgResponse.status == 403) return delete tempStorage[fileWithoutEnding];
const vidResponse = await fetch(`${prefix}${tempStorage[fileWithoutEnding].vidKey}/${tempStorage[fileWithoutEnding].name}.mp4`, { method: 'HEAD' })
if (vidResponse.status == 403) return delete tempStorage[fileWithoutEnding];
console.log('data', tempStorage[fileWithoutEnding])
if (duplicate.length == 0) await db('data').insert(tempStorage[fileWithoutEnding])
delete tempStorage[fileWithoutEnding];
// Garbage collection
for (key in tempStorage) {
if (!tempStorage[key].createdAt || Date.now() - tempStorage[key].createdAt > 3600000) {
delete tempStorage[key]
console.log('garbage', key)
}
}
// Give it 10 seconds to post the final picture
}, 10000)
// Collect Images
} else if (newMessage.content.includes('.jpg')) {
const fileUrl = newMessage.content.split('\n').filter(line => line.includes('.jpg')).pop();
if (!fileUrl) return;
const fileName = fileUrl.split('/').pop();
const fileEnding = fileName.split('.').pop()
const fileWithoutEnding = fileName.replace('.' + fileEnding, '')
if (!fileWithoutEnding) return
const imgKey = fileUrl.replace(prefix, '').split('/')[0];
if (!tempStorage[fileWithoutEnding]) tempStorage[fileWithoutEnding] = {}
tempStorage[fileWithoutEnding].imgKey = imgKey;
tempStorage[fileWithoutEnding].createdAt = Date.now()
// console.log('Img', fileWithoutEnding, imgKey)
}
})
client.login(config.token).catch(console.error)