-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
552 lines (519 loc) · 17 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
const { App } = require('@slack/bolt')
const Client = require("@replit/database")
const database = new Client()
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
})
async function main () {
await app.start(process.env.PORT || 3000)
//Slash command to list all projects
app.command('/projects', async ({ command, ack, say }) => {
await ack()
// Gets current projects or sets currentProjects to an empty array if there is nothing
let currentProjects = JSON.parse(await database.get("projects")) || []
let response = ""
// Loops through current projects and formats them into a bulleted list
currentProjects.forEach((project, index) => {
if(index % 2 == 1){
response += `\n:purple_heart: ${project["project"]}`
}
else {
response += `\n:black_heart: ${project["project"]}`
}
})
if (response) {
await say({
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":purple_heart: *Project List* :purple_heart:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `This list has been created by members of this Slack. Each project should take roughly around 1 hour to complete.`
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": response
}
}
]
})
} else {
await say(`Your project list is currently empty!`)
}
})
//Slash command to remove project
//Only available for admins
app.command('/removeproject', async ({ command, ack, say }) => {
await ack()
let currentProjects= JSON.parse(await database.get("projects")) || []
let result = []
let adminBol = false
let userList = await client.users.list()
userList.members.forEach((user) => {
if(user.id == command.user_id && user.is_admin){
adminBol = true
}
})
if(adminBol){
currentProjects.forEach((item) => {
if(item["project"].toLowerCase() != command.text.toLowerCase()) { result.push(item) }
})
await database.set("projects", JSON.stringify(result))
await say(`Removed *${command.text}* from your project list`)
}
else {
await say(`Ask an admin to do this.`)
}
})
//Slash command to add project
app.command('/addproject', async ({ command, ack, say }) => {
await ack()
let currentProjects = JSON.parse(await database.get("projects")) || []
// Checks if the project has already been added
let dup = false
currentProjects.forEach((item) => {
if(command.text.toLowerCase().indexOf(item["project"].toLowerCase()) != -1){
dup = true
}
})
// If the project has not been added already, pushes new project to the list of projects
if(!dup) {
currentProjects.push({
"project": command.text,
"user": command.user_id
})
await database.set("projects", JSON.stringify(currentProjects)) // Set the array in the database
await say({
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Thank you for your project submission!* :memo:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Thank you, <@${command.user_id}>! I've added *${command.text}* to our list of projects.`
}
}
]
})
}
else {
await say("This project has already been added.")
}
})
//Slash command to clear entire project list
//Admins only
app.command('/clearprojects', async ({ command, ack, say, client }) => {
await ack()
//Checks if user is an admin
let adminBol = false
let userList = await client.users.list()
userList.members.forEach((user) => {
if(user.id == command.user_id && user.is_admin){
adminBol = true
}
})
//Only deletes if user is an admin
if(adminBol){
await database.delete("projects")
await say(`Cleared our project list.`)
}
else {
await say(`Ask an admin to do this.`)
}
})
//Slash command to get list of user's completed projects
app.command('/completed', async ({ command, ack, say }) => {
await ack()
let currentProgress = JSON.parse(await database.get(command.user_id)) || []
// Nicely format the array
let response = ""
currentProgress.forEach((project, index) => {
if(index % 2 == 1){
response += `\n:purple_heart: Completed ${project["project"]} on ${project["date"]}`
}
else {
response += `\n:black_heart: Completed *${project["project"]}* on *${project["date"]}*`
}
})
if (response) {
await say({
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Your Completed Project List"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `This is a list of the projects you've completed.`
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": response
}
}
]
})
}
else {
await say(`Your completed project list is currently empty!`)
}
})
//Slash command to add completed project to user's list
app.command('/addcompleted', async ({ command, ack, say }) => {
await ack()
let currentProgress = JSON.parse(await database.get(command.user_id)) || []
// Gets today's date and formats it
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
//Pushes project and date to user's list
currentProgress.push({
"project": command.text,
"date": today
})
await database.set(command.user_id, JSON.stringify(currentProgress)) // Set the array in the database
await say({
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Nice job!* :facepunch:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Great work, <@${command.user_id}>! I've added *${command.text}* to your list of completed projects.`
}
}
]
})
})
//Slash command to clear entire user's completed project list
app.command('/clearcompleted', async ({ ack, body, client }) => {
await ack()
try {
const result = await client.views.open({
// Pass a valid trigger_id within 3 seconds of receiving it
trigger_id: body.trigger_id,
// View payload
view: {
type: 'modal',
"callback_id": "clear-conf",
title: {
type: 'plain_text',
text: 'Confirmation'
},
submit: {
"type": "plain_text",
"text": "Submit"
},
blocks: [
{
"type": "input",
"block_id":"block_1",
"element": {
"type": "static_select",
"action_id": "static_select-action",
"placeholder": {
"type": "plain_text",
"text": "Select an item",
"emoji": true
},
"options": [
{
"text": {
"type": "plain_text",
"text": "Yes",
"emoji": true
},
"value": "value-0"
},
{
"text": {
"type": "plain_text",
"text": "No",
"emoji": true
},
"value": "value-1"
}
],
},
"label": {
"type": "plain_text",
"text": "Are you sure you want to delete your completed list?",
"emoji": true
}
}
]
}
})
}
catch (error) {
console.error(error);
}
})
//Runs when user interacts with modal above
app.view("clear-conf", async ({ body, view, ack, client, payload }) => {
// Acknowledge the action
await ack()
// Receives the user's decision
const val = view['state']['values']['block_1']['static_select-action']['selected_option']['text']['text']
if(val == 'Yes') {
await client.views.open({
trigger_id: body.trigger_id,
"response_action": "push",
"view": {
"type": "modal",
"title": {
"type": "plain_text",
"text": "Confirmed"
},
"blocks": [
{
"type": "image",
"image_url": "https://media.tenor.com/images/ad7f5a2f78bde0c98fc3ec66e678ad33/tenor.gif",
"alt_text": "Cool cat in sunglasses."
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "I cleared your completed projects list."
}
]
}
]
}
})
await database.delete(body['user']['id'])
}
else {
await client.views.open({
trigger_id: body.trigger_id,
"response_action": "push",
"view": {
"type": "modal",
"title": {
"type": "plain_text",
"text": "Confirmed"
},
"blocks": [
{
"type": "image",
"image_url": "https://media.tenor.com/images/22950d8b41732282904e13838f3c252c/tenor.gif",
"alt_text": "Elephant in sneakers"
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "I didn't clear your completed projects list."
}
]
}
]
}
})
}
})
//Slash command to delete a completed project
app.command('/removecompleted', async ({ command, ack, say, client, body }) => {
await ack()
try {
const result = await client.views.open({
// Pass a valid trigger_id within 3 seconds of receiving it
trigger_id: body.trigger_id,
// View payload
view: {
type: 'modal',
"callback_id": "delete-conf",
title: {
type: 'plain_text',
text: 'Confirmation'
},
submit: {
"type": "plain_text",
"text": "Submit"
},
blocks: [
{
"type": "input",
"block_id":"block_1",
"element": {
"type": "static_select",
"action_id": "static_select-action",
"placeholder": {
"type": "plain_text",
"text": "Select an item",
"emoji": true
},
"options": [
{
"text": {
"type": "plain_text",
"text": "Yes",
"emoji": true
},
"value": `${command.text}`
},
{
"text": {
"type": "plain_text",
"text": "No",
"emoji": true
},
"value": "value-1"
}
],
},
"label": {
"type": "plain_text",
"text": `Are you sure you want to delete ${command.text} from your completed list?`,
"emoji": true
}
}
]
}
})
}
catch (error) {
console.error(error);
}
})
//Runs when user interacts with modal above
app.view("delete-conf", async ({ body, view, ack, client, payload }) => {
// Acknowledge the action
await ack()
const val = view['state']['values']['block_1']['static_select-action']['selected_option']['text']['text']
if(val == 'Yes') {
await client.views.open({
trigger_id: body.trigger_id,
"response_action": "push",
"view": {
"type": "modal",
"title": {
"type": "plain_text",
"text": "Confirmed"
},
"blocks": [
{
"type": "image",
"image_url": "https://media.tenor.com/images/7eab24e729e5500886d26a3b69f88d08/tenor.gif",
"alt_text": "Donkey in hammock."
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `I deleted ${view['state']['values']['block_1']['static_select-action']['selected_option']['value']} from your completed projects list.`
}
]
}
]
}
})
//Removes project from list
let currentProjects= JSON.parse(await database.get(body['user']['id'])) || []
let result = []
currentProjects.forEach((item) => {
if(item['project'].toLowerCase() != view['state']['values']['block_1']['static_select-action']['selected_option']['value'].toLowerCase()) {
result.push(item)
}
})
await database.set(body['user']['id'], JSON.stringify(result))
}
else {
await client.views.open({
trigger_id: body.trigger_id,
"response_action": "push",
"view": {
"type": "modal",
"title": {
"type": "plain_text",
"text": "Confirmed"
},
"blocks": [
{
"type": "image",
"image_url": "https://media.tenor.com/images/44b643552319f6f021bb1924496a5aa2/tenor.gif",
"alt_text": "Dog dancing in time warp."
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `I didn't delete ${view['state']['values']['block_1']['static_select-action']['selected_option']['value']} from your completed projects list.`
}
]
}
]
}
})
}
})
//Slash command to choose a random project from the list
app.command('/projectroulette', async ({ command, ack, say }) => {
await ack()
let currentProjects = JSON.parse(await database.get("projects")) || []
let roulette = Math.floor((Math.random() * currentProjects.length))
await say(`Rolling...`)
await sleep(2000)
await say(`Rolling...`)
await sleep(2000);
await say(`Rolling...`)
await sleep(2000);
await say(`You rolled *${currentProjects[roulette]["project"]}*. Now go do it!`)
})
console.log('⚡️ Server ready')
}
//Function that delays the message time
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
main()