Deno Twitch Message Interface
this package is very similar to tmi.js but for Deno. This package is still very early in development.
What it does
tmi.ts allows you to create bots and automate tasks in a users Twitch Chat.
What you need in order to use
- OAuth token â https://twitchapps.com/tokengen/ â make sure that your appâs redirect url matches the websites redirect url
- Twitch Client ID
- Your Twitch username in lower-case
Quick Example
import { TwitchChat, Channel } from "https://deno.land/x/tmi/mod.ts";
import { delay } from "https://deno.land/std@0.64.0/async/delay.ts";
const tc = new TwitchChat(oauth, username);
await tc.connect();
const channel = tc.joinChannel("xqc");
const handlePrivMsg = async (chanel: Channel) => {
for await (const pMsg of channel.privMsg()) {
//do something with private msg here
if (pMsg.directMsg) {
console.log(`I've been direct messaged by: ${pmsg.username}`);
}
}
});
// Do not await this as it will block the call stack, instead throw it in the event loop.
handlePrivMsg(channel);
await delay(60000);
tc.exit();
TwitchChat
Allows you to connect to Twitchâs chat, listen to private whispers and more
.connect()
Connects to Twitchâs secure WebSocket endpoint
wss://irc-ws.chat.twitch.tv:443
. Returns a promise that resolves when the user has correctly authenticated else it rejects..joinChannel(channel: string)
Joins the channel that itâs given as a parameter. Returns a promise.
.exit()
Parts all channels that have been joined, cleans up everything in the Event Loop and closes connection to Twitchâs websocket.
channels: Map<string, Channel>
A Map for all channels that are currently joined. If a channel is parted it will also delete itself from this Map.
[Symbol.asyncIterator](): AsyncIterableIterator<TwitchMessage>
Listen to whispers.
const tc = new TwitchChat(oauth, username);
await tc.connect();
for await (const whisper of tc) {
//do something with whisper
}
Channel
Listen to specific events of a channel or part it (leave the channel).
.send(message: string)
Send a message to the channels chat.
.part()
Leave the channel, deletes itself from channels Map in TwitchChat, and resolves all of its promises in event loop.
.channelOwnerName: string
Returns the username of the owner of the chat. For example, if I join âninjaâ chat, it will return âninjaâ.
.commands
These are commands that can be used in a twitch chat. Note that certain commands require certain scopes in your oauth token `. For more information about these commands visit: twitchâs docs
.privMsg(), joinMsg(), roomStageMsg(), clearChatMsg(), clearMsg()
These are all async generators. Use them in order to listen to messages and events of the chatâs channel. The naming of these generators match Irc and Twitchâs commands.
An example of visiting a single channel
const tc = new TwitchChat(oauth, username);
await tc.connect();
const channel = tc.joinChannel("ninja");
// Private messages are just messages of users talking in the chat.
for await (const pMsg of channel.privMsg()) {
//do something with whisper
}
An example of joining multiple channels
async function handlePrivMsg(channel: Channel) {
for await (const pMsg of channel.privMsg()) {
// do something wwith private message
}
}
async function handleClearChatMsg(channel: Channel) {
for await (const ccMsg of channel.clearChatMsg()) {
console.log(`${ccMsg.username} got banned!`);
}
}
const tc = new TwitchChat({ clientId, oauth, userName });
await tc.connect();
const channels: string[] = ["xqc", "ninja", "kitboga"];
for (const channelName of channels) {
const channel = tc.joinChannel(channelName);
channel.send(`Joined ${channelName}'s channel`);
}