-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
63 lines (51 loc) · 2.25 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
const webhook = require("webhook-discord");
const utils = require("./utils.js");
const cron = require('node-cron');
const fs = require('fs');
async function sendMessage(channelConfig, channel, quotes, imgURL, colors){
let tag = imgURL[0];
let sfw = imgURL[1];
let url = imgURL[2];
console.log(sfw);
let quoteArray = (tag in quotes) ? quotes[tag][sfw] : quotes["generic"][sfw];
let tagColor = (tag in colors) ? colors[tag] : colors["default"];
// generate quote
let quoteToSend = quoteArray[Math.floor(Math.random() * quoteArray.length)];
console.log(quoteToSend);
// actual webhook
const Hook = new webhook.Webhook(channelConfig[channel]["endpoint"]);
const msg = new webhook.MessageBuilder()
.setName(channelConfig[channel]["name"])
.setColor(tagColor)
.setTitle(quoteToSend)
.setAuthor("From Waifu.pics", "https://waifu.pics/favicon.png", "https://waifu.pics/")
.setImage(url)
.setDescription("[Star us on Github!](https://github.com/weeb-poly/Waifu-Webhook)");
Hook.send(msg);
}
async function driver(channelConfig, channel, quotes, colors, cronString){
// gets all the images for each channel
let tagArray = channelConfig[channel]["tags"].match(/[^,]+/g);
// downloads all images
cron.schedule(cronString, async () => {
let tag = tagArray[Math.floor(Math.random() * tagArray.length)]; // pick a random tag
let imgURL = await utils.imgGet(tag, channelConfig[channel]["type"]);
sendMessage(channelConfig, channel, quotes, imgURL, colors);
});
}
// this is the main function
(() => {
/* -- Parsing the json config files -- */
// channel-tokens contains the actual urls, names, and images for each webhook
let tokens = JSON.parse(fs.readFileSync("./config/channel-tokens.json"));
// contains quotes to display for each tag
let quotes = JSON.parse(fs.readFileSync("./config/quotes.json"));
// containst the colors for each tag
let colors = JSON.parse(fs.readFileSync("./config/colors.json"));
/* -- -- */
// await sendMessage(channelConfig, quotes, schedule);
Object.keys(tokens).forEach((key) => {
let cronString = utils.cronCalc(tokens[key]["frequency"]);
driver(tokens, key, quotes, colors, cronString);
});
})();