-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
discord.ts
46 lines (38 loc) · 1.27 KB
/
discord.ts
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
import {Link, Store} from '../store/model';
import {MessageBuilder, Webhook} from 'discord-webhook-node';
import {config} from '../config';
import {logger} from '../logger';
const discord = config.notifications.discord;
const hooks = discord.webHookUrl;
const notifyGroup = discord.notifyGroup;
export function sendDiscordMessage(link: Link, store: Store) {
if (discord.webHookUrl.length > 0) {
logger.debug('↗ sending discord message');
(async () => {
try {
const embed = new MessageBuilder();
embed.setTitle('Stock Notification');
if (link.cartUrl)
embed.addField('Add To Cart Link', link.cartUrl, true);
embed.addField('Product Page', link.url, true);
embed.addField('Store', store.name, true);
embed.addField('Brand', link.brand, true);
embed.addField('Series', link.series, true);
embed.addField('Model', link.model, true);
if (notifyGroup) {
embed.setText(notifyGroup.join(' '));
}
embed.setColor(0x76b900);
embed.setTimestamp();
const promises = [];
for (const hook of hooks) {
promises.push(new Webhook(hook).send(embed));
}
await Promise.all(promises);
logger.info('✔ discord message sent');
} catch (error) {
logger.error("✖ couldn't send discord message", error);
}
})();
}
}