-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (77 loc) · 3.59 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
const express = require('express');
const discord = require('discord.js');
const bodyparser = require('body-parser');
const config = require('./config');
const helpers = require('./helpers');
const { messageReactionAdd } = require('./messageReactionAdd');
const app = express();
app.use(bodyparser.json())
const client = new discord.Client();
app.post('/opsgenie', async (req, res) => {
res.sendStatus(200);
const data = req.body;
const channel = await client.channels.fetch(config.discord.channel);
switch (data.action) {
case "Create":
const createMessages = await helpers.sendOpsgenieEmbed(data, channel);
Object.keys(config.emojis).forEach(async (key) => {
if (key == "priority") return createMessages.react(createMessages.guild.emojis.cache.get(config.emojis.priority.main));
createMessages.react(createMessages.guild.emojis.cache.get(config.emojis[key]));
});
break;
case "Close":
const closeMessages = await helpers.getRelatedMessages(channel, data.alert.alertId);
if (closeMessages.array().length > 0) {
closeMessages.each((message) => message.delete());
}
break;
case "Acknowledge":
const ackMessages = await helpers.getRelatedMessages(channel, data.alert.alertId);
if (ackMessages.array().length > 0) {
ackMessages.each(async (message) => {
let reaction = message.reactions.cache.get(config.emojis.acknowledge);
reaction.remove();
const embed = message.embeds[0];
if (embed.fields.some((field) => field.name == "Acknowledged")) {
embed.fields.forEach((field) => {
if (field.name == "Acknowledged") field.value = "True";
});
} else embed.addField("Acknowledged", "True");
message.edit(embed);
})
}
break;
case "UnAcknowledge":
const unackMessages = await helpers.getRelatedMessages(channel, data.alert.alertId);
if (unackMessages.array().length > 0) {
unackMessages.each((message) => {
let reaction = message.guild.emojis.cache.get(config.emojis.acknowledge);
message.react(reaction);
const embed = message.embeds[0];
if (embed.fields.some((field) => field.name == "Acknowledged")) {
embed.fields.forEach((field) => {
if (field.name == "Acknowledged") field.value = "False";
});
} else embed.addField("Acknowledged", "False");
message.edit(embed);
})
}
break;
case "UpdatePriority":
const priMessages = await helpers.getRelatedMessages(channel, data.alert.alertId);
if (priMessages.array().length > 0) {
priMessages.each((message) => {
const embed = message.embeds[0];
embed.fields.forEach((field) => { if (field.name == "Priority") field.value = data.alert.priority });
message.edit(embed);
});
}
break;
default:
break;
}
});
client.on('messageReactionAdd', messageReactionAdd);
app.listen(config.port, () => console.log("express live"));
client.on('ready', () => console.log("discord live"));
client.login(config.discord.token);