From eec98d7e5c585d6e6a1b5b713d42c04876d8ceae Mon Sep 17 00:00:00 2001 From: Manuel Date: Mon, 18 Jan 2021 23:16:34 +0000 Subject: [PATCH] Properly exit program and use hex instead of hsl * Properly exit on Ctrl+C and termination signal * Define default username color and calculate HSL from this hex value --- colorbot.config.js | 5 +++-- index.js | 24 +++++++++++++++++++++--- package.json | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/colorbot.config.js b/colorbot.config.js index 4337d00..e5bc6d2 100644 --- a/colorbot.config.js +++ b/colorbot.config.js @@ -8,8 +8,9 @@ module.exports = { "TWITCH_BOT_KEY": "", "TWITCH_BOT_USERNAME": "", "TWITCH_BOT_COLOR_MODE": "loop", - "TWITCH_BOT_INTERVAL": 10 + "TWITCH_BOT_INTERVAL": 10, + "TWITCH_BOT_DEFAULT_COLOR": "F1C40F" } } ] -} \ No newline at end of file +} diff --git a/index.js b/index.js index 72093ef..4ac12a6 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,8 @@ const chalk = require("chalk"); const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs)); -let hue = 48; -let sat = 89; -let light = 50; +let defaultcolor = process.env.TWITCH_BOT_DEFAULT_COLOR || "F1C40F"; +let [ hue, sat, light ] = convert.rgb.hsl(convert.hex.rgb(defaultcolor)); let connected = false; @@ -88,3 +87,22 @@ const intervalTime = process.env.TWITCH_BOT_INTERVAL * 1000 || 10000; interval = setInterval(function () { updateColor(); }, intervalTime); + + +const sigs = ['SIGINT', 'SIGTERM', 'SIGQUIT']; +sigs.forEach(sig => { + process.on(sig, () => { + console.log(sig + " received"); + clearInterval(interval); + console.log( + `* Updating username color to`, + chalk.hex(`#${defaultcolor}`)(`#${defaultcolor}`) + ); + client.color(`#${defaultcolor}`).then(() => client.disconnect()); + + console.log("Waiting 3 seconds before exiting..."); + sleep(3000).then(() => { + process.exit(0); + }); + }); +}); diff --git a/package.json b/package.json index 4689a30..e311324 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "colorbot-twitch", - "version": "1.0.0", + "version": "1.0.1", "main": "index.js", "author": "Manuel Hüsers", "license": "MIT",