diff --git a/beerchat.js b/beerchat.js index 6c9b8b5..06dd7e1 100644 --- a/beerchat.js +++ b/beerchat.js @@ -1,6 +1,7 @@ module.exports = { "remotes": [{ - "debug": true, + "debug": false, + "announce_channel": false, "name": "IRC", "type": "irc", "host": "irc.libera.chat", @@ -11,7 +12,8 @@ module.exports = { "main": "pandorabox-test" } },{ - "debug": true, + "debug": false, + "announce_channel": false, "name": "Discord", "type": "discord", "token": process.env.BEERCHAT_DISCORD_TEST_TOKEN, diff --git a/docker-compose.yml b/docker-compose.yml index 7479713..1c477fe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,4 +6,4 @@ services: volumes: - "./beerchat.js:/data/beerchat.js" ports: - - "8080:8080" \ No newline at end of file + - "8080:8080" diff --git a/src/api/reconnect.js b/src/api/shutdown.js similarity index 57% rename from src/api/reconnect.js rename to src/api/shutdown.js index fa691f9..6bd62ba 100644 --- a/src/api/reconnect.js +++ b/src/api/shutdown.js @@ -2,7 +2,7 @@ const app = require("../app"); const events = require("../events"); // mod -> web -app.post('/reconnect', function(req, res){ - events.emit("reconnect", {}); +app.post('/shutdown', function(req, res){ + events.emit("shutdown", {}); res.end(); }); diff --git a/src/handler/discord.js b/src/handler/discord.js index 2d06579..861343e 100644 --- a/src/handler/discord.js +++ b/src/handler/discord.js @@ -24,7 +24,7 @@ module.exports = class { Object.keys(remote.channels).forEach(ingame_name => { const discord_channel_name = remote.channels[ingame_name]; const channel = this.client.channels.cache.find(ch => ch.name === discord_channel_name); - if (channel) { + if (channel && remote.announce_channel) { channel.send(`beerchat_proxy connected! ingame-channel: ${ingame_name}`); } }); diff --git a/src/handler/irc.js b/src/handler/irc.js index 6cd7872..ab60ecf 100644 --- a/src/handler/irc.js +++ b/src/handler/irc.js @@ -29,11 +29,13 @@ module.exports = class { this.client.on('registered', () => { let delay = 5000; Object.keys(remote.channels).forEach(ingame_name => { - setTimeout(function(){ + setTimeout(() => { const irc_name = remote.channels[ingame_name]; var channel = this.client.channel("#" + irc_name); channel.join(); - channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`); + if (remote.announce_channel) { + channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`); + } channels[ingame_name] = channel; }, delay); delay += 2000; diff --git a/src/index.js b/src/index.js index 86f6614..468e555 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,7 @@ enableWs(app); require("./api/rx"); require("./api/tx"); require("./api/ws"); -require("./api/reconnect"); +require("./api/shutdown"); const cfg = require("./config"); const events = require("./events"); @@ -19,19 +19,26 @@ const handlers = { discord: require("./handler/discord") }; +const instances = []; cfg.remotes.forEach(remote => { const Handler = handlers[remote.type]; const instance = new Handler(); console.log(`Setting up remote: ${remote.name} with type: ${remote.type}`); instance.init(remote, events); -}); - -events.on("reconnect", function(){ - //TODO + instances.push(instance); }); console.log("Starting message router"); router(cfg, events); -app.listen(8080, () => console.log('Listening on http://127.0.0.1:8080')); +const server = app.listen(8080, () => console.log('Listening on http://127.0.0.1:8080')); + +events.on("shutdown", function(){ + // close http server + server.close(); + // close clients + instances.forEach(i => i.destroy()); + // forcibly quit + process.exit(0); +}); \ No newline at end of file