-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
61 lines (54 loc) · 1.39 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
const { secrets } = require("./settings.js");
const Bree = require("bree");
const { ShardingManager } = require("discord.js");
const manager = new ShardingManager("./bot.js", { token: secrets.bot_token });
const debug = require("debug");
const { check } = require("./handlers/depCheck.js");
// first check for dependency issues
try {
check();
} catch (err) {
console.error(err);
console.log(err);
process.exit(1);
}
manager.spawn({ amount: "auto", delay: 1000 }); // spawn auto
manager.on("shardCreate", (shard) => {
console.log(`Spawned shard ${shard.id}`);
});
const logger = {
info: debug("niles:bree:info"),
warn: debug("niles:bree:warn"),
error: console.error
};
/**
* Emit update message
* @param {Client} client
* @param {Object} args - Arguments to pass through
*/
function emitUpdate (client, { guild, channel}) {
client.emit("nilesCalendarUpdate", guild, channel);
}
/**
* handle worker messages
* @param {{channel, guild}} msg
*/
function workerMessageHandler(msg) {
const { guild, channel } = msg.message;
try {
manager.broadcastEval(emitUpdate, { context: { guild, channel }});
} catch (err) {
console.log(err);
if (err.name === "Error [SHARDING_NO_SHARDS]") process.exit();
}
}
const bree = new Bree({
jobs: [{
name: "updater",
interval: secrets.calendar_update_interval,
timeout: "20s"
}],
workerMessageHandler,
logger
});
bree.start();