-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.js
271 lines (256 loc) · 11.8 KB
/
backup.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
//https://discord.com/api/oauth2/authorize?client_id=872846014837584024&permissions=122726720641&scope=bot
const { Client, IntentsBitField } = require('discord.js')
const fs = require('fs')
const sqlite3 = require('sqlite3')
// https://discord.com/developers/docs/topics/gateway
const IBF = new IntentsBitField()
IBF.add(IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.GuildWebhooks, IntentsBitField.Flags.GuildInvites, IntentsBitField.Flags.GuildMessages)
const client = new Client({intents: IBF, partials: ['MESSAGE']})
const {token} = require('./config.json')
client.login(token)
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
client.on('ready', async () => {
console.log(client.user.username)
console.log('Fetching guild')
const gl = await client.guilds.resolve('845287136264454184')
//client.user.setPresence({ activities: [{ name: `Saving ${gl.name}` }], status: 'idle' })
client.user.setPresence({ activities: [{ name: `eGtzWjU4akhid0E=` }], status: 'idle' })
const glpath = __dirname + '/backups/' + gl.name
if (!fs.existsSync(glpath)){fs.mkdirSync(glpath)}
const path = __dirname + '/backups/' + gl.name + '/channels'
if (!fs.existsSync(path)){fs.mkdirSync(path)}
// DB MOMENT ---------------------------------------------------------
const db = new sqlite3.Database(`${path}/messages.db`)
db.run("drop table if exists messages") // Deletes itself if already exists
//db.configure('busyTimeout',0)
console.log('Fetching roles')
var roles = []
const ros = await gl.roles.fetch()
ros.each(function(v,k) {
roles.push({
"color": v.hexColor,
"name": v.name,
"position": v.position,
"id": v.id,
"createdTimestamp": v.createdTimestamp,
"botowner": v.botId,
"mentionable": v.mentionable,
"managed": v.managed,
"hoist": v.hoist,
"permissions": v.permissions.bitfield + "",
})
})
await fs.writeFileSync(glpath + `/roles.json`, JSON.stringify(roles, null, 1), {encoding: "utf8"})
console.log('Fetching channels')
var allchannels = (await gl.channels.fetch()).filter(ch => ch.isTextBased())
var threads = []
var channels = []
var b = Date.now()
function doshit(th) {
const thr = th.threads
if (thr.size > 0) {
thr.each(v1 => {
channels.push(v1)
console.log(`Got ${v1.name}`)
})
}
b = Date.now()
}
await allchannels.each(v => {
//console.log(v)
channels.push(v)
if (!v.isVoiceBased()) {
v.threads.fetchActive().then(doshit)
v.threads.fetchArchived().then(doshit)
}
b = Date.now()
})
while (Date.now() - b < 10000) {await sleep(50)} //Just wait
//await sleep(10000)
var a = 0
console.log('Processing...')
let everything = {}
db.serialize(() => {
db.run(`CREATE TABLE messages (
id INT NOT NULL,
channelId INT,
editedTimestamp INT,
createdTimestamp INT NOT NULL,
type TEXT,
embeds TEXT,
authorId INT,
content TEXT,
attachments TEXT,
reactions TEXT,
PRIMARY KEY (id)
);`)
let bunchsize = 5000
let queue = []
async function save(list, forced) {
if (list) {
queue.push(list)
}
if (forced || queue.length > bunchsize) {
db.run("begin transaction")
/*
let stmt = db.prepare("INSERT or IGNORE INTO messages VALUES (?,?,?,?,?,?,?,?,?,?)");
for (var l of queue) stmt.run(...l)
stmt.finalize()
*/
for (var l of queue) db.run('INSERT or IGNORE INTO messages VALUES (?,?,?,?,?,?,?,?,?,?)', ...l)
db.run("commit transaction")
queue = []
}
// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
/*
if (currentbunch > bunchsize || !stmt || force) {
if (stmt) {
stmt.finalize()
}
if (transactionactive) {
//db.run("commit")
transactionactive = false
db.run("commit transaction;")
}
if (!force) {
if (!transactionactive) {
db.run("begin transaction;")
transactionactive = true
}
stmt = db.prepare("INSERT or IGNORE INTO messages VALUES (?,?,?,?,?,?,?,?,?,?)");
}
currentbunch = 0
}
if (list) {
stmt.run(...list)
currentbunch += 1
}
*/
}
for (c in channels) {
(async () => {
//console.log(c)
const ch = channels[c]
if (ch.isTextBased()) {
var permissions = {}
if (ch.permissionOverwrites) ch.permissionOverwrites.cache.each(function(v,k) {
permissions[v.id] = {
"id": v.id,
"type": v.role,
"deny": "" + v.deny.bitfield,
"allow:": "" + v.allow.bitfield,
}
})
everything[ch.id] = {
"name": ch.name,
"id": ch.id,
"guildid": gl.id,
"nsfw": ch.nsfw,
"created": ch.createdTimestamp,
"permissions": permissions,
'messages': 0,
"threadparent": (ch.isThread() ? ch.parentId : null),
'lastmsg': 0,
'lasttime': 0
}
a += 1
let nmsg = ch.messages
console.log(`(${a}/${channels.length}) Started ${ch.name}`)
await (async (msg)=>{
while (true) {
const ch = msg.channel
let messages
if (!everything[msg.channel.id]['lastmsg'])
messages = await msg.fetch({limit: 50})
else
messages = await msg.fetch({limit: 50, before: everything[msg.channel.id]['lastmsg']})
console.log(ch.name, everything[ch.id]['messages'])
if (ch.name == 'countdown') console.log(everything[ch.id]['lastmsg'])
if (messages.size == 0) break
//console.log(messages)
//await sleep(10)
//console.log(messages.size, msg.channel.id,)
for (m of messages.sort((msgA, msgB) => msgB.createdTimestamp - msgA.createdTimestamp).values()) {
const chid = m.channelId
/*
let actionrow = []
m.components.forEach(function(item, index, array) {
actionrow.push(item.toJSON())
})
*/
let attachments = []
m.attachments.each(i => {
attachments.push(i.url)
})
let embeds = []
m.embeds.forEach(function(item, index, array) {
embeds.push(item.toJSON())
})
let reactions = []
if (m.reactions.cache.first()) {
for (i in m.reactions.cache) {
let reacted = []
let us = await i.users.fetch()
us.each(u => {
reacted.push(`${u.id}`)
})
reactions.push({
"emoji": i.emoji.toString(),
"count": i.count,
"users": reacted
})
await sleep(10)
}
}
/*
let msgdata = {
"id": m.id,
"channelid": m.channelId,
"edit": m.editedTimestamp,
"time": m.createdTimestamp,
"type": m.type,
"embeds": embeds,
"author": m.author.id,
"content": m.content,
"components": actionrow,
"attachments": attachments,
"reactions": reactions
}
*/
//db.run(`INSERT or IGNORE INTO messages VALUES (?,?,?,?,?,?,?,?,?,?);`,m.id,m.channelId,m.editedTimestamp,m.createdTimestamp,m.type,JSON.stringify(m.embeds),m.author.id,m.content,JSON.stringify(attachments),JSON.stringify(reactions))
await save([m.id,m.channelId,m.editedTimestamp,m.createdTimestamp,m.type,JSON.stringify(m.embeds),m.author.id,m.content,JSON.stringify(attachments),JSON.stringify(reactions)], false)
everything[chid]['messages'] += 1
if (!everything[chid]['lastmsg'] || m.createdTimestamp < everything[chid]['lasttime']) {
everything[chid]['lastmsg'] = m.id
everything[chid]['lasttime'] = m.createdTimestamp
}
await sleep(10)
}
if (messages.size < 50) break
}
})(nmsg)
a -= 1
console.log(`(${a}/${channels.length}) Finished ${ch.name} (${everything[ch.id]['messages']})`)
if (a == 0) {
console.log('AAAAAAAAAAAAAAAAAAAAAAAAA')
await save(null, true)
for (ch11 in everything) {
if (everything.hasOwnProperty(ch11)) {
const ch1 = everything[ch11]
await fs.writeFileSync(path + `/${ch1.name.replaceAll('|', '').replaceAll('\\', '').replaceAll('//', '').replaceAll('*', '').replaceAll('?', '').replaceAll('<', '').replaceAll('>', '').replaceAll(':', '')}-${ch1.id}.json`, JSON.stringify(ch1, null, 1), {encoding: "utf8"})
}
}
//db.close();
}
}
})()
}
})
await sleep(1000)
while (a > 0) await sleep(50) // just wait
client.destroy()
})