Boto/node/rng.js

45 lines
1.3 KiB
JavaScript

export function rnguser() {
// RNG User
const rp = require('request-promise'); // For RNG
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)];
});
}
}