Twitch deprecations
-
Twitch removed
/-chat commands other than/me(action) through the IRC connection on February 18, 2023. See the announcement. This removed a lot of functionality which affects tmi.js. To resolve this and keep the nice interface of tmi.js, it will be rewritten completely to support making API calls directly. A new v2 will be available in the near future.
Getting started
Install
Install using npm:
npm i tmi.js
Examples
Anonymous connection
An anonymous connection allows you to connect to Twitch without having to authenticate. This is ideal for chat overlays.
const tmi = require('tmi.js');
const client = new tmi.Client({
channels: [ 'my_name' ]
});
client.connect();
client.on('message', (channel, tags, message, self) => {
// "Alca: Hello, World!"
console.log(`${tags['display-name']}: ${message}`);
});
OAuth token authorization
Using authorization allows the bot to send messages on behalf of the authenticated account. Typically this would be a dedicated bot account, but it could be an interface for a user to send messages directly.
const tmi = require('tmi.js');
const client = new tmi.Client({
options: { debug: true },
identity: {
username: 'my_bot_name',
password: 'oauth:my_bot_token'
},
channels: [ 'my_name' ]
});
client.connect();
client.on('message', (channel, tags, message, self) => {
// Ignore echoed messages.
if(self) return;
if(message.toLowerCase() === '!hello') {
// "@alca, heya!"
client.say(channel, `@${tags.username}, heya!`);
}
});
Simple command handler
This command handler example will split a message like this example chat message: !Echo Chat
message here into the command "echo" with the arguments [ "Chat", "message", "here" ].
client.on('message', (channel, tags, message, self) => {
if(self || !message.startsWith('!')) return;
const args = message.slice(1).split(' ');
const command = args.shift().toLowerCase();
if(command === 'echo') {
client.say(channel, `@${tags.username}, you said: "${args.join(' ')}"`);
}
});
Guide
Client options
Send messages
You must use an authenticated client (via the identity option) to send messages to
chat. This means you need an OAuth token connected to a Twitch account. Ideally this would be an
alternate account dedicated to the bot, like the "Nightbot" account.
// Regular
client.say(channel, message);
// Action
client.action(channel, message);
