tw-irc
TypeScript icon, indicating that this package has built-in type declarations

6.1.3 • Public • Published

tw-irc

NPM version Dependencies Size Version

Overview

Here is a library that handles connection to Twitch IRC. It allows you to join or leave channels, detect and send new messages and other.

Compatible for both node and browser.

Table of contents

Install

npm install --save tw-irc
yarn add tw-irc

Usage

To start working with Twitch IRC, we have to create a client. You can specify if connection is secure, pass channels which client will automatically join on connection established, or pass auth data which is required to send messages from someones face.

Basic

import Client, {ECommand} from 'tw-irc';
 
const {Message} = ECommand;
 
// Create IRC client
const client = new Client();
 
// Bind events before connect. Just watch for incoming messages
client.on(Message, ({message, displayName}) => {
  console.log(`User ${displayName} said: "${message}"`);
});
 
// When socket connection is successfully opened, join channel
client.onConnected(() => {
  client.channels.join('rxnexus');
});
 
// Connect client to IRC
client.connect();

Authenticated client

import Client from 'tw-irc';
 
// Create authenticated IRC client
const client = new Client({
  channels: ['rxnexus'],
  secure: true, // secure connection recommended
  auth: {
    login: 'twitchfan', // your Twitch login
    password: 'oauth:...', // oauth token. Get it here: https://twitchapps.com/tmi/
  },
});
 
client.onConnected(() => {
  // Say hi!
  client.channels.say('Hello @rxnexus!', 'rxnexus');
});
 
client.connect();

Using channel commands

tw-irc supports all of the channel modes and commands.

import Client from 'tw-irc';
 
const client = new Client();
 
client.onConnected(() => {
  client.channels.join('rxnexus');
 
  // Ban someone
  client.channels.ban('troll123', 'rxnexus');
 
  // Set emote-only mode
  client.channels.emoteOnly.enable('rxnexus');
});
 
client.connect();

Forking channels

For easier usage you can create channels controllers from client.

import Client from 'tw-irc';
 
const client = new Client();
 
client.onConnected(() => {
  // Create channel controller
  const channel = client.fork('rxnexus');
  
  // Join channel
  channel.join();
 
  // Say hi
  channel.say('Hello!');
 
  // Set emote only mode
  channel.emoteOnly.enable();
 
  // Ban some troll in channel
  channel.ban('troll123');
});
 
client.connect();

Getting full control

If you want full control over the messages coming from IRC, you can use this trick:

import Client from 'tw-irc';
import {prepareIRCMessage, parseIRCMessage} from 'tw-irc/utils';
 
const client = new Client();
 
client.onMessage(event => {
  // Convert raw socket message to array of messages. We need this action 
  // because commands can be concatenated in one message and doing this, 
  // we just detect them. 
  const messages = prepareIRCMessage(event.data);
 
  // Parse each of the messages  
  const parsedMessages = messages.map(parseIRCMessage);
 
  parsedMessages.forEach(message => {
    // You can react however you want after all of messages are parsed. 
  });
});
 
client.connect();

Example

There are 2 examples for node and browser.

Running node version:

  1. Clone repo;
  2. Type yarn dev-node or npm run dev-node;

Running browser version:

  1. Clone repo;
  2. Type yarn dev or npm run dev;
  3. Open browser and go to http://localhost:9000;
  4. Open console;

Updates history

You can find updates history here.

License

MIT

Package Sidebar

Install

npm i tw-irc

Weekly Downloads

15

Version

6.1.3

License

MIT

Unpacked Size

126 kB

Total Files

127

Last publish

Collaborators

  • qbnk