From ca038a2cf6ad82661b0f7edb43ffc1b0285250ec Mon Sep 17 00:00:00 2001 From: Max Goodhart Date: Thu, 2 Jul 2020 21:41:04 -0700 Subject: [PATCH] Add configurable dwell delay to Twitch bot --- example.config.toml | 1 + src/node/TwitchBot.js | 13 +++++++++---- src/node/index.js | 5 +++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/example.config.toml b/example.config.toml index 26809d6..9aacd12 100644 --- a/example.config.toml +++ b/example.config.toml @@ -46,6 +46,7 @@ json-url = ["https://woke.net/api/streams.json"] [twitch.announce] #template = "SingsMic <%- stream.source %> <%- stream.city && stream.state ? `(${stream.city} ${stream.state})` : '' %> <%- stream.link %>" #interval = 60 + #delay = 30 [cert] # SSL certificates (optional) diff --git a/src/node/TwitchBot.js b/src/node/TwitchBot.js index 39f8299..0e804ef 100644 --- a/src/node/TwitchBot.js +++ b/src/node/TwitchBot.js @@ -20,6 +20,7 @@ export default class TwitchBot extends EventEmitter { this.streams = null this.listeningURL = null + this.dwellTimeout = null this.announceTimeouts = new Map() client.on('ready', () => { @@ -70,10 +71,14 @@ export default class TwitchBot extends EventEmitter { this.onListeningURLChange(listeningURL) } - async onListeningURLChange(listeningURL) { - if (!this.announceTimeouts.has(listeningURL)) { - await this.announce() - } + onListeningURLChange(listeningURL) { + const { announce } = this.config + clearTimeout(this.dwellTimeout) + this.dwellTimeout = setTimeout(() => { + if (!this.announceTimeouts.has(listeningURL)) { + this.announce() + } + }, announce.delay * 1000) } async announce() { diff --git a/src/node/index.js b/src/node/index.js index 0a12c03..1b6bc1f 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -110,6 +110,11 @@ function parseArgs() { number: true, default: 60, }) + .option('twitch.announce.delay', { + describe: 'Time to dwell on a stream before its details are announced', + number: true, + default: 30, + }) .group( [ 'control.username',