-
Notifications
You must be signed in to change notification settings - Fork 0
/
dadJoke.js
executable file
·42 lines (36 loc) · 1.08 KB
/
dadJoke.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
import axios from 'axios';
import dotenv from 'dotenv';
import cron from 'node-cron';
dotenv.config();
async function getDadJoke() {
try {
const res = await axios.get('https://icanhazdadjoke.com/', {
headers: {
'User-Agent': 'Discord Dad Joke Webhook ([email protected])',
'Accept': 'application/json'
}
});
return res.data.joke;
} catch (err) {
console.error(err);
}
}
async function postDadJokeDiscord() {
const joke = await getDadJoke();
const data = {
content: joke,
username: 'Big Booty Dad',
avatar_url: 'https://cdn4.iconfinder.com/data/icons/avatar-148/512/dad-father-people-avatar-man-512.png'
};
try {
const webhook = process.env.DISCORD_WEBHOOK;
await axios.post(webhook, data);
} catch (err) {
console.error(err);
}
}
cron.schedule('0 7,12,17 * * *', () => {
const delay = (Math.floor(Math.random() * 240)) * 60 * 1000;
console.log(`Sleeping for ${delay}ms`);
setTimeout(postDadJokeDiscord, delay);
});