From e138f0eecf2f92138d9196e1b47ffd4f14a1f5d7 Mon Sep 17 00:00:00 2001 From: Sarimoko Date: Mon, 14 Feb 2022 15:52:11 -0800 Subject: [PATCH] rng --- node/boto.js | 150 +++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 76 deletions(-) diff --git a/node/boto.js b/node/boto.js index 62c6cd0..a16984c 100644 --- a/node/boto.js +++ b/node/boto.js @@ -17,53 +17,81 @@ const client = new tmi.Client({ channels: [ process.env.TTV_CHANNELS ] }); -// 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)]; - }); - } -} - client.connect(); +// 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)]; + }); +} + +client.on('message', (channel, tags, message, self) => { + if(self) return; + const badges = tags.badges || {}; // Scan Badges + const isBroadcaster = badges.broadcaster; // Define Streamer + const isMod = badges.moderator; // Define Mod + const isModUp = isBroadcaster || isMod; // Permission Merge = Mod+Streamer + const isSub = badges.subscriber || badges.founder; // Define Subs + const botUserState = client.userstate[channel]; // MOD Status Check + const amMod = botUserState !== undefined && botUserState.mod === true; // Define Mod Status + + if(self || !message.startsWith('!')) return; // Command Parser + const args = message.slice(1).split(' '); + const command = args.shift().toLowerCase(); // !COMMAND => command + if(command === 'rnguser') { + // Get a random user but skip the user requesting a random user + getRandomChatter(channel, { skipList: [ tags.username ] }) + .then(user => { + if(user === null) { + client.say(channel, `Sorry ${tags.username}, this is embarrassing... There was no one to choose from! #smallstreamerproblems Bleep-Bloop!`); + } + else { + let { name, type } = user; + client.say(channel, `${tags.username} exluded, at random I choose "${name}" cuz they're such an amazing ${type}!`); + } + }) + .catch(err => console.log('[ERR]', err)); + } + +}); // ALERTS START // ================================ // -------------------------------- @@ -334,34 +362,4 @@ client.on("whisper", (from, userstate, message, self) => { client.say(channel, `/me [+1] Inbox | Received a new SEXT message from: @`+ from +`!`); }); // ALERTS STOP -// ================================ - -client.on('message', (channel, tags, message, self) => { - if(self) return; - const badges = tags.badges || {}; // Scan Badges - const isBroadcaster = badges.broadcaster; // Define Streamer - const isMod = badges.moderator; // Define Mod - const isModUp = isBroadcaster || isMod; // Permission Merge = Mod+Streamer - const isSub = badges.subscriber || badges.founder; // Define Subs - const botUserState = client.userstate[channel]; // MOD Status Check - const amMod = botUserState !== undefined && botUserState.mod === true; // Define Mod Status - - if(self || !message.startsWith('!')) return; // Command Parser - const args = message.slice(1).split(' '); - const command = args.shift().toLowerCase(); // !COMMAND => command - if(command === 'rnguser') { - // Get a random user but skip the user requesting a random user - getRandomChatter(channel, { skipList: [ tags.username ] }) - .then(user => { - if(user === null) { - client.say(channel, `Sorry ${tags.username}, this is embarrassing... There was no one to choose from! #smallstreamerproblems Bleep-Bloop!`); - } - else { - let { name, type } = user; - client.say(channel, `${tags.username} exluded, at random I choose "${name}" cuz they're such an amazing ${type}!`); - } - }) - .catch(err => console.log('[ERR]', err)); - } - -}); \ No newline at end of file +// ================================ \ No newline at end of file