-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
208 lines (177 loc) · 6.1 KB
/
app.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
const { App } = require('@slack/bolt');
const axios = require('axios');
require('dotenv').config();
const fs = require('fs/promises');
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
socketMode: true,
appToken: process.env.APP_TOKEN,
});
const events = {
halloween: new Date().getMonth() === 9 ? true : false,
christmas: new Date().getMonth() === 11 ? true : false,
};
const quotes = {
halloween: [
'“It’s Halloween, everyone’s entitled to one good scare.“',
'“I’m a mouse, duh”',
'“Oh look, another glorious morning. Makes me sick.”',
'“You can’t kill the boogeyman!”',
'“I’ll stop wearing black when they make a darker colour”',
'“I’m a ghost with the most, babe.”',
'“When there is no room left in hell, the dead will walk the earth.”',
'“It’s a full moon tonight. That’s when all the weirdos are out.”',
'“I am the pumpkin king.”',
'“It’s showtime”',
'“Hi I’m Chucky, wanna play?”',
'“No, please don’t kill me, Mr. Ghostface, I wanna be in the sequel.”',
'“What an excellent day for an exorcism.”',
'“Be afraid… Be very afraid.”',
'“Last night you were unhinged. You were like some desperate, howling demon. You frightened me. Do it again.”',
'“We’ve got to find Jack! There’s only 365 days left until next Halloween.”',
'“Isn´t the view beautiful? It takes my breath away. Well, it would if I had any.”',
'“I see dead people”',
],
christmas: [
'“You have such a pretty face. You should be on a Christmas card.”',
'“The best way to spread Christmas cheer is singing loud for all to hear.”',
'“I am a cotton-headed ninny muggins!”',
'“Did I burn down the joint? I don’t think so. I was making ornaments out of fishhooks.”',
'“Mom, does Santa Claus have to go through customs?”',
'“Keep the change, ya filthy animal.”',
'“You’re what the French call les incompétents.”',
'“I wouldn’t let you sleep in my room if you were growing on my ass!”',
'“Son of a nutcracker!”',
'“He’s an angry elf.”',
'“SANTA! Oh my God! Santa, here?! I know him! I know him!”',
'“Holiday who-be what-ee?”',
'“Am I just eating because I’m bored?”',
],
};
const omegaURL = process.env.OMEGA_URL;
const token = process.env.X_MASTER_KEY;
const chooChooURL = process.env.CHOO_CHOO_URL;
const deployHookUrl = process.env.DEPLOY_HOOK_URL;
const delpoyChannelId = process.env.DEPLOY_CHANNEL_ID;
const axiosHeaders = {
headers: {
'Content-Type': 'application/json',
'X-Master-Key': token,
},
};
const addVisit = async () => {
await getVisits()
.then((visits) => {
const newVisits = JSON.stringify({ counter: visits + 1 });
fs.writeFile('./data.json', newVisits, { flag: 'w' }, (err) => {
if (err) {
console.log(err);
}
});
})
.catch((error) => {
console.log(error);
});
};
const getOvStatus = async () => {
let status = false;
await axios
.get(omegaURL)
.then((response) => {
status = response.data.includes('Omega Verksted er åpent!')
? true
: false;
})
.catch((error) => {
console.log(error);
});
return status;
};
const getVisits = async () => {
let visits = 0;
try {
const data = await fs.readFile('./data.json', 'utf-8');
visits = JSON.parse(data).counter;
} catch (err) {
if (err.code === 'ENOENT') {
// File not found error, create a new file
await fs.writeFile('./data.json', JSON.stringify({ counter: 0 }));
} else {
console.log(err);
}
}
return visits;
};
app.message(async ({ message, say }) => {
if (message.text === 'OV?') {
const ovStatus = await getOvStatus();
ovStatus ? await say('OV!') : await say(':disagreeing_astrid:');
if (events.halloween) {
await say(
`${
quotes.halloween[Math.floor(Math.random() * quotes.halloween.length)]
} 🎃👻`
);
} else if (events.christmas) {
await say(
`${
quotes.christmas[Math.floor(Math.random() * quotes.christmas.length)]
} 🎄🎅`
);
}
await addVisit();
}
});
app.message(async ({ message, say }) => {
if (message.text === 'OV#') {
const visits = await getVisits().catch((error) => console.log(error));
await say(`OV-COUNTER: ${visits}`);
}
});
app.command('/ov-status', async ({ ack, respond }) => {
await ack();
const ovStatus = await getOvStatus();
await respond('OV er...');
setTimeout(async () => await respond('...'), 1000);
setTimeout(
async () =>
ovStatus
? await respond('ÅPENT :high-hk: :catrave:')
: await respond('Stengt :disagreeing_astrid:'),
2000
);
});
app.command('/start-train', async ({ ack, say }) => {
await ack();
await say('<!channel>, OV-toget has started! :ov: :steam_locomotive:');
setTimeout(async () => axios.get(chooChooURL), 180000);
});
app.command('/deploy-website', async ({ ack, say, command }) => {
await ack();
if (command.channel_id === delpoyChannelId) {
await axios.post(deployHookUrl).catch(async (error) => {
console.log(error);
await say('Web deploy failed. Check the logs for more info :(');
return false;
});
await say('A new website deploy has started! :muscle: :gear:');
} else {
await say('You cannot deploy from this channel! :angry-wilhelm:');
}
});
app.command('/help', async ({ ack, respond }) => {
await ack();
await respond(`
Hello :wave:, available commands:
- OV? - Answers you OV! if OV is open, if not you would know that it isn't.
- OV# - Gives you the number of times you and the memebers of Orbit has asked to go to OV.
- /ov-status - Tells you the state of OV.
- /start-train - Starts OV-toget! :ov:
- /deploy-website - Yes, the OV-Bot is used to deploy our website. :rocket:
- /help - Shows available commands :muscle:`);
});
(async () => {
await app.start(process.env.PORT || 3000);
console.log('⚡️ OV-BOT is running!');
})();