-
Notifications
You must be signed in to change notification settings - Fork 3
/
glitterbot.js
134 lines (118 loc) · 3.73 KB
/
glitterbot.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
const axios = require("axios");
const schedule = require("node-schedule");
const program = require("commander");
// Define command line options and documentation.
program
.option(
"-w, --webhook <url>",
"\x1b[1mRequired: The incoming webhook for your Slack team\x1b[0m"
)
.option("-i, --instant", "Run bot once without starting cronjob")
.option(
"-c, --cron [cron]",
"CRON string used for scheduling messages",
"00 09 * * 1-5"
)
.option(
"-s, --source [url]",
"The path to where images.json and all images are hosted",
"https://super.vette.website"
)
.option("-d, --debug", "Log complete axios error messages")
.parse(process.argv);
// If users didn't provide a Slack webhook URL, close the bot.
if (!program.webhook) {
console.log("Please provide a Slack webhook URL");
program.help();
}
// If there is no instant flag, start a cronjob and send a message
// so the user knows it's running.
if (!program.instant) {
console.log("Glitterbot is running");
schedule.scheduleJob(program.cron, sendGlitter);
} else {
sendGlitter();
}
// Returns a random number between 0 and 1
function artificialIntelligence() {
return Math.random();
}
// Choose if a generic image or a day specific image should be sent.
// There's a 25% chance a generic image gets chosen.
function blockChain() {
const d = new Date();
const today = `${d.getDate()}-${d.getMonth() + 1}`;
const holidays = {
"1-1": "new-year",
"14-2": "valentine",
"5-5": "liberation",
"31-10": "halloween",
"25-12": "christmas",
"26-12": "christmas"
};
// Set easter dynamically
holidays[getEaster()] = "easter"
if (today in holidays) {
return holidays[today]
}
if (artificialIntelligence() > 0.75) {
return "generic";
}
const days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
return days[new Date().getDay()];
}
// Fetch the list of all images, and then select a
// random image of the previously chosen image type.
function machineLearning(folder, data) {
const images = data[folder];
const image = images[Math.floor(artificialIntelligence() * images.length)];
return `${program.source}/${image}`;
}
async function sendGlitter() {
try {
// Select a folder with images from the blockchain
// using artificial intelligence.
const folder = blockChain();
// Then, using machine learning, get the url for a
// specific glitterplaatje.
const { data } = await axios.get(`${program.source}/images.json`);
const url = machineLearning(folder, data);
// Sent image url to Slack
postMessage(url);
} catch (error) {
if (program.debug) console.log(error);
console.log(`Error requesting images.json from ${program.source}`);
}
}
// Posts a Slack message to the webhook as Glitterbot.
async function postMessage(text) {
try {
await axios.post(program.webhook, {
icon_emoji: ":sparkles:",
username: "Glitterbot",
text
});
console.log("Sent message: " + text);
} catch (error) {
if (program.debug) console.log(error);
console.log("Error sending message to Slack webhook");
}
}
function getEaster() {
const year = new Date().getYear();
var f = Math.floor,
// Golden Number - 1
G = year % 19,
C = f(year / 100),
// related to Epact
H = (C - f(C / 4) - f((8 * C + 13) / 25) + 19 * G + 15) % 30,
// number of days from 21 March to the Paschal full moon
I = H - f(H / 28) * (1 - f(29 / (H + 1)) * f((21 - G) / 11)),
// weekday for the Paschal full moon
J = (year + f(year / 4) + I + 2 - C + f(C / 4)) % 7,
// number of days from 21 March to the Sunday on or before the Paschal full moon
L = I - J,
month = 3 + f((L + 40) / 44),
day = L + 28 - 31 * f(month / 4);
return `${day}-${month}`;
}