UNPKG

twitchcord-bot

Version:
124 lines (110 loc) 3.54 kB
import chalk from 'chalk' import tmi from 'tmi.js' import Discord from 'discord.js' export default class Bot { constructor (opts) { const { twitch, discord } = opts this.twitch = new tmi.client(twitch) this.discord = new Discord.Client() this.joined = [] // Keep reference to joins this.present = [] // A list of present users // Initalize the clients this.twitch.connect() this.discord.login(discord.token) // Twitch client listeners this.twitch.on('message', (channel, userstate, message, self) => { const username = userstate['display-name'] if (userstate['message-type'] !== 'whisper') { console.log(`${chalk.green(username)}: ${message}`) } if (username !== twitch.identity.username) { this.sendToDiscord({ username, message }) } }) this.twitch.on('join', (channel, username, self) => { this.present = [ ...this.present, username ] console.log(chalk.blue(`Who's here now: \n`), chalk.yellow(this.present.join('\n'))) this.joined.push(username) const message = 'has joined the stream' console.log(`${chalk.green(username)}: ${message}`) if (this.channel) { this.sendToDiscord({ username, message }) } }) this.twitch.on('part', (channel, username, self) => { const message = 'has left the stream' console.log(`${chalk.green(username)}: ${message}`) // Remove users from present list const index = this.present.indexOf(username) this.present.splice(index, 1) console.log(chalk.blue(`Who's here now: `, this.present.join('\n'))) if (this.channel) { this.sendToDiscord({ username, message }) } }) // Discord client listeners this.discord.on('ready', () => { this.channel = this.discord.channels.find('name', discord.channel) }) this.discord.on('message', (message) => { if (message.author.username === twitch.identity.username && message.channel.name === discord.channel) { this.sendToTwitch(message) } }) } /** * Sends a message from twitch irc to discord. * @param {object} the message data * @return {promise} the sent message */ async sendToDiscord ({ username, message }) { let sent try { sent = await this.channel.send(`**${username}** ${message}`) } catch (err) { throw new Error(err) } return sent } /** * Sends a message to twitch irc from discord. * @param {object} the message data * @return {promise} the sent message */ async sendToTwitch ({ content, author }) { let sent try { sent = await this.twitch.say(author.username, content) } catch (err) { throw new Error(err) } const response = await Commands.testForCommand(content) this.handleResponse(author.username, response) return sent } /** * Sends a twitch whisper. * @param {object} the message data * @return {promise} the sent message */ async sendWhisper ({ username, message }) { let sent try { sent = await this.twitch.whisper(username, message) } catch (err) { throw new Error(err) } return sent } handleResponse (username, response) { if (response && !Array.isArray(response)) { this.twitch.say(username, response) } else if (response && Array.isArray(response)) { response.forEach(r =>{ this.twitch.say(username, r.link) this.sendToDiscord({ username, message: r.link }) }) } } }