-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
55 lines (42 loc) · 1.67 KB
/
start.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
const conf = require("ocore/conf.js");
const headlessWallet = require('headless-obyte');
const eventBus = require('ocore/event_bus.js');
const lightWallet = require('ocore/light_wallet.js');
const network = require('ocore/network.js');
const discordInstance = require('./discordInstance');
const newVoteHandler = require("./handlers/newVoteHandler");
const commitHandler = require("./handlers/commitHandler");
const ParamsStore = require("./store/ParamsStore");
lightWallet.setLightVendorHost(conf.hub);
eventBus.on('connected', async function (ws) {
console.log('log: connected to hub');
await headlessWallet.waitTillReady();
network.initWitnessesIfNecessary(ws, start);
network.sendJustsaying(ws, 'watch_system_vars');
});
eventBus.on("message_for_light", async (_ws, subject, body) => {
if (subject === "system_var_vote") {
await ParamsStore.updateVotes();
newVoteHandler(body);
} else if (subject === 'system_vars') {
await ParamsStore.updateVotes();
const diff = ParamsStore.diff(body);
if (diff.size > 0) {
console.error('log: diff', diff);
diff.forEach(({ value, previousValue }, key) => {
console.error('log: value changed', key, value, previousValue);
commitHandler({ key, value, previousValue });
});
}
} else {
console.error('log(message_for_light): another message', subject, body);
}
});
async function start() {
console.error('log: notification bot has been started');
if (conf.BOT_TOKEN) {
await discordInstance.login(conf.BOT_TOKEN);
} else {
throw new Error('error: BOT_TOKEN is required');
}
}