Update 'node/boto.js'

This commit is contained in:
Sarimoko 2022-02-12 22:28:55 +00:00
parent a39fc63c56
commit 1f86e386c5
1 changed files with 58 additions and 1 deletions

View File

@ -20,6 +20,50 @@ const client = new tmi.Client({
client.connect(); client.connect();
const rp = require('request-promise');
function getChatters(channelName, _attemptCount = 0) {
return rp({
uri: `https://tmi.twitch.tv/group/user/${channelName}/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)];
});
}
// SCAN MSG | START // SCAN MSG | START
client.on('message', (channel, tags, message, self) => { client.on('message', (channel, tags, message, self) => {
if(self) return; if(self) return;
@ -36,7 +80,20 @@ client.on('message', (channel, tags, message, self) => {
const command = args.shift().toLowerCase(); // !COMMAND => command const command = args.shift().toLowerCase(); // !COMMAND => command
// CMD | CHAT COMMANDS | Start // CMD | CHAT COMMANDS | Start
if(command === 'randomuser') {
// Get a random user but skip the user requesting a random user
getRandomChatter(chan, { skipList: [ userstate.username ] })
.then(user => {
if(user === null) {
client.send(chan, `${userstate.username}, there was no one to choose.`);
}
else {
let { name, type } = user;
client.send(chan, `${userstate.username}, I chose "${name}" with type ${type}!`);
}
})
.catch(err => console.log('[ERR]', err));
}
// CMD | Color // CMD | Color
if(command === 'color') { if(command === 'color') {
// Change your username color. Color must be in hex (#000000) or one of the following: Blue, BlueViolet, CadetBlue, Chocolate, Coral, DodgerBlue, Firebrick, GoldenRod, Green, HotPink, OrangeRed, Red, SeaGreen, SpringGreen, YellowGreen. // Change your username color. Color must be in hex (#000000) or one of the following: Blue, BlueViolet, CadetBlue, Chocolate, Coral, DodgerBlue, Firebrick, GoldenRod, Green, HotPink, OrangeRed, Red, SeaGreen, SpringGreen, YellowGreen.