This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
215 lines (190 loc) · 7.67 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
const MongoClient = require("mongodb").MongoClient;
const config = require("./config.json");
const dbClient = new MongoClient(config.mongo, {useUnifiedTopology: true});
// CONSTANTS
const HEADER_OLDSTYLE = /^# Message archive for \"#(.+)\" \((\d{10,})\) in guild ".+" \((\d{10,})\)\n{1,2}# Format:\n\[date \+ time] Member ID\/Message ID\/Username - Message content\n----------------$/;
const HEADER_NEWSTYLE = /^# Message archive for guild ".+" \((\d{10,})\)\nIncluded channels: .+\n# Format:\n\[date \+ time] Member ID\/Message ID\/Channel\/Username - Message content\n----------------$/;
const MESSAGE_OLDSTYLE = /^\[([0-9/]{10} [0-9:]{8} UTC)\] \((\d{10,})\/(\d{10,})\/(.+)#(.{4})\): (.+)$/;
const MESSAGE_NEWSTYLE = /^\[([0-9/]{10} [0-9:]{8} UTC)\] \((\d{10,})\/(\d{10,})\/#(.+)\/(.+)#(.{4})\): (.+$)/;
(async function() {
// Connect to database and set exposed collections
let collections = {};
try {
await dbClient.connect();
const bowser = dbClient.db('bowser');
const modmail = dbClient.db('modmail');
collections = {
bowserArchive: bowser.collection('archive'),
modmailLogs: modmail.collection('logs')
};
console.log("Connected to database.");
} catch (err) {
console.log("Could not connect to database:", err.stack);
process.exit(1);
}
// Setup archive cursor and test/report on collections
let archiveCursor;
try {
archiveCursor = collections.bowserArchive.find({});
logsCursor = collections.modmailLogs.find({});
archiveCount = await archiveCursor.count();
logsCount = await logsCursor.count();
console.log(`Bowser archive collection reports ${archiveCount} items.`);
console.log(`Modmail logs collection reports ${logsCount} items.`);
} catch (err) {
console.log("Failed to check collections:", err.stack);
process.exit(1);
}
for await(const doc of archiveCursor) {
try {
const id = doc._id;
const body = doc.body;
const lines = body.split('\n');
console.log(`Converting ${id}...`);
// Check if archive already exists
logsCursor = collections.modmailLogs.find({"_id" : id});
logDoc = await logsCursor.next();
if(logDoc) {
console.log(`${id} already exists in the logs collection.`);
continue;
}
// Extract Header
var header = "";
var i;
for(i in lines) {
const line = lines[i];
header += line;
if(line === "----------------") break;
else header += '\n';
};
var messagesStartAt = Number(i) + 1;
// Test Header
const matchOld = header.match(HEADER_OLDSTYLE);
const matchNew = header.match(HEADER_NEWSTYLE);
var style, guildid, channel;
if(matchOld) {
style = "old";
channel = {
name: matchOld[1],
id: matchOld[2],
}
guildid = matchOld[3];
} else if(matchNew) {
channel = {
name: "unknown",
id: "0",
}
style = "new";
guildid = matchNew[1];
}
else throw new Error(`Document ${id || "[Unknown]"} does not have any known header.`);
// We need to do some weird string manip to convert '2012-11-04T14:51:06.157Z' to '2012-11-04 14:51:06.157'
const createdAt = new Date(doc.timestamp * 1000).toISOString().replace(/T/, ' ').replace(/Z/, '');
const closedAt = new Date().toISOString().replace(/T/, ' ').replace(/Z/, '');
// Initialize document w/o messages
const newDoc = {
"_id": id,
"key": id,
"migrated": true,
"open": false,
"created_at": createdAt,
"closed_at": closedAt,
"channel_id": "0",
"guild_id": guildid,
"bot_id": config.botid,
"recipient": {
"id": 0,
"name": "Migrated Archive",
"discriminator": 0,
"avatar_url": config.avatar,
"mod": false
},
"creator": {
"id": "0",
"name": "migrator",
"discriminator": 0,
"avatar_url": "",
"mod": false
},
"closer": {
"id": "0",
"name": "migrator",
"discriminator": 0,
"avatar_url": "",
"mod": false
},
"messages": []
};
// Default message values
var time = new Date(0);
var user = {
id: 0,
name: "Unknown",
discriminator: "0000"
};
var text = "*unknown text*";
var messageid = 0;
// For each actual message
for (i = messagesStartAt; i < lines.length; i++) {
line = lines[i];
// Regex parasing
if(style == 'old') {
const match = line.match(MESSAGE_OLDSTYLE);
if(match) {
time = match[1];
memberid = match[2];
messageid = match[3];
user = {
id: match[2],
name: match[4],
discrim: match[5]
};
text = match[6];
} else { // if not match assume part of last message, but add as new message
text = line;
}
} else if(style == 'new') {
const match = line.match(MESSAGE_NEWSTYLE);
if(match) {
time = match[1];
memberid = match[2];
messageid = match[3];
channel = {
name: match[4],
id: "0"
};
user = {
id: match[2],
name: match[5],
discrim: match[6]
};
text = match[7];
} else text = line;
}
// more timestamp fun
timestamp = new Date(time).toISOString().replace(/T/, ' ').replace(/Z/, '');
msg = {
'timestamp': timestamp,
'message_id': messageid,
'content': text,
'type': 'thread_message',
'author': {
'id': user.id,
'name': user.name,
'discriminator': user.discrim,
'avatar_url': "",
'mod': false
},
'attachments': [],
'channel': channel
};
newDoc.messages.push(msg)
}
await collections.modmailLogs.insertOne(newDoc);
console.log(`Converted ${id}.`);
} catch (err) {
console.log("Failed to convert document:", err.stack);
}
}
dbClient.close();
})();