This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (91 loc) · 2.89 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
const fetch = require("node-fetch");
const { interval: intervalSeconds, url, token, channels, initialPost, message } = require("./config.json");
const { join } = require("path");
const { promises: {readFile, writeFile } } = require("fs");
const lastNumLoc = join(__dirname, "lastNum");
const Eris = require("eris");
const bot = new Eris(`Bot ${token}`, {
restMode: true
})
let lastNum = initialPost || 0;
// Writes lastDate to file
async function persistLatest () {
try {
await writeFile(lastNumLoc, `${lastNum}`)
} catch (e) {
throw new Error(`Failed to write to ${lastNumLoc}: ${e}`);
}
}
/**
* Gets any new posts that have been made.
*/
async function getNew () {
const res = await fetch(url);
const data = await res.json();
if (data.errors) {
console.log(data.errors);
throw new Error(data.errors[0]);
}
const toReturn = {
title: data.title,
count: data.posts_count,
newPosts: []
}
for (const post of data.post_stream.posts) {
if (post.post_number > lastNum) {
lastNum = post.post_number;
const base = url.split("/t/")[0]
const avatarUrl = `${base}/${post.avatar_template.replace("{size}", "100")}`
toReturn.newPosts.push({
content: post.cooked,
username: post.username,
avatarUrl,
url: `${url.split(".json")[0]}/${post.post_number}`
})
}
}
persistLatest().catch(console.error);
return toReturn;
}
async function notifyPost (name, post) {
for (const channelConfig of channels) {
const channel = channelConfig.id;
const crosspost = channelConfig.crosspost;
try {
const m = await bot.createMessage(channel, { content: `${message}\nBy: ${post.username}.\n\n${post.url}` })
if (m && m.id && crosspost) {
await bot.crosspostMessage(channel, m.id);
}
} catch (e) {
console.error(`Failed to message ${channel}: ${e}`);
}
}
}
async function run() {
const latest = await getNew();
if (latest.newPosts.length !== 0) {
const p = [];
for (let post of latest.newPosts) {
p.push(notifyPost(latest.title, post))
}
await Promise.all(p);
}
}
async function main () {
// Read initial num from file
try {
const res = await readFile(lastNumLoc)
lastNum = parseInt(res, 10);
if (isNaN(lastNum)) {
console.warn("Value from file was NaN!")
lastNum = initialPost || 0
}
} catch (e) {
if (e.code !== "ENOENT") {
throw new Error(`Failed to read ${lastNumLoc}: ${e}`)
}
}
setInterval(run, intervalSeconds * 60000)
return run();
}
main().catch(console.error)