diff --git a/node/rng.js b/node/rng.js new file mode 100644 index 0000000..b818b2c --- /dev/null +++ b/node/rng.js @@ -0,0 +1,42 @@ +// RNG User +function getChatters(channelName, _attemptCount = 0) { + return rp({ + uri: `https://tmi.twitch.tv/group/user/sarimoko/chatters`, + json: true + }) + .then(data => { + return Object.entries(data.chatters) + .reduce((p, [ type, list ]) => p.concat(list.map(name => { + if(name === channelName) type = 'broadcaster'; + return { name, type }; + })), []); + }) + .catch(err => { + if(_attemptCount < 3) { + return getChatters(channelName, _attemptCount + 1); + } + throw err; + }) +} + +function getRandomChatter(channelName, opts = {}) { + let { + onlyViewers = false, + noBroadcaster = false, + skipList = [] + } = opts; + return getChatters(channelName) + .then(data => { + let chatters = data + .filter(({ name, type }) => + !( + (onlyViewers && type !== 'viewers') || + (noBroadcaster && type === 'broadcaster') || + skipList.includes(name) + ) + ); + return chatters.length === 0 ? + null : + chatters[Math.floor(Math.random() * chatters.length)]; + }); +} // RNG END \ No newline at end of file