Add 'node/rng.js'
This commit is contained in:
parent
2f90be78bc
commit
48b71c9aba
|
@ -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
|
Loading…
Reference in New Issue