async function getUser(identifier) { const user = await axios(`https://api.ivr.fi/v2/twitch/user?login=${identifier}`) .then(res => res.data[0]) .catch(() => undefined); return user; } const getChannels = async (url, after, destination = []) => { const headers = { Authorization: `Bearer ${Credentials.twitch.bearer}`, 'Client-ID': Credentials.twitch.client_id }; await axios(`${url}${after ? `&after=${after}` : ''}`, { headers }).then(res => res.data).then(data => { data.data.forEach(entry => { destination.push(entry.to_login); }); if (data.pagination?.cursor) { return getChannels(url, data.pagination.cursor, destination); } }); return destination; }; const filterChannels = async (arr, max_follower_count) => { const filtered = await Promise.all(arr.map(async channel => { let channelInfo = await getUser(channel); if (channelInfo.followers <= max_follower_count) { return channelInfo.login; } })).then(res => res.filter(e => e)); return filtered; }; const parseIrc = (msg) => { const args = msg.split(';'); let time = args.find(e => /tmi-sent-ts=\d+/.test(e)); let sender = args.find(e => /display-name=\w+/.test(e)); let content = args.find(e => /PRIVMSG/.test(e))?.replace(/= :/g, ''); if (time && content && sender) { const channel = content.split(' ').find(e => /#\w+/.test(e)); time = new Date(Number(time.slice(time.indexOf('=') + 1))); content = content.slice(content.indexOf(`${channel}`) + channel.length + 1); sender = sender.slice(sender.indexOf('=') + 1); return `[${time.toLocaleString('en-GB', { timeZone: 'UTC' }).replace(', ', ' ')} UTC] ${sender}: ${content.startsWith(':') ? content.slice(1) : content}`; } }; const getParsedMessages = async (channel) => { const rm = await axios(`https://recent-messages.robotty.de/api/v2/recent-messages/${channel}`).then(res => res.data.messages).catch(() => false); return rm?.length && rm.map(e => parseIrc(e)); }; const user = await getUser('drapsnatt'); const nameRegex = new RegExp(String.raw`\b${user.displayName}\b`); const channels = await getChannels(`https://api.twitch.tv/helix/users/follows?first=100&from_id=${user.id}`); const filteredChannels = await filterChannels(channels, 44000); const out = await Promise.all(filteredChannels.map(async channel => { let messages = await getParsedMessages(channel); if (!messages) return; messages = messages.filter?.(e => e).length ? messages.filter(e => nameRegex.test(e)) : null; if (messages) { return { channel, messages }; } })).then(res => res.filter(e => e?.messages && e)); dump(out);