-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
386 lines (342 loc) · 11.1 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import fs from 'fs/promises'
import process from 'process'
import got from 'got'
import { CookieJar } from 'tough-cookie'
import { WebhookClient, MessageEmbed } from 'discord.js'
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
require('dotenv').config();
/**
* @classdesc Main class for the bot
*
* @property {object} config - Configuration provided by the user, including secrets
* @property {WebhookClient} webhook - Discord webhook client
* @property {Got} api - Client for Fandom APIs, with stored cookies from [logging in]{@link ReportedPostsBot#fandomLogin}
* @property {Set} cache - Reported posts that have already been sent
* @property {NodeJS.Timeout} interval - [Polling function]{@link ReportedPostsBot#poll} interval
*/
class ReportedPostsBot {
/**
* Initializes the Discord webhook, API client, and cache
*/
constructor() {
this.config = {
devMode: process.env.ENVIRONMENT?.toLowerCase()?.startsWith('dev') || false,
webhook: {
id: process.env.WEBHOOK_ID || null,
token: process.env.WEBHOOK_TOKEN || null
},
fandom: {
wiki: process.env.FANDOM_WIKI || null,
domain: process.env.FANDOM_DOMAIN || 'fandom.com',
username: process.env.FANDOM_USERNAME || null,
password: process.env.FANDOM_PASSWORD || null
},
interval: (process.env.INTERVAL || 30) * 1000
};
if (this.config.devMode && process.env.DEV_WEBHOOK_ID && process.env.DEV_WEBHOOK_TOKEN) {
this.config.webhook = {
id: process.env.DEV_WEBHOOK_ID,
token: process.env.DEV_WEBHOOK_TOKEN
}
}
// Check for missing config
if (Object.values(this.config).flat(1).includes(null)) {
this.finish();
throw console.error('Missing required config variable(s)');
}
// Discord webhook
this.webhook = new WebhookClient({
id: this.config.webhook.id,
token: this.config.webhook.token
});
// API client
this.config.fandom.wikiUrl = this.getWikiUrl(this.config.fandom.wiki, this.config.fandom.domain);
let pkg = require('./package.json');
this.cookieJar = new CookieJar();
this.api = got.extend({
cookieJar: this.cookieJar,
headers: {
'User-Agent': `${pkg.name} v${pkg.version} (${pkg.homepage})`
}
});
// Cache
this.cache = new Set();
if (!this.config.devMode) {
try {
this.cache = new Set(require('./cache.json'));
console.info('Loaded cache');
} catch (err) {
console.info('Didn\'t load cache');
}
}
}
/**
* Logs into Fandom in the API client
* @returns {Promise} - API response or error
*/
async fandomLogin() {
return new Promise(async (resolve, reject) => {
try {
// Initiate login flow
let flow = await this.api.get(`https://services.${this.config.fandom.domain}/kratos-public/self-service/login/api`).json();
// Login
let login = await this.api.post(flow.ui.action, {
form: {
password_identifier: this.config.fandom.username,
password: this.config.fandom.password,
method: 'password'
}
}).json();
if ('session' in login) { // Got a good response
// Test that login worked
let whoami = await this.api.get(`https://services.${this.config.fandom.domain}/whoami`).json();
console.info(`Logged into Fandom as ${login.session.identity.traits.username} (ID ${whoami.userId})`);
resolve(login);
} else {
throw new Error(req.ui.messages);
}
} catch (err) {
console.error('Failed to log in:', err.response ? err.response.body : err);
return await new Promise(resolve => setTimeout(async () => {
resolve();
return await this.fandomLogin();
}, 10000))
}
});
}
/**
* Save this.cache to cache.json
*/
saveCache() {
if (!this.config.devMode) fs.writeFile('cache.json', JSON.stringify(Array.from(this.cache)), () => {});
}
/**
* Utility to get a wiki URL from domain and interwiki
* @param {string} interwiki - Interwiki (subdomain or lang.subdomain)
* @param {string} domain - Root domain of the wiki and services (like fandom.com)
* @returns {string} - Root wiki URL
*/
getWikiUrl(interwiki, domain) {
if (interwiki.includes('.')) {
let [lang, subdomain] = interwiki.split('.');
return `https://${subdomain}.${domain}/${lang}`;
}
return `https://${interwiki}.${domain}`;
}
/**
* Utility to trim a string to a length
* @param {string} text - Input text
* @param {number} length - Maximum length
* @param {string} [elipsis=…] - Text to use an an elipsis if trimmed
* @returns {string} - Trimmed string
*/
trimEllip(text, length, elipsis = '…') {
text = text.trim(); // Remove whitespace padding
return text.length > length ?
text.substring(0, length - elipsis.length) + elipsis
: text;
}
/**
* Utility to kind of convert post ADF into plain text (good enough for a preview)
* @param {string} adf - ADF JSON
* @returns {string} - Plain text
*/
adfToText(adf) {
let plainText = '';
try {
let json = JSON.parse(adf);
for (let paragraph of json.content) {
if (paragraph.type === 'paragraph') {
let paragraphText = '';
for (let content of paragraph.content) {
if (content.text) {
paragraphText += content.text;
}
}
if (paragraphText) plainText += paragraphText + '\n';
}
}
} catch (err) { }
return plainText;
}
/**
* Initiator
*/
async run() {
await this.fandomLogin();
this.poll();
this.interval = setInterval(
this.poll.bind(this),
this.config.interval
);
}
/**
* Polling function
*
* Queries the API for all reported posts and schedules them to be sent if not already sent
*/
async poll() {
try {
let response = await this.api.get(this.config.fandom.wikiUrl + '/wikia.php', {
searchParams: {
controller: 'DiscussionModeration',
method: 'getReportedPosts',
format: 'json',
limit: 100,
t: Date.now()
}
}).json();
let embeds = [],
pageIds = new Set([]),
userIds = new Set([]);
for (let post of response._embedded['doc:posts']) {
if (!this.cache.has(post.id) && !post.isDeleted && !post?._embedded?.thread?.[0]?.isDeleted) {
this.cache.add(post.id);
// @todo support for anons
let data = {
title: post.title,
body: this.adfToText(post.jsonModel) || post.rawContent,
image: post._embedded?.contentImages?.[0]?.url,
timestamp: post.creationDate.epochSecond * 1000,
author: {
name: post.createdBy.name,
id: post.createdBy.id,
avatar: post.createdBy.avatarUrl
},
postId: post.id,
threadId: post.threadId,
containerType: post._embedded.thread?.[0]?.containerType,
containerId: post._embedded.thread?.[0]?.containerId,
containerName: post.forumName,
isReply: post.isReply,
isLocked: post._embedded.thread?.[0]?.isLocked,
poll: post.poll,
quiz: post.quiz
}
if (data.containerType === 'ARTICLE_COMMENT') pageIds.add(data.containerId);
if (data.containerType === 'WALL') {
data.wallOwnerId = response._embedded.wallOwners?.find(wall => wall.wallContainerId === data.containerId).userId;
userIds.add(data.wallOwnerId);
}
embeds.push(data);
}
}
// Load article details
if (pageIds || userIds) this.containerCache = (await this.api.get(this.config.fandom.wikiUrl + '/wikia.php', {
searchParams: {
controller: 'FeedsAndPosts',
method: 'getArticleNamesAndUsernames',
stablePageIds: Array.from(pageIds).join(','),
userIds: Array.from(userIds).join(',')
}
}).json());
// Split embeds into chunks of 10 and send them
if (embeds.length) {
// Show newest posts last
embeds = embeds.reverse();
// Create new arrays of 10 or less items and populate them
[...Array(Math.ceil(embeds.length / 10))].map((_, i) => embeds.slice(i * 10, i * 10 + 10))
// Generate and send embeds for each
.map(list => {
this.webhook.send({ embeds: list.map(data => this.generateEmbed(data)) });
})
};
this.saveCache();
} catch (err) {
if (err.response?.statusCode === 403) this.fandomLogin();
else console.error(err);
}
}
/**
* Generates a Discord embed from collected post data
* @param {object} data - Collected post data
* @returns {MessageEmbed}
*/
generateEmbed(data) {
let embed = new MessageEmbed()
.setColor(0xE1390B)
.setURL(this.getPostUrl(data))
.setAuthor({
name: this.trimEllip(data.author.name, 256),
iconURL: data.author.avatar,
url: `${this.config.fandom.wikiUrl}/wiki/Special:UserProfileActivity/${data.author.name.replaceAll(' ', '_')}`
})
.setTimestamp(data.timestamp);
if (data.title) {
embed.setTitle(this.trimEllip(data.title, 256));
embed.setDescription(this.trimEllip(data.body, 500));
} else if (data.body) {
embed.setTitle(this.trimEllip(data.body, 256));
} else {
embed.setTitle('(untitled)');
}
if (data.poll) {
embed.addField('Poll', this.trimEllip(data.poll.answers.map(a => '• ' + a.text).join('\n'), 1024), true)
}
if (data.image) embed.setImage(data.image);
let footer = '';
if (data.isLocked) footer += '\uD83D\uDD12\uFE0E';
if (data.isReply) footer += '↶';
if (data.poll) footer += '\uD83D\uDCCA\uFE0E';
if (data.quiz) footer += '\uD83D\uDD51\uFE0E';
if (footer.length) footer += ' ';
// @todo add container link? would require switching to fields
switch (data.containerType) {
case 'FORUM':
footer += `Discussions • ${data.containerName}`; break;
case 'ARTICLE_COMMENT':
footer += `Article comment • ${this.containerCache.articleNames[data.containerId].title}`; break;
case 'WALL':
footer += `Message Wall • ${this.containerCache.userIds[data.wallOwnerId].username}`; break;
}
embed.setFooter({ text: footer });
return embed;
}
/**
* Get the URL to a post or it's container
* @param {object} data - Collected post data
* @param {boolean} getContainer - Get the post's container instead of the post itself (category for posts, article for comments, wall for messages)
* @returns {string} - URL to container
*/
getPostUrl(data, getContainer = false) {
let url = new URL(this.config.fandom.wikiUrl + '/');
switch (data.containerType) {
case 'FORUM':
if (getContainer) {
url.pathname += 'f';
url.searchParams.set('catid', data.containerId);
} else {
url.pathname += `f/p/${data.threadId}` + (data.isReply ? `/r/${data.postId}` : '');
}
break;
case 'ARTICLE_COMMENT':
url.pathname += this.containerCache.articleNames[data.containerId].relativeUrl;
url.hash = 'articleComments';
if (!getContainer) {
url.searchParams.set('commentId', data.threadId);
if (data.isReply) url.searchParams.set('replyId', data.postId);
}
break;
case 'WALL':
url.pathname += `wiki/Message_Wall:${this.containerCache.userIds[data.wallOwnerId].username.replaceAll(' ', '_')}`;
if (!getContainer) {
url.searchParams.set('threadId', data.threadId);
if (data.isReply) url.hash = data.postId;
}
break;
}
return url.toString();
}
/**
* Cleans up the interval and client
* @param {string} [reason] - Reason for exiting
*/
finish() {
console.info('Exiting...');
if (this.interval) clearInterval(this.interval);
this.webhook?.destroy();
}
}
const myBot = new ReportedPostsBot();
myBot.run();