forked from hackclub/toriel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
361 lines (325 loc) · 9.55 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
require('dotenv').config()
const { inviteUserToChannel } = require('./util/invite-user-to-channel')
const { mirrorMessage } = require('./util/mirror-message')
const { transcript } = require('./util/transcript')
const { upgradeUser } = require('./util/upgrade-user')
const {
postWelcomeCommittee,
} = require('./interactions/post-welcome-committee')
const express = require('express')
const { app, client } = require('./app.js')
const { receiver } = require('./express-receiver')
const { getInvite } = require('./util/get-invite')
const { sleep } = require('./util/sleep')
const { prisma } = require('./db')
receiver.router.use(express.json())
receiver.router.get('/', require('./endpoints/index'))
receiver.router.get('/ping', require('./endpoints/ping'))
receiver.router.get(
'/start-from-clippy',
require('./endpoints/start-from-clippy')
)
receiver.router.get(
'/slack-tutorial/:user',
require('./endpoints/slack-tutorial')
)
receiver.router.post('/slack-invite', require('./endpoints/slack-invite'))
const defaultChannels = [
'lounge',
'scrapbook',
'ship',
'hq',
'neighborhood',
'8-ball',
'code',
'counttoamillion',
'sprig',
'community',
'hack-night',
'welcome',
'hackathons',
'question-of-the-day',
]
const apacChannels = [
'apac-lounge',
'apac-hq',
'apac-community',
'apac-hack-night',
]
const getSuggestion = () => {
const suggestions = [
`hang out in the <#${transcript('channels.lounge')}>`,
`post your ships in <#${transcript('channels.ship')}>`,
`post a project you're currently working on in <#${transcript(
'channels.scrapbook'
)}>`,
`post the next number in <#${transcript('channels.counttoamillion')}>`,
`answer the latest question in <#${transcript(
'channels.question-of-the-day'
)}>`,
`ask 8-ball your fortune for the coming week in <#${transcript(
'channels.8-ball'
)}>`,
`talk to the friendly cow from the club farm in their <#${transcript(
'channels.pasture'
)}>`,
]
return suggestions[Math.floor(Math.random() * suggestions.length)]
}
app.event('message', async (args) => {
// begin the firehose
const { body, client } = args
const { event } = body
const { type, subtype, user, channel, ts, text } = event
if (
text?.toLowerCase()?.includes('toriel') ||
text?.includes(transcript('selfUserID'))
) {
mirrorMessage({
message: text,
user,
channel,
type,
})
}
const protectedChannels = [transcript('channels.cave')]
if (type == 'message' && protectedChannels.includes(channel)) {
console.log(`Attempting to remove ${subtype} message in #cave channel`)
await client.chat
.delete({
token: process.env.SLACK_LEGACY_TOKEN, // sudo
channel,
ts,
})
.catch((e) => {
console.warn(e)
})
}
defaultAdds = defaultChannels.concat(apacChannels) // add all default channels into new array
defaultAddsId = defaultAdds.map((e) => {
return transcript(`channels.${e}`)
}) // map all default channels into ids as channel prop is given as id
if (
subtype === 'channel_join' &&
text === `<@${user}> has joined the channel` &&
defaultAddsId.includes(channel)
) {
console.log('Deleting "user has joined" message')
await client.chat
.delete({
token: process.env.SLACK_LEGACY_TOKEN, // sudo
channel,
ts,
})
.catch((e) => {
console.warn(e)
})
} // delete "user has joined" message if it is sent in one of the default channels that TORIEL adds new members to
})
const addToChannels = async (user, epoch) => {
await upgradeUser(user)
await sleep(1000) // timeout to prevent race-condition during channel invites
const invite = await getInvite({ user })
let channelsToInvite = defaultChannels
if (epoch) {
channelsToInvite.push('epoch')
}
await Promise.all([
Promise.all(
channelsToInvite.map((c) =>
inviteUserToChannel(user, transcript(`channels.${c}`))
)
),
postWelcomeCommittee(user),
])
const suggestion = getSuggestion()
await client.chat.postMessage({
text: transcript(
epoch ? 'house.added-to-channels' : 'house.added-to-channels-epoch',
{ suggestion }
),
blocks: [
transcript('block.text', {
text: transcript(
!epoch ? 'house.added-to-channels' : 'house.added-to-channels-epoch',
{ suggestion }
),
}),
transcript('block.single-button', {
text: !epoch ? 'reroll' : `I've introduced myself...`,
value: 'reroll',
}),
],
channel: user,
})
// TODO weigh by reactions or just do something else entirely
const history = await client.conversations.history({
channel: transcript('channels.ship'),
limit: 10,
})
const message = history.messages[Math.floor(Math.random() * 10)]
const link = (
await client.chat.getPermalink({
channel: transcript('channels.ship'),
message_ts: message.ts,
})
).permalink
}
app.command(/.*?/, async (args) => {
const { ack, payload, respond } = args
const { command, text, user_id, channel_id } = payload
try {
mirrorMessage({
message: `${command} ${text}`,
user: user_id,
channel: channel_id,
type: 'slash-command',
})
await ack()
await respond({
blocks: [
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `${command} ${text}`,
},
],
},
],
})
switch (command) {
case '/toriel-restart':
await require(`./commands/restart`)(args)
break
case '/toriel-call':
await require(`./commands/call`)(args)
break
default:
await require('./commands/not-found')(args)
break
}
} catch (e) {
console.error(e)
}
})
app.action(/.*?/, async (args) => {
const { ack, respond, payload, client, body } = args
const user = body.user.id
mirrorMessage({
message: `_<@${user}> clicked '${payload.text.text}'_`,
user: user,
channel: body.container.channel_id,
type: body.type,
})
await ack()
switch (payload.value) {
case 'cave_start':
const { joinCaveInteraction } = require('./interactions/join-cave')
await joinCaveInteraction({ ...args, payload: { user } })
break
case 'coc_complete':
const slackuser = await client.users.info({ user })
const email = slackuser?.user?.profile?.email
const invite = await prisma.invite.findFirst({
where: { email },
orderBy: { createdAt: 'desc' },
})
if (invite?.welcome_message == "I'm going to Epoch!") {
await prisma.user.update({
where: { user_id: user },
data: { club_leader: false },
})
await addToChannels(user, true)
break
} else {
await client.chat.postMessage({
text: transcript('house.profile'),
blocks: [
transcript('block.text', { text: transcript('house.profile') }),
transcript('block.image', {
url: transcript('house.profile-image'),
altText: transcript('house.profile-alt-text'),
}),
transcript('block.single-button', {
text: "i've filled out my profile",
value: 'profile_complete',
}),
],
channel: user,
})
}
break
case 'profile_complete':
await client.chat.postMessage({
text: transcript('house.checkClubLeader'),
blocks: [
transcript('block.text', { text: transcript('house.club-leader') }),
transcript('block.double-button', [
{ text: 'yes', value: 'club_leader_yes' },
{ text: 'no', value: 'club_leader_no' },
]),
],
channel: user,
})
break
case 'club_leader_yes':
await prisma.user.update({
where: { user_id: user },
data: { club_leader: true },
})
await client.chat.postMessage({
text: transcript('club-leader.text'),
channel: transcript('club-leader.notifiee'),
})
await addToChannels(user)
break
case 'club_leader_no':
await prisma.user.update({
where: { user_id: user },
data: { club_leader: false },
})
await addToChannels(user)
break
case 'reroll':
const suggestion = getSuggestion()
await respond({
replace_original: true,
text: transcript('house.added-to-channels', { suggestion }),
blocks: [
transcript('block.text', {
text: transcript('house.added-to-channels', { suggestion }),
}),
transcript('block.single-button', {
text: 'reroll',
value: 'reroll',
}),
],
unfurl_links: false,
})
break
default:
await respond({
replace_original: false,
text: transcript('errors.not-found'),
})
console.log({ args })
break
}
})
app.start(process.env.PORT || 3000).then(async () => {
console.log(transcript('startupLog'))
const { ensureSlackChannels } = require('./interactions/ensure-channels')
await ensureSlackChannels()
const { cleanupCaveChannel } = require('./interactions/cleanup-cave')
await cleanupCaveChannel()
if (process.env.NODE_ENV === 'production') {
const { startupInteraction } = require('./interactions/startup')
await startupInteraction()
}
/* DEVELOPMENT UTILITIES (uncomment to use) */
const { setupCaveChannel } = require('./setup/cave-channel')
// await setupCaveChannel(app)
})
module.exports = { app }