Boto/node/rng.js

45 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2022-02-14 09:46:55 +00:00
export function rnguser() {
2022-02-14 09:41:33 +00:00
// RNG User
2022-02-14 11:39:47 +00:00
const rp = require('request-promise'); // For RNG
2022-02-14 11:29:53 +00:00
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)
2022-02-14 09:41:33 +00:00
.reduce((p, [ type, list ]) => p.concat(list.map(name => {
if(name === channelName) type = 'broadcaster';
return { name, type };
})), []);
2022-02-14 11:29:53 +00:00
})
.catch(err => {
if(_attemptCount < 3) {
return getChatters(channelName, _attemptCount + 1);
}
2022-02-14 09:41:33 +00:00
throw err;
2022-02-14 11:29:53 +00:00
})
}
2022-02-14 09:41:33 +00:00
2022-02-14 11:38:50 +00:00
function getRandomChatter(channelName, opts = {}) {
let {
onlyViewers = false,
noBroadcaster = false,
skipList = []
} = opts;
return getChatters(channelName)
.then(data => {
let chatters = data
2022-02-14 09:41:33 +00:00
.filter(({ name, type }) =>
!(
(onlyViewers && type !== 'viewers') ||
(noBroadcaster && type === 'broadcaster') ||
skipList.includes(name)
)
);
2022-02-14 11:38:50 +00:00
return chatters.length === 0 ?
2022-02-14 09:41:33 +00:00
null :
chatters[Math.floor(Math.random() * chatters.length)];
2022-02-14 11:38:50 +00:00
});
}
}