Comparing version 1.1.2 to 1.1.3
545
lib/Bot.js
@@ -12,352 +12,357 @@ 'use strict' | ||
const TwitchBot = class TwitchBot extends EventEmitter { | ||
constructor({ | ||
username, | ||
oauth, | ||
channels = [], | ||
port = 443 | ||
}) { | ||
super(); | ||
try { | ||
assert(username); | ||
assert(oauth); | ||
} catch (err) { | ||
throw new Error('invalid arguments'); | ||
} | ||
this.username = username; | ||
this.oauth = oauth; | ||
this.port = port; | ||
this.channels = channels; | ||
this.irc = new tls.TLSSocket(); | ||
//this.pubsub = pubsub; | ||
//this.topics = topics; | ||
constructor({ | ||
username, | ||
oauth, | ||
channels = [], | ||
port = 443 | ||
}) { | ||
super(); | ||
this._connect(); | ||
//if(this.pubsub) { | ||
// this.connectSub(); | ||
//} | ||
try { | ||
assert(username); | ||
assert(oauth); | ||
} catch (err) { | ||
throw new Error('invalid arguments'); | ||
} | ||
this.username = username; | ||
this.oauth = oauth; | ||
this.port = port; | ||
this.channels = channels; | ||
this.irc = new tls.TLSSocket(); | ||
//this.pubsub = pubsub; | ||
//this.topics = topics; | ||
async _connect() { | ||
this.irc.connect({ | ||
host: 'irc.chat.twitch.tv', | ||
port: this.port | ||
}); | ||
this.irc.setEncoding('utf-8'); | ||
this.irc.once('connect', () => { | ||
this.irc.on('error', err => this.emit('error', err)); | ||
this.listen(); | ||
this._connect(); | ||
//if(this.pubsub) { | ||
// this.connectSub(); | ||
//} | ||
} | ||
this.write("PASS " + this.oauth); | ||
this.write("NICK " + this.username); | ||
async _connect() { | ||
this.irc.connect({ | ||
host: 'irc.chat.twitch.tv', | ||
port: this.port | ||
}); | ||
this.irc.setEncoding('utf-8'); | ||
this.irc.once('connect', () => { | ||
this.irc.on('error', err => this.emit('error', err)); | ||
this.listen(); | ||
this.channels.forEach(channel => this.join(channel)); | ||
this.write("PASS " + this.oauth); | ||
this.write("NICK " + this.username); | ||
this.write("CAP REQ :twitch.tv/tags"); | ||
this.write("CAP REQ :twitch.tv/membership"); | ||
this.write("CAP REQ :twitch.tv/commands"); | ||
this.channels.forEach(channel => this.join(channel)); | ||
this.emit('connected'); | ||
}); | ||
} | ||
this.write("CAP REQ :twitch.tv/tags"); | ||
this.write("CAP REQ :twitch.tv/membership"); | ||
this.write("CAP REQ :twitch.tv/commands"); | ||
async write(message) { | ||
this.irc.write(message + "\r\n"); | ||
} | ||
this.emit('connected'); | ||
}); | ||
} | ||
async join(channel) { | ||
channel = utils.formatCHANNEL(channel); | ||
this.write("JOIN " + channel); | ||
} | ||
async write(message) { | ||
this.irc.write(message + "\r\n"); | ||
} | ||
async check(text) { | ||
let res = await utils.checkErrors(text); | ||
switch (res) { | ||
case "LOGIN": | ||
this.irc.emit('error', { | ||
message: "Login authentication failed" | ||
}); | ||
break; | ||
async join(channel) { | ||
channel = utils.formatCHANNEL(channel); | ||
this.write("JOIN " + channel); | ||
} | ||
case "AUTH": | ||
this.irc.emit('error', { | ||
message: "Improperly formatted auth" | ||
}); | ||
break; | ||
async check(text) { | ||
let res = await utils.checkErrors(text); | ||
switch (res) { | ||
case "LOGIN": | ||
this.irc.emit('error', { | ||
message: "Login authentication failed" | ||
}); | ||
break; | ||
case "SPAM": | ||
this.irc.emit('error', { | ||
message: "Your message was not sent because you are sending messages too quickly" | ||
}); | ||
break; | ||
case "AUTH": | ||
this.irc.emit('error', { | ||
message: "Improperly formatted auth" | ||
}); | ||
break; | ||
case "OK": | ||
break; | ||
} | ||
case "SPAM": | ||
this.irc.emit('error', { | ||
message: "Your message was not sent because you are sending messages too quickly" | ||
}); | ||
break; | ||
case "OK": | ||
break; | ||
} | ||
} | ||
async leave(channel) { | ||
if (!channel && this.channels.length > 0) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.write("PART " + channel); | ||
async leave(channel) { | ||
if (!channel && this.channels.length > 0) { | ||
channel = this.channels[0]; | ||
} | ||
async listen() { | ||
this.irc.on('data', data => { | ||
this.emit('ircdata', data); | ||
this.check(data); | ||
channel = utils.formatCHANNEL(channel); | ||
this.write("PART " + channel); | ||
} | ||
/** HeartBeat */ | ||
if (data.includes('PING :tmi.twitch.tv')) { | ||
this.irc.write('PONG :tmi.twitch.tv\r\n'); | ||
} | ||
async listen() { | ||
this.irc.on('data', data => { | ||
this.emit('ircdata', data); | ||
this.check(data); | ||
if (data.includes('PRIVMSG')) { | ||
const chatter = utils.formatPRIVMSG(data); | ||
this.emit('message', chatter); | ||
} | ||
/** HeartBeat */ | ||
if (data.includes('PING :tmi.twitch.tv')) { | ||
this.irc.write('PONG :tmi.twitch.tv\r\n'); | ||
} | ||
if (data.includes('CLEARCHAT')) { | ||
const event = utils.formatCLEARCHAT(data); | ||
if (event.type === 'timeout') this.emit('timeout', event); | ||
if (event.type === 'ban') this.emit('ban', event); | ||
} | ||
if (data.includes('PRIVMSG')) { | ||
const chatter = utils.formatPRIVMSG(data); | ||
this.emit('message', chatter); | ||
} | ||
if (data.includes('USERNOTICE')) { | ||
const event = utils.formatUSERNOTICE(data); | ||
if (['sub', 'resub', 'subgift'].includes(event.msg_id)) { | ||
this.emit('subscription', event); | ||
} | ||
} | ||
if (data.includes('CLEARCHAT')) { | ||
const event = utils.formatCLEARCHAT(data); | ||
if (event.type === 'timeout') this.emit('timeout', event); | ||
if (event.type === 'ban') this.emit('ban', event); | ||
} | ||
if (data.includes(`@${this.username}.tmi.twitch.tv JOIN`)) { | ||
const channel = utils.formatJOIN(data).split(':')[0]; | ||
if (channel) { | ||
if (!this.channels.includes(channel)) { | ||
this.channels.push(channel); | ||
} | ||
this.emit('join', channel); | ||
} | ||
} | ||
if (data.includes('USERNOTICE')) { | ||
const event = utils.formatUSERNOTICE(data); | ||
if (['sub', 'resub', 'subgift'].includes(event.msg_id)) { | ||
this.emit('subscription', event); | ||
} | ||
} | ||
if (data.includes(`@${this.username}.tmi.twitch.tv PART`)) { | ||
const channel = utils.formatPART(data).split(':')[0]; | ||
if (channel) { | ||
if (this.channels.includes(channel)) { | ||
this.channels.pop(channel); | ||
} | ||
this.emit('part', channel); | ||
} | ||
} | ||
}); | ||
} | ||
if (data.includes(`@${this.username}.tmi.twitch.tv JOIN`)) { | ||
const channel = utils.formatJOIN(data).split(':')[0]; | ||
if (channel) { | ||
if (!this.channels.includes(channel)) { | ||
this.channels.push(channel); | ||
} | ||
this.emit('join', channel); | ||
} | ||
} | ||
async callback(callback, obj) { | ||
if (callback) { | ||
obj.ts = new Date(); | ||
callback(obj); | ||
if (data.includes(`@${this.username}.tmi.twitch.tv PART`)) { | ||
const channel = utils.formatPART(data).split(':')[0]; | ||
if (channel) { | ||
if (this.channels.includes(channel)) { | ||
this.channels.pop(channel); | ||
} | ||
this.emit('part', channel); | ||
} | ||
} | ||
}); | ||
} | ||
async callback(callback, obj) { | ||
if (callback) { | ||
obj.ts = new Date(); | ||
callback(obj); | ||
} | ||
} | ||
async say(message, channel, callback) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (message.length >= 500) { | ||
this.callback(callback, { | ||
sent: false, | ||
message: 'Message to long (>=500)' | ||
}) | ||
} else { | ||
this.write('PRIVMSG ' + channel + ' :' + message); | ||
} | ||
async say(message, channel, callback) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (message.length >= 500) { | ||
this.callback(callback, { | ||
sent: false, | ||
message: 'Message to long (>=500)' | ||
}) | ||
} else { | ||
this.write('PRIVMSG ' + channel + ' :' + message); | ||
} | ||
} | ||
async timeout(username, channel, duration = 600, reason = '') { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async me(message, channel, callback) { | ||
this.say(`/me ${message}`, channel, callback); | ||
} | ||
this.say(`/timeout ${username} ${duration} ${reason}`, channel); | ||
async timeout(username, channel, duration = 600, reason = '') { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async ban(username, channel, reason = '') { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/timeout ${username} ${duration} ${reason}`, channel); | ||
} | ||
this.say(`/ban ${username} ${reason}`, channel); | ||
async ban(username, channel, reason = '') { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async emoteChat(state, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/ban ${username} ${reason}`, channel); | ||
} | ||
if (state) this.say(`/emoteonly`, channel); | ||
else this.say(`/emoteonlyoff`, channel); | ||
async emoteChat(state, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async followerChat(state, channel, duration = null) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (state) this.say(`/emoteonly`, channel); | ||
else this.say(`/emoteonlyoff`, channel); | ||
} | ||
if (state) this.say(`/followers ${duration}`); | ||
else this.say(`/followersoff`, channel); | ||
async followerChat(state, channel, duration = null) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async subscriberChat(state, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (state) this.say(`/followers ${duration}`); | ||
else this.say(`/followersoff`, channel); | ||
} | ||
if (state) this.say(`/subscribers`); | ||
else this.say(`/subscribersoff`, channel); | ||
async subscriberChat(state, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async uniqueChat(state, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (state) this.say(`/subscribers`); | ||
else this.say(`/subscribersoff`, channel); | ||
} | ||
if (state) this.say(`/uniquechat`, channel); | ||
else this.say(`/uniquechatoff`, channel); | ||
async uniqueChat(state, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async slowChat(state, channel, delay = 10) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (state) this.say(`/uniquechat`, channel); | ||
else this.say(`/uniquechatoff`, channel); | ||
} | ||
if (state) this.say(`/slow ${delay}`, channel); | ||
else this.say(`/slowoff`, channel); | ||
async slowChat(state, channel, delay = 10) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async clearChat(channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
if (state) this.say(`/slow ${delay}`, channel); | ||
else this.say(`/slowoff`, channel); | ||
} | ||
this.say(`/clear`, channel); | ||
async clearChat(channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async setMod(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/clear`, channel); | ||
} | ||
this.say(`/mod ${username}`, channel); | ||
async setMod(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async removeMod(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/mod ${username}`, channel); | ||
} | ||
this.say(`/unmod ${username}`, channel); | ||
async removeMod(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async setVip(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/unmod ${username}`, channel); | ||
} | ||
this.say(`/vip ${username}`, channel); | ||
async setVip(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async removeVip(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/vip ${username}`, channel); | ||
} | ||
this.say(`/unvip ${username}`, channel); | ||
async removeVip(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async setColor(color) { | ||
channel = utils.formatCHANNEL(this.channels[0]); | ||
this.say(`/color ${color}`, channel); | ||
} | ||
this.say(`/unvip ${username}`, channel); | ||
} | ||
async removeBan(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async setColor(color) { | ||
channel = utils.formatCHANNEL(this.channels[0]); | ||
this.say(`/color ${color}`, channel); | ||
} | ||
this.say(`/unban ${username}`, channel); | ||
async removeBan(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async removeTimeout(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
this.say(`/unban ${username}`, channel); | ||
} | ||
this.say(`/untimeout ${username}`, channel); | ||
async removeTimeout(username, channel) { | ||
if (!channel) { | ||
channel = this.channels[0]; | ||
} | ||
channel = utils.formatCHANNEL(channel); | ||
async updateTitle(title, channel) { | ||
let id = await api.getUserByName(channel, this.oauth)['_id']; | ||
await api.updateTitle(id, title, this.oauth); | ||
} | ||
this.say(`/untimeout ${username}`, channel); | ||
} | ||
async updateGame(game, channel) { | ||
let id = await api.getUserByName(channel, this.oauth)['_id']; | ||
await api.updateGame(id, game, this.oauth); | ||
} | ||
async updateTitle(title, channel) { | ||
let id = await api.getUserByName(channel, this.oauth)['_id']; | ||
await api.updateTitle(id, title, this.oauth); | ||
} | ||
async getChannel(channel) { | ||
let id = await api.getUserByName(channel)['_id']; | ||
return await api.getChannel(id, this.oauth); | ||
} | ||
async updateGame(game, channel) { | ||
let id = await api.getUserByName(channel, this.oauth)['_id']; | ||
await api.updateGame(id, game, this.oauth); | ||
} | ||
async close() { | ||
this.irc.destroy(); | ||
this.emit('close'); | ||
} | ||
async getChannel(channel) { | ||
let id = await api.getUserByName(channel)['_id']; | ||
return await api.getChannel(id, this.oauth); | ||
} | ||
async _nonce(length) { | ||
var text = ""; | ||
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
for (var i = 0; i < length; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
async close() { | ||
this.irc.destroy(); | ||
this.emit('close'); | ||
} | ||
async _nonce(length) { | ||
var text = ""; | ||
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
for (var i = 0; i < length; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
} | ||
async _listenPubSub(topics, channel) { | ||
channel = api.getUserByName(channel, this.oauth); | ||
let x = []; | ||
topics.forEach((topic) => { | ||
x.push(topic + "." + channel['_id']); | ||
}) | ||
async _listenPubSub(topics, channel) { | ||
channel = api.getUserByName(channel, this.oauth); | ||
let x = []; | ||
topics.forEach((topic) => { | ||
x.push(topic + "." + channel['_id']); | ||
}) | ||
let message = { | ||
type: 'LISTEN', | ||
nonce: this._nonce(15), | ||
data: { | ||
topics: x, | ||
auth_token: this.oauth | ||
} | ||
}; | ||
let message = { | ||
type: 'LISTEN', | ||
nonce: this._nonce(15), | ||
data: { | ||
topics: x, | ||
auth_token: this.oauth | ||
} | ||
}; | ||
if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message)); | ||
} | ||
if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message)); | ||
} | ||
@@ -367,2 +372,2 @@ } | ||
module.exports = TwitchBot; | ||
module.exports = TwitchBot; |
{ | ||
"name": "twitch-kit", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Twitch Bot Package with amazing features like Title Update.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
654
27207