-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
207 lines (203 loc) · 5.77 KB
/
index.js
File metadata and controls
207 lines (203 loc) · 5.77 KB
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
require("dotenv").config();
const { App } = require("@slack/bolt");
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: false,
appToken: process.env.SLACK_APP_TOKEN,
port: process.env.PORT,
});
app.message(/.*/gim, async ({ message }) => {
if (!["C08MTLF071T", "C08NHQNG2TS"].includes(message.channel)) return;
if (message.thread_ts) return;
await app.client.reactions.add({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
const response = await (
await fetch(
`https://journey.hackclub.com/api/check_user?` +
new URLSearchParams({
slack_id: message.user,
}),
{
headers: {
Authorization: process.env.API_KEY,
},
}
)
).json();
if (!response.exists) {
await app.client.reactions.remove({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "ember-sad",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "exclamation",
timestamp: message.ts,
});
return await app.client.chat.postMessage({
channel: message.channel,
user: message.user,
text: "You don't seem to have an account on journey. Make an account at https://journey.hackclub.com, then repost this update.",
thread_ts: message.ts
});
}
if (!response.has_project) {
await app.client.reactions.remove({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "ember-sad",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "exclamation",
timestamp: message.ts,
});
return await app.client.chat.postMessage({
channel: message.channel,
user: message.user,
text: "You don't seem to have a project on journey. Go make a new one, then repost this update.",
thread_ts: message.ts
});
}
if (!message.text) {
await app.client.reactions.remove({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "ember-sad",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "exclamation",
timestamp: message.ts,
});
return await app.client.chat.postMessage({
channel: message.channel,
user: message.user,
text: "You must have text attached.",
thread_ts: message.ts
});
}
var attachment = undefined;
if (message.files && message.files?.length != 0) {
if (message.files.length > 1) {
await app.client.reactions.remove({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "ember-sad",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "exclamation",
timestamp: message.ts,
});
return await app.client.chat.postMessage({
channel: message.channel,
user: message.user,
text: "You can only have one attachment.",
thread_ts: message.ts
});
}
const fileres = await app.client.files.sharedPublicURL({
file: message.files[0].id,
token: process.env.SLACK_USER_TOKEN,
});
const url = (
await (await fetch(fileres.file?.permalink_public)).text()
).match(/src="([^"]*\.(mp4|jpg|jpeg|png|gif)[^"]*)"/)?.[1];
attachment = url;
}
const response2 = await fetch(`https://journey.hackclub.com/api/updates`, {
headers: {
Authorization: process.env.API_KEY,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
slack_id: message.user,
project_id: response.project.id,
update: {
text: message.text,
attachment,
},
}),
});
try { await response2.json();} catch(e){
await app.client.reactions.remove({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "ember-sad",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "exclamation",
timestamp: message.ts,
});
return await app.client.chat.postMessage({
channel: message.channel,
user: message.user,
text: "Welp, something went wrong while uploading to the Journey API. Try again posting it again in 1 minute, then contact a developer.",
thread_ts: message.ts
});
}
await app.client.reactions.remove({
channel: message.channel,
name: "lollipopload",
timestamp: message.ts,
});
await app.client.reactions.add({
channel: message.channel,
name: "ember-trianglehappy",
timestamp: message.ts,
});
if (attachment)
await app.client.reactions.add({
channel: message.channel,
name: "tw_frame_with_picture",
timestamp: message.ts,
});
for (const [emoji_key, emoji] of Object.entries(require("./emojis"))) {
if (!message.text.toLowerCase().includes(emoji_key)) continue;
await app.client.reactions.add({
channel: message.channel,
name: emoji,
timestamp: message.ts
});
}
await app.client.chat.postMessage({
channel: message.channel,
user: message.user,
text: "Got your update!",
thread_ts: message.ts
});
});
app.start(process.env.PORT);