-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_articles.ts
181 lines (160 loc) · 5.39 KB
/
get_articles.ts
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
import { ArticlesResponse } from "./zendesk.ts";
import { ChannelType, GENERAL_CHANNEL_ID, WEBHOOK_URL } from "./index.ts";
export async function get_articles(
channel_type: ChannelType,
articles_url: string,
): Promise<void> {
try {
const articles_response = await fetch(
articles_url,
{
method: "GET",
redirect: "manual",
headers: {
"content-type": "application/json;charset=UTF-8",
},
},
);
const { headers } = articles_response;
const contentType = headers.get("content-type") || "";
if (contentType.includes("application/json")) {
const articles = await articles_response.json() as ArticlesResponse;
for (const article of articles.articles) {
const last_updated = localStorage.getItem(article.id.toString());
if (last_updated) {
if (last_updated != article.updated_at.toString()) {
console.log("Article", article.id, "has been updated");
}
} else {
console.log(
"Found new article",
article.id,
"from",
article.updated_at.toString(),
);
localStorage.setItem(
article.id.toString(),
article.updated_at.toString(),
);
localStorage.setItem(article.id + "-name", article.name);
if (article.name.includes("Java")) {
console.log(
"Skipping article",
article.id,
"it's java edition, eww",
);
continue;
}
const regex = new RegExp(
"Minecraft.* -\\s*([\\.0-9/]*)( \\((.*)\\))?",
"gm",
);
const extracted_name = regex.exec(article.name);
let title = article.name;
let new_thread = true;
if (extracted_name) {
switch (extracted_name.length) {
case 2: {
title = extracted_name[1];
break;
}
case 4: {
if (
extracted_name[3] !== undefined &&
extracted_name[3] != "Bedrock"
) {
title = extracted_name[1] + " - Platforms (*" +
extracted_name[3] + "*)" + " - " + channel_type;
new_thread = false;
} else {
title = extracted_name[1] + " - " + channel_type;
}
break;
}
default: {
console.error(
"Problem with article name",
article.name,
"[" + extracted_name + "] - [" + extracted_name.length + "]" +
" - " + channel_type,
);
break;
}
}
}
let username;
let avatar;
switch (channel_type) {
case ChannelType.PREVIEW: {
avatar =
"https://cdn.discordapp.com/emojis/1019717536805306398.png";
username = "Preview Release";
break;
}
case ChannelType.STABLE: {
avatar =
"https://cdn.discordapp.com/emojis/1019717534976577617.png";
username = "Stable Release";
break;
}
}
if (new_thread) {
const discord_response = await fetch(
WEBHOOK_URL,
{
method: "POST",
// redirect: "manual",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
"username": username,
"avatar_url": avatar,
"thread_name": title,
"content": article.html_url,
}),
},
);
if (discord_response.status != 200) {
console.error("Post wasn't made, trying again next time");
console.debug("Resp: " + await discord_response.json());
localStorage.removeItem(article.id.toString());
}
} else {
const discord_response = await fetch(
WEBHOOK_URL + "?thread_id=" + GENERAL_CHANNEL_ID,
{
method: "POST",
redirect: "manual",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
"username": username,
"avatar_url": avatar,
"content": "**A new hotfix has been released - " + title +
"**\n This will not have a dedicated thread.\n" +
article.html_url,
}),
},
);
if (discord_response.status != 200) {
console.error("Post wasn't made, trying again next time");
console.debug("Resp: " + await discord_response.json());
localStorage.removeItem(article.id.toString());
}
}
}
}
} else {
console.error(await articles_response.text());
return Promise.reject(new Error("Unable to get articles"));
}
} catch (e) {
if (e instanceof TypeError) {
console.error("Unable to make fetch request:", e.message);
} else {
console.error("Unable to make fetch request: ", e);
}
}
}