|
17 | 17 | // Author:
|
18 | 18 | // JamesLefrere
|
19 | 19 |
|
20 |
| -const CronJob = require('cron').CronJob; |
| 20 | +const CronJob = require('cron').CronJob |
21 | 21 |
|
22 |
| -const getBrain = require('./utils/brain'); |
| 22 | +const getBrain = require('./utils/brain') |
23 | 23 |
|
24 |
| -const { |
25 |
| - getOffsetDate, |
26 |
| - parseNaturalDate, |
27 |
| -} = require('./utils/dates'); |
| 24 | +const { getOffsetDate, parseNaturalDate } = require('./utils/dates') |
28 | 25 |
|
29 |
| -const { isPrivateSlackMessage } = require('./utils/channels'); |
| 26 | +const { isPrivateSlackMessage } = require('./utils/channels') |
30 | 27 |
|
31 |
| -const BRAIN_PREFIX = 'countdown'; |
32 |
| -const COUNTDOWNS = 'countdowns'; |
| 28 | +const BRAIN_PREFIX = 'countdown' |
| 29 | +const COUNTDOWNS = 'countdowns' |
33 | 30 |
|
34 |
| -const { |
35 |
| - addToMap, |
36 |
| - getFromMap, |
37 |
| - getMap, |
38 |
| - removeFromMap, |
39 |
| -} = getBrain(BRAIN_PREFIX); |
| 31 | +const { addToMap, getFromMap, getMap, removeFromMap } = getBrain(BRAIN_PREFIX) |
40 | 32 |
|
41 | 33 | const getSass = hours => {
|
42 |
| - if (hours > 24 * 7 * 4) return 'An ocean of time.'; |
43 |
| - if (hours > 24 * 7 * 3.5) return 'Ages, mate.'; |
44 |
| - if (hours > 24 * 7 * 3) return `It's getting closer, but it'll be fine.`; |
45 |
| - if (hours > 24 * 7 * 2.5) return 'Watch out for this one.'; |
46 |
| - if (hours > 24 * 7 * 2) return 'Did you try working faster?'; |
47 |
| - if (hours > 24 * 7 * 1.5) return `That's concerning.`; |
48 |
| - if (hours > 24 * 7) return `That's quite soon if you think about it.`; |
49 |
| - if (hours > 24 * 6) return `Well that doesn't sound right...`; |
50 |
| - if (hours > 24 * 5) return 'A week?! A mere working week?!'; |
51 |
| - if (hours > 24 * 4) return 'Shit, we can do it!'; |
52 |
| - if (hours > 24 * 3) return 'A'.repeat(20); |
53 |
| - if (hours > 24 * 2) return `I'll give you 1 ETH if you finish it today.`; |
54 |
| - if (hours > 24) return `*${'A'.repeat(200)}*`; |
55 |
| - return '*PANIC MODE ENGAGE!!!* gogogogogogogogogogogogogo54321111111glhf'; |
56 |
| -}; |
57 |
| - |
58 |
| -const getKey = title => title.replace(/\s/g, ''); |
| 34 | + if (hours > 24 * 7 * 4) return 'An ocean of time.' |
| 35 | + if (hours > 24 * 7 * 3.5) return 'Ages, mate.' |
| 36 | + if (hours > 24 * 7 * 3) return `It's getting closer, but it'll be fine.` |
| 37 | + if (hours > 24 * 7 * 2.5) return 'Watch out for this one.' |
| 38 | + if (hours > 24 * 7 * 2) return 'Did you try working faster?' |
| 39 | + if (hours > 24 * 7 * 1.5) return `That's concerning.` |
| 40 | + if (hours > 24 * 7) return `That's quite soon if you think about it.` |
| 41 | + if (hours > 24 * 6) return `Well that doesn't sound right...` |
| 42 | + if (hours > 24 * 5) return 'A week?! A mere working week?!' |
| 43 | + if (hours > 24 * 4) return 'Shit, we can do it!' |
| 44 | + if (hours > 24 * 3) return 'A'.repeat(20) |
| 45 | + if (hours > 24 * 2) return `I'll give you 1 ETH if you finish it today.` |
| 46 | + if (hours > 24) return `*${'A'.repeat(200)}*` |
| 47 | + return '*PANIC MODE ENGAGE!!!* gogogogogogogogogogogogogo54321111111glhf' |
| 48 | +} |
| 49 | + |
| 50 | +const getKey = title => title.replace(/\s/g, '') |
59 | 51 |
|
60 | 52 | const processCountdowns = robot => {
|
61 |
| - const { brain } = robot; |
62 |
| - const currentDate = getOffsetDate(-11); |
| 53 | + const { brain } = robot |
| 54 | + const currentDate = getOffsetDate(-11) |
63 | 55 |
|
64 | 56 | Object.entries(getMap(COUNTDOWNS, brain)).forEach(
|
65 | 57 | ([key, { title, dueDate, room }]) => {
|
66 |
| - const diff = new Date(dueDate).getTime() - new Date(currentDate).getTime(); |
| 58 | + const diff = new Date(dueDate).getTime() - new Date(currentDate).getTime() |
67 | 59 |
|
68 | 60 | if (diff < 0) {
|
69 |
| - robot.messageRoom(room, `${title}: due date elapsed!`); |
70 |
| - return removeFromMap(COUNTDOWNS, key, brain); |
| 61 | + robot.messageRoom(room, `${title}: due date elapsed!`) |
| 62 | + return removeFromMap(COUNTDOWNS, key, brain) |
71 | 63 | }
|
72 | 64 |
|
73 |
| - const hours = (diff / (1000 * 60 * 60)).toFixed(2); |
| 65 | + const hours = (diff / (1000 * 60 * 60)).toFixed(2) |
74 | 66 |
|
75 | 67 | robot.messageRoom(
|
76 | 68 | room,
|
77 | 69 | `${title}: ${hours} hours remaining. ${getSass(hours)}`
|
78 |
| - ); |
79 |
| - }); |
80 |
| -}; |
| 70 | + ) |
| 71 | + } |
| 72 | + ) |
| 73 | +} |
81 | 74 |
|
82 | 75 | const setupCronJob = robot => {
|
83 | 76 | const job = new CronJob({
|
84 | 77 | // Every weekday 23:45h
|
85 | 78 | cronTime: '00 45 23 * * *',
|
86 | 79 | onTick: () => {
|
87 |
| - processCountdowns(robot); |
| 80 | + processCountdowns(robot) |
88 | 81 | },
|
89 | 82 | start: false,
|
90 | 83 | // Last time zone of the day (UTC-11)
|
91 | 84 | timeZone: 'Pacific/Niue'
|
92 |
| - }); |
93 |
| - job.start(); |
94 |
| -}; |
| 85 | + }) |
| 86 | + job.start() |
| 87 | +} |
95 | 88 |
|
96 | 89 | module.exports = robot => {
|
97 |
| - const { brain } = robot; |
98 |
| - setupCronJob(robot); |
| 90 | + const { brain } = robot |
| 91 | + setupCronJob(robot) |
99 | 92 |
|
100 | 93 | robot.hear(/^countdown add '(.+)' (.+)$/, res => {
|
101 |
| - const { message: { user, room }, match } = res; |
| 94 | + const { |
| 95 | + message: { user, room }, |
| 96 | + match |
| 97 | + } = res |
102 | 98 |
|
103 | 99 | if (isPrivateSlackMessage(res)) {
|
104 |
| - return res.send('Countdowns can only be added in a channel.'); |
| 100 | + return res.send('Countdowns can only be added in a channel.') |
105 | 101 | }
|
106 | 102 |
|
107 | 103 | if (user.slack.tz_offset == null) {
|
108 | 104 | return res.send('Please set your time zone in slack first')
|
109 | 105 | }
|
110 | 106 |
|
111 |
| - const title = match[1]; |
112 |
| - const dueDate = parseNaturalDate(match[2], user); |
| 107 | + const title = match[1] |
| 108 | + const dueDate = parseNaturalDate(match[2], user) |
113 | 109 |
|
114 |
| - const key = getKey(title); |
115 |
| - const existing = getFromMap(COUNTDOWNS, key, brain); |
| 110 | + const key = getKey(title) |
| 111 | + const existing = getFromMap(COUNTDOWNS, key, brain) |
116 | 112 |
|
117 | 113 | if (existing) {
|
118 |
| - return res.send('Oops, a countdown with that title already exists'); |
| 114 | + return res.send('Oops, a countdown with that title already exists') |
119 | 115 | }
|
120 | 116 |
|
121 |
| - addToMap( |
122 |
| - COUNTDOWNS, |
123 |
| - key, |
124 |
| - { title, dueDate, room, userId: user.id }, |
125 |
| - brain |
126 |
| - ); |
| 117 | + addToMap(COUNTDOWNS, key, { title, dueDate, room, userId: user.id }, brain) |
127 | 118 |
|
128 |
| - return res.send('The countdown begins!'); |
129 |
| - }); |
| 119 | + return res.send('The countdown begins!') |
| 120 | + }) |
130 | 121 |
|
131 | 122 | robot.hear(/^countdown status$/, () => {
|
132 |
| - processCountdowns(robot); |
133 |
| - }); |
| 123 | + processCountdowns(robot) |
| 124 | + }) |
134 | 125 |
|
135 | 126 | robot.hear(/^countdown remove '(.*)'$/, res => {
|
136 |
| - const { message: { user }, match } = res; |
| 127 | + const { |
| 128 | + message: { user }, |
| 129 | + match |
| 130 | + } = res |
137 | 131 |
|
138 |
| - const key = getKey(match[1]); |
139 |
| - const existing = getFromMap(COUNTDOWNS, key, brain); |
| 132 | + const key = getKey(match[1]) |
| 133 | + const existing = getFromMap(COUNTDOWNS, key, brain) |
140 | 134 |
|
141 | 135 | if (!existing) {
|
142 |
| - return res.send(`That countdown doesn't exist`); |
| 136 | + return res.send(`That countdown doesn't exist`) |
143 | 137 | }
|
144 | 138 |
|
145 | 139 | if (user.id !== existing.userId) {
|
146 |
| - return res.send(`Only user ID ${userId} can remove that countdown`); |
| 140 | + return res.send(`Only user ID ${userId} can remove that countdown`) |
147 | 141 | }
|
148 | 142 |
|
149 |
| - removeFromMap(COUNTDOWNS, key, brain); |
| 143 | + removeFromMap(COUNTDOWNS, key, brain) |
150 | 144 |
|
151 |
| - return res.send('Countdown removed'); |
152 |
| - }); |
153 |
| -}; |
| 145 | + return res.send('Countdown removed') |
| 146 | + }) |
| 147 | +} |
0 commit comments