-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (135 loc) · 5.17 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
require('dotenv').config();
const request = require('request');
const Discord = require('discord.io');
const spoontacularUrl = `https://api.spoonacular.com/recipes/random?number=1&apiKey=${process.env.SPOONTACULAR_TOKEN}&tag=lunch,dinner`;
const masterChannelID = process.env.MASTER_CHANNEL_ID;
var queue = [];
/*Embed titles are limited to 256 characters
Embed descriptions are limited to 2048 characters
There can be up to 25 fields
A field's name is limited to 256 characters and its value to 1024 characters
The footer text is limited to 2048 characters
The author name is limited to 256 characters
In addition, the sum of all characters in an embed structure must not exceed 6000 characters
A bot can have 1 embed per message
A webhook can have 10 embeds per message*/
function chunkString(str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'gsm'));
}
function sendRandomRecipe(channelID) {
request(spoontacularUrl, { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
const recipe = body.recipes[0];
let url = recipe.sourceUrl;
let image = recipe.image;
let ingredients = recipe.extendedIngredients;
let title = recipe.title;
let instructions = recipe.instructions;
let readyInMinutes = recipe.readyInMinutes;
let servings = recipe.servings;
let fields = [];
if (ingredients) {
let ingredientStringArray = ingredients.map((ingredient) => {
let name = ingredient.originalString;
let amount = ingredient.measures.metric.amount;
let unit = ingredient.measures.metric.unitLong;
return `${name} (${amount} ${unit})`;
});
fields.push({
name: `Ingrédients [${servings} personnes]`,
value: ingredientStringArray.join('\n')
});
}
if (instructions) {
let compiledInstructions = instructions.replace(/(<ol>)|(<\/ol>)|(<li>)/gm, '').replace(/<\/li>/gm, '\n');
// Fields value cannot exceed 1024 bytes.
let listOfCompiledInstructions = chunkString(compiledInstructions, 1024);
listOfCompiledInstructions.forEach((inst, index) => {
if (index === 0)
fields.push({
name: `Préparation [prêt en ${readyInMinutes} minutes]`,
value: inst
});
else
fields.push({
name: "suite",
value: inst
});
});
}
let embedObject = {
color: Math.floor(Math.random() * 16777215),
title: title,
fields: fields,
thumbnail: {
url: image
},
url: url
};
console.log(embedObject)
queue.push({
to: channelID,
embed: embedObject
});
});
}
// Bot message handling
const bot = new Discord.Client({
token: process.env.DISCORD_TOKEN,
autorun: true
});
bot.on('ready', function () {
console.log(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', async function (user, userID, channelID, message) {
if (user === bot.username) {
return;
}
if (message.substring(0, 1) === '$') {
var args = message.substring(1).split(' ');
var cmd = args[0];
switch (cmd) {
// $menu
case 'menu':
console.log('Sending a menu');
await sendRandomRecipe(channelID);
break;
default:
break;
}
}
if (message.indexOf('ortolan') !== -1) {
console.log(`Sending ortolan message to ${channelID}`);
queue.push({
to: channelID,
message: 'Je lui suce le derrière! https://www.youtube.com/watch?v=SEPMuyGe7dg'
});
}
});
bot.on('disconnect', function (errMsg, code) {
console.log(`Disconnected from Discord. Error Code ${code}. Message ${errMsg}.`);
bot.connect();
});
// Sending queue messages
setInterval(() => {
if (queue.length === 0) { return; }
let messageToSend = queue.shift();
let roughByteSize = JSON.stringify(messageToSend).length;
console.log(`Sending message queue. Queue length: ${queue.length}, Message length: ${roughByteSize}`);
if (roughByteSize >= 6000) { return; }
bot.sendMessage(messageToSend);
}, 1500);
// Sending random recipe each day at 16h30, sending lunch and dinner at 11h30 during weekend
setInterval(async () => {
var date = new Date();
var hours = date.getHours();
if ((date.getDay() === 6 || date.getDay() === 0) && hours === 10 && date.getMinutes() === 30) {
console.log(`It is ${date.toLocaleString()}, sending lunch and dinner recipes.`);
await sendRandomRecipe(masterChannelID);
await sendRandomRecipe(masterChannelID);
} else if (date.getDay() !== 6 && date.getDay() !== 0 && hours === 15 && date.getMinutes() === 30) {
console.log(`It is ${date.toLocaleString()}, sending dinner recipe.`);
await sendRandomRecipe(masterChannelID);
}
}, 60000);