Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.1.0 #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ jspm_packages
config.js
npm_test.js
local_test.js
expected.js
.vscode/
186 changes: 100 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,138 @@

<b>Note: </b> This package is not to be used for Twitch botting (inflating live viewer counts) and should only be used for chatbots. Attempting to 'bot' a Twitch channel can lead to your account being permanently banned. ![](https://static-cdn.jtvnw.net/emoticons/v1/91/1.0)

### Installation
Version 2.0.0^ (<b>Recommended</b>):
# Install
```bash
$ npm install node-twitchbot
```
Version 1 (Deprecated):
```bash
$ npm install [email protected]
```

## V2 DOCS
### Example
# Docs
## Basic example
```javascript
const TwitchBot = require('node-twitchbot')

const Bot = new TwitchBot({
username : 'GLADOS',
oauth : 'oauth:secret-oauth-pass',
channel : 'Aperture'
username: 'your_twitch_bot',
oauth: 'oauth:your-oauth-key',
channel: 'your_channel'
})

/* Connect bot to Twitch IRC */
Bot.connect()
.then(() => {

/* Listen for all messages in channel */
Bot.listen((err, chatter) => {
if(err) {
console.log(err)
} else {
console.log(chatter.msg) // 'Hello World!'

Bot.on('message', chatter => {
if(chatter.msg === '!command') {
Bot.msg('Command executed PogChamp')
}
})

/* Listen for an exact messages match */
Bot.listenFor('KKona', (err, chatter) => {
console.log(chatter)
})

/* Send a message in the channel */
Bot.msg('this is the message text PogChamp')

/* Listen for raw IRC events */
Bot.raw((err, event) => {
console.log(event)
Bot.on('error', err => {
console.log('twitch irc error', err)
})
})
.catch(err => {
console.log('Connection error!')
console.log(err)
})
.catch(err => console.log(err))
```

### Chatter Object
Most callbacks return a `chatter` object which contains the following attributes:
## `TwitchBot()` options
| parameter | type | description | required |
| - | - | - | - |
| `username` | `String` | Bot username | ✔️ |
| `oauth` | `String` | Twitch chat oauth token | ✔️ |
| `channel` | `String` | Channel, `#` can be included e.g. `#channel` or `channel` | ✔️ |
| `port` | `int` | Twitch IRC port, usually `443` or `6777`. Defaults to `443`| ❌ |
| `silence` | `boolean` | Prevent bot from sending messages in chat. Outbound messages logged in console - useful for development. Defaults to `false` | ❌ |
| `limit` | `int` | Limit number of raw messages sent to IRC. Defaults to `19`. Use `30` for moderators. | ❌ |
| `period` | `int` | Message rate limit period (milliseconds). Defaults to `30000` | ❌ |

## Events
This package makes use of an `EventEmitter` to emit messages on certain Twitch IRC events. Events can be listened by using the `on` method, for example:
```javascript
{
user: 'kritzware',
msg: 'Hello world! Kappa',
channel: 'kritzware',
twitch_id: '44667418',
level: 'mod',
sub: 0,
turbo: 0
}
Bot.on('event', message => {
// ...
})
```

## V1 DOCS
### Example
```javascript
const Bot = require('node-twitchbot')
The available events are listed below:

Bot.run({
username: 'bot_username',
oauth: 'oauth:twitch_oauth_key',
channel: 'channel'
### `join` - `(connected)`
Example
```javascript
Bot.on('join', connected => {
// ...
})
```
Response
```javascript
connected { joined: true, ts: timestamp }
```

/* Exact message match */
Bot.listenFor('Kappa', (err, chatter) => {
if(err) {
console.log(err)
} else {
console.log(chatter)
}
### `message` - `(chatter)`
Example
```javascript
Bot.on('message', chatter => {
// ...
})
```
Response
```javascript
chatter {
badges: 'subscriber/6,premium/1',
color: '#00FF6A',
'display-name': 'AceSlash',
emotes: true,
id: '73358dc0-e898-4cd2-b5ae-f647893b64b3',
mod: '0',
'room-id': '23161357',
'sent-ts': '1496436125243',
subscriber: '1',
'tmi-sent-ts': '1496436125849',
turbo: '0',
'user-id': '40705354',
'user-type': true
}
```

/* Return all user message in channel */
Bot.listenFor('*', (err, chatter) {
// Returns all viewer messages in channel
})
## Methods

/* String is included in message */
Bot.listen('PogChamp', (err, chatter) => {
console.log(chatter)
### `connect()`
The `connect` method creates a connection to `"irc.chat.twitch.tv"` and resolves once the connection is established.
#### Usage
```javascript
Bot.connect()
.then(() => {
// ...
})
.catch(err => console.log(err))
```

/* Sub/resub event in chat */
Bot.resub((err, chatter, sub) => {
console.log(sub)
})
#### Usage with async/await
```javascript
async function start() {
await Bot.connect()
// ...
}

try {
start()
} catch(err) {
console.log(err)
}
```

/* Say messages in chat */
Bot.msg('Hello chat!')
### `msg(text, callback)`
Sends a chat message to the connected Twitch channel. If `silence` was set to `true` the message will be printed in the console and not sent to IRC. A callback is provided if you wish to validate if the message was correctly sent.
#### Usage
```javascript
Bot.msg('This is a message from the bot! PogChamp')
Bot.msg('Kappa 123')

/* Private message user */
Bot.whisper('kritzware', 'This is a private message Kappa')
Bot.msg('Did this message send? :thinking:', err => {
if(err) console.log(err)
})
```

OLD DOCS (REMOVED BEFORE PUBLISHING)
```javascript
/* Setting commands instead of checking via string match */
const commands = {
help : 'For help using this bot, contact kritzware',
Expand All @@ -132,14 +156,4 @@ Bot.commands('!', commands, (err, chatter, command) => {
console.log(chatter)
}
})
```

#### Output for example '!goodnight' command above
![](http://i.imgur.com/buPqiaK.gif)

#### Example of a command
```javascript
Bot.listenFor('!command', (err, chatter) => {
Bot.msg('This is the command response')
})
```
```
106 changes: 1 addition & 105 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,105 +1 @@
"use strict"

const IRC = require('irc')
const _ = require('lodash')
const parser = require('./src/parser')

function Bot({
username=null,
oauth=null,
channel=null
}) {
if(!username || !oauth || !channel) {
throw new Error('Bot() requires options argument')
}
this.username = username
this.oauth = oauth
this.channel = channel.toLowerCase()
this.client = null
}

Bot.prototype = {

connect() {
return new Promise((resolve, reject) => {
this.client = new IRC.Client('irc.chat.twitch.tv', this.username, {
port: 443,
password: this.oauth,
channels: ['#' + this.channel],
debug: false,
secure: true,
autoConnect: false
})
this.client.connect(connected => {
if(!connected) reject()
if(connected.rawCommand === '001') {
this.client.send('CAP REQ', 'twitch.tv/membership')
this.client.send('CAP REQ', 'twitch.tv/tags')
this.client.send('CAP REQ', 'twitch.tv/commands')
resolve()
}
})
this.client.addListener('error', err => {
console.log('CONNECTION ERROR')
console.log(err)
reject(err)
})
})
},

listen(callback) {
return new Promise((resolve, reject) => {
this.raw((err, event) => {
if(err) {
resolve(callback(err))
} else {
if(event.commandType === 'normal') {
const split = event.command.split(';')
if(_.includes(split[2], 'display-name=') && !_.includes(event.args[0], 'USERSTATE')) {
parser.createChatter(event)
.then(chatter => resolve(callback(null, chatter)))
.catch(err => resolve(callback(err)))
}
}
}
})
})
},

listenFor(word, callback) {
return new Promise((resolve, reject) => {
this.raw((err, event) => {
if(err) {
resolve(callback(err))
} else {
if(event.commandType === 'normal') {
const split = event.command.split(';')
if(_.includes(split[2], 'display-name=')) {
parser.exactMatch(event, word)
.then(chatter => resolve(callback(null, chatter)))
.catch(err => resolve(callback(err)))
}
}
}
})
})
},

raw(cb_event) {
return new Promise((resolve, reject) => {
this.client.addListener('raw', event => {
resolve(cb_event(null, event))
})
this.client.addListener('error', err => {
resolve(cb_event(err))
})
})
},

msg(text) {
this.client.send('PRIVMSG #' + this.channel, text)
}

}

module.exports = Bot
module.exports = require('./src/bot')
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Easily create chat bots for Twitch.tv",
"main": "index.js",
"scripts": {
"test": "mocha test/test.js --timeout 6000"
"test": "mocha test/test.js --timeout 10000"
},
"repository": {
"type": "git",
Expand All @@ -24,7 +24,8 @@
},
"homepage": "https://github.com/kritzware/node-twitchbot#readme",
"dependencies": {
"irc": "kritzware/node-irc",
"irc-message": "^3.0.2",
"limiter": "^1.1.0",
"lodash": "^4.16.2"
},
"devDependencies": {
Expand Down
Loading