-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
397 lines (335 loc) · 10.7 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
const Discord = require('discord.js');
const fetch = require('node-fetch');
const client = new Discord.Client();
var configFile = {};
try {
configFile = require('./config.json');
} catch (ex) {
// process.env may be available
}
/// CONSTANTS ///
const events = {
MESSAGE_REACTION_ADD: 'addReaction',
MESSAGE_REACTION_REMOVE: 'removeReaction',
MESSAGE_CREATE: 'handleMessageCreation',
CHANNEL_UPDATE: 'handleChannelUpdate',
};
const emojis = {
thumbsup: "👍",
thumbsdown: "👎",
heart: "❤",
heavy_multiplication_x: "✖",
x: "❌"
};
const urlRegexesEnvironments = {
production: [
/(https?:\/\/www.stomt.com\/.+\/[^\s]+)/g, // Stomt links (e.g. https://www.stomt.com/stomt/teamwork-webhook)
/(https?:\/\/stomt.co\/[^\s]+)/g, // Short Stomt links (e.g. https://stomt.co/gtKug)
],
staging: [
/(https?:\/\/test.stomt.com\/.+\/[^\s]+)/g,
/(https?:\/\/test.stomt.co\/[^\s]+)/g,
],
local: [
/(https?:\/\/stomt.web\/.+\/[^\s]+)/g,
/(https?:\/\/stomt.short\/[^\s]+)/g,
]
};
/// CONFIG ///
const config = {
token: process.env.DISCORD_APP_TOKEN || configFile.discord_app_token,
api_endpoint: process.env.STOMT_API_ENDPOINT || configFile.stomt_api_endpoint,
app_id: process.env.STOMT_APP_ID || configFile.stomt_app_id,
environment: process.env.ENVIRONMENT || configFile.environment,
};
const upvote_emoji = emojis.heart;
const downvote_emoji = emojis.heavy_multiplication_x;
const urlRegexes = urlRegexesEnvironments[config.environment];
const prefix = config.environment === 'production' ? '' : config.environment;
const wish_commands = [prefix + '!iwish', prefix + '.iwish', prefix + 'iwish', prefix + '!wish', prefix + '.wish'];
const like_commands = [prefix + '!ilike', prefix + '.ilike', prefix + 'ilike', prefix + '!like', prefix + '.like'];
/// HELPER FUNCTIONS ///
function getStomtLink(message) {
var texts = [];
if (message && message.embeds && message.embeds.length > 0 && message.embeds[0].url) {
texts.push(message.embeds[0].url);
}
if (message && message.content) {
texts.push(message.content);
}
return extractLink(texts);
}
function extractLink(texts) {
for (var i in texts) {
for (var j in urlRegexes) {
const results = texts[i].match(urlRegexes[j]);
if (results && results.length > 0) {
return results[0];
}
}
}
}
function shouldReactionBeHandeled(reaction, user) {
// Is by bot?
if (user.bot) {
return false;
}
// Is allowed reaction?
if (reaction.emoji.name !== upvote_emoji && reaction.emoji.name !== downvote_emoji) {
return false;
}
return true;
}
function sendApiRequestPost(url, data) {
console.log('POST', url);
const options = {
method: 'post',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'AppId': config.app_id,
},
};
return fetch(url, options)
.then(res => res.json())
.then(json => {
if (!json.data || json.error) {
console.log('Request failed:', json.error);
console.log(' > POST', url);
console.log(' > data:', data);
console.log(' > response:', json);
}
return json;
})
.catch(err => console.error(err));
}
/// EVENTS ///
/**
* Choose which Discord events will be handeled further
* and extend the results.
*/
client.on('raw', async event => {
// reaction events
if (event.t === 'MESSAGE_REACTION_ADD' || event.t === 'MESSAGE_REACTION_REMOVE') {
const { d: data } = event;
const user = client.users.get(data.user_id);
const channel = client.channels.get(data.channel_id) || await user.createDM();
const message = await channel.fetchMessage(data.message_id);
const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
let reaction = message.reactions.get(emojiKey);
if (!reaction) {
const emoji = new Discord.Emoji(client.guilds.get(data.guild_id), data.emoji);
reaction = new Discord.MessageReaction(message, emoji, 1, data.user_id === client.user.id);
}
client.emit(events[event.t], reaction, user);
}
// message events
if (event.t === 'MESSAGE_CREATE') {
const { d: data } = event;
const user = client.users.get(data.author.id);
const channel = client.channels.get(data.channel_id) || await user.createDM();
if (channel.messages.has(data.id)) return;
const message = await channel.fetchMessage(data.id);
client.emit(events[event.t], message, user);
if (!message.author.bot) {
client.emit('messageCommand', message, user);
}
}
// connected to new guild
if (event.t === 'GUILD_CREATE') {
const { d: data } = event;
const guild = await client.guilds.get(data.id);
if (!guild) {
console.log('Can\'t load guild:', data);
return;
}
console.log('CONNECTED TO GUILD');
console.log(' > name: ', guild.name);
console.log(' > id: ', guild.id);
console.log(' > members:', guild.memberCount);
guild.channels.forEach(channel => {
if (channel.type === 'text') {
client.emit(events['CHANNEL_UPDATE'], channel);
}
});
}
// access to channel changed
if (event.t === 'CHANNEL_UPDATE') {
const { d: data } = event;
const channel = await client.channels.get(data.id);
if (channel.type === 'text') {
client.emit(events['CHANNEL_UPDATE'], channel);
}
}
// ready event
if (event.t === 'READY') {
console.log('Ready!');
}
});
/**
* Handle all added reactions and send valid ones to the
* STOMT servers.
*/
client.on(events.MESSAGE_REACTION_ADD, (reaction, user) => {
const stomtLink = getStomtLink(reaction.message);
if (!stomtLink || !shouldReactionBeHandeled(reaction, user)) {
return;
}
removeOtherReaction(reaction, user);
addReactionOnSTOMT(reaction, user, stomtLink);
});
function removeOtherReaction(reaction, user) {
let votes = null;
if (reaction.emoji.name === upvote_emoji) {
votes = reaction.message.reactions.get(downvote_emoji);
} else {
votes = reaction.message.reactions.get(upvote_emoji);
}
if (!votes) {
return;
}
// We could fetch the users first to check if the current user gave the
// other reaction aswell. But trying to remove it is also only one request.
//const users = await votes.fetchUsers();
//users.has(user.id);
votes.remove(user).catch(err => {
if (err.code === 50013) { // Missing Permissions
console.warn(
'[MANAGE_MESSAGES] No permissions to remove reactions in \n'
+ ' > guild: ' + reaction.message.channel.guild.name + " (" + reaction.message.channel.guild.id + ")\n"
+ ' > channel: ' + reaction.message.channel.name + " (" + reaction.message.channel.id + ")\n"
);
} else {
console.error(err);
}
});
}
function addReactionOnSTOMT(reaction, user, stomtLink) {
const url = config.api_endpoint + '/addVote'
const data = {
message_id: reaction.message.id,
channel_id: reaction.message.channel.id,
guild_id: reaction.message.channel.guild.id,
user_id: user.id,
stomt_link: stomtLink,
positive: reaction.emoji.name === upvote_emoji
};
sendApiRequestPost(url, data);
}
/**
* Handle all removed reactions and send valid ones to the
* STOMT servers.
*/
client.on(events.MESSAGE_REACTION_REMOVE, (reaction, user) => {
const stomtLink = getStomtLink(reaction.message);
if (!stomtLink || !shouldReactionBeHandeled(reaction, user)) {
return;
}
removeReactionOnSTOMT(reaction, user, stomtLink);
});
function removeReactionOnSTOMT(reaction, user, stomtLink) {
const url = config.api_endpoint + '/removeVote'
const data = {
message_id: reaction.message.id,
channel_id: reaction.message.channel.id,
guild_id: reaction.message.channel.guild.id,
user_id: user.id,
stomt_link: stomtLink,
positive: reaction.emoji.name === upvote_emoji
};
sendApiRequestPost(url, data);
}
/**
* Let bot add reactions on every posted Stomt link, so users
* just have to click on the reaction.
*/
client.on(events.MESSAGE_CREATE, async (message, user) => {
const stomtLink = getStomtLink(message);
if (!stomtLink) {
return;
}
// validate link
const url = config.api_endpoint + '/validateLink'
const data = {
stomt_link: stomtLink
};
const resonse = await sendApiRequestPost(url, data);
if (resonse.error) {
return; // No Stomt found for this link
}
message
.react(upvote_emoji)
.then(() => message.react(downvote_emoji));
});
/**
* Allow to create a Stomt from discord.
*/
client.on('messageCommand', async (message, user) => {
const args = message.content.split(/ +/);
const command = args.shift().toLowerCase();
// check command
if (!wish_commands.includes(command) && !like_commands.includes(command)) return;
const positive = like_commands.includes(command);
// check args
if (args.length < 2) {
if (positive) {
return message.channel.send(`Please specify who you want to address and write your like \`Ilike stomt because of this!\`, ${message.author}!`);
} else {
return message.channel.send(`Please specify who you want to address and write your wish \`IWish stomt would save this!\`, ${message.author}!`);
}
}
// submit
const target_id = args[0];
const text = args.slice(1).join(' ').substring(0, 120);
const url = config.api_endpoint + '/addStomt'
const data = {
message_id: message.id,
channel_id: message.channel.id,
guild_id: message.channel.guild.id,
user_id: user.id,
target_id: target_id,
text: text,
positive: false
};
const response = await sendApiRequestPost(url, data);
// answer
if (!response || !response.data || response.error) {
if (response.error === 'You already posted this stomt.') {
return message.channel.send(`You already posted this Stomt.`);
} else {
if (positive) {
return message.channel.send(`I was unable to find \`${target_id}\` on STOMT, please specify who you want to address and write your like \`Ilike stomt-discord a lot!!!\`, ${message.author}!`);
} else {
return message.channel.send(`I was unable to find \`${target_id}\` on STOMT, please specify who you want to address and write your wish \`Iwish stomt-discord would post my wish.\`, ${message.author}!`);
}
}
}
const embed = new Discord.RichEmbed(response.data.embed.embeds[0]);
message.channel.send(`Thanks ${message.author}, I saved your Stomt to the feedback directory, lets share and vote it.`, {embed});
});
/**
* Check last messages of channel for Stomt links.
*/
client.on(events.CHANNEL_UPDATE, (channel) => {
channel.fetchMessages({ limit: 20 })
.then(messages => {
messages.forEach(message => {
client.emit(events['MESSAGE_CREATE'], message, message.author);
});
})
.catch(err => {
if (err.code === 50001) {
console.warn(
'[Missing Access] Failed to read messages in \n'
+ ' > guild: ' + channel.guild.name + " (" + channel.guild.id + ")\n"
+ ' > channel: ' + channel.name + " (" + channel.id + ")\n"
);
} else {
console.error('[Missing Access] Failed to read messages', err)
}
});
});
/**
* Connect to all authorized Discord Guilds
*/
client.login(config.token);