From 1f86e386c56f072fae4fc1740e8288001e27143e Mon Sep 17 00:00:00 2001 From: Sarimoko Date: Sat, 12 Feb 2022 22:28:55 +0000 Subject: [PATCH] Update 'node/boto.js' --- node/boto.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/node/boto.js b/node/boto.js index 86785a6..3475897 100644 --- a/node/boto.js +++ b/node/boto.js @@ -20,6 +20,50 @@ const client = new tmi.Client({ 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 client.on('message', (channel, tags, message, self) => { if(self) return; @@ -36,7 +80,20 @@ client.on('message', (channel, tags, message, self) => { const command = args.shift().toLowerCase(); // !COMMAND => command // 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 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.