fair-twitch
Version:
Fair's Twitch API and Chat bot library
306 lines (305 loc) • 17.9 kB
TypeScript
/// <reference types="node" />
import net from 'net';
import ExpandedEventEmitter from './ExpandedEventEmitter';
import NotificationsEmitter from './NotificationsEmitter';
import RoomTracker from './RoomTracker';
/**
* Notes:
* Normal subs (usernotice):
* tags['msg-id'] = 'sub' or 'resub'
* tags['msg-param-sub-plan'] = 'Prime', '1000', '2000', '3000'
* tags['msg-param-cumulative-months'] = String number of months subscribed
* Gift subs (usernotice):
* tags['msg-id'] = 'subgift' or 'anonsubgift'
* tags['msg-param-sub-plan'] = 'Prime', '1000', '2000', '3000'
* tags['msg-param-sender-count'] = String count of gifters total gifts
* tags['msg-param-recipient-user-name'] = login of recipient
* tags['msg-param-recipient-display-name'] = display name of recipient
* tags['msg-param-cumulative-months'] = String number of months recipient subscribed
* Mass gift subs (usernotice):
* Will first send a:
* tags['msg-id'] = 'submysterygift'
* tags['msg-param-sub-plan'] = 'Prime', '1000', '2000', '3000'
* tags['msg-param-mass-gift-count'] = String number of subs that's being gifted
* tags['msg-param-sender-count'] = String count of gifters total gifts (was 0 when admiralbahroo gave 100 subs to himself?)
* Then all the subs will be sent as gift subs (see above)
* Bits (msg):
* tags['bits'] = https://dev.twitch.tv/docs/irc/tags#privmsg-twitch-tags
*/
interface TwitchIRC {
addListener(event: string, listener: (...args: any[]) => void): this;
/** When an error has happened */
addListener(event: 'error', listener: (error: any) => void): this;
/** When a parse error happens */
addListener(event: 'parseerror', listener: (line: string, error: any) => void): this;
/** When successfully connected and authorized */
addListener(event: 'ready', listener: () => void): this;
/** A raw IRC event */
addListener(event: 'raw', listener: (raw: string, parsed: ParsedMessage) => void): this;
/** When a raw message is sent out */
addListener(event: 'rawSend', listener: (message: string) => void): this;
/** When you have joined a channel */
addListener(event: 'join', listener: (channel: string) => void): this;
/** When you have left a channel */
addListener(event: 'part', listener: (channel: string) => void): this;
/** When a user joined a channel */
addListener(event: 'userjoin', listener: (channel: string, login: string) => void): this;
/** When a user left a channel */
addListener(event: 'userpart', listener: (channel: string, login: string) => void): this;
/** When a message was submitted to a channel */
addListener(event: 'msg', listener: (channel: string, login: string, message: string, tags: any) => void): this;
/** When a channel changes its roomstate (like submode, slowmode) */
addListener(event: 'roomstate', listener: (channel: string, tags: any) => void): this;
/** When a usernotice has happened (like a sub, resub, giftsub) */
addListener(event: 'usernotice', listener: (channel: string, login: string, message: string, tags: any) => void): this;
/** When a channel notice happens (example: slow mode off) */
addListener(event: 'notice', listener: (channel: string, message: string, tags: any) => void): this;
/** Happens when a chat is cleared by a moderator */
addListener(event: 'clearchat', listener: (channel: string) => void): this;
/** User was permanently or temporarily banned (tags has ban-duration if not permanent) */
addListener(event: 'userban', listener: (channel: string, login: string, tags: any) => void): this;
/** Happens when a single message was removed */
addListener(event: 'clearmsg', listener: (channel: string, tags: any) => void): this;
/** Happens on successful login, tags contain information about user */
addListener(event: 'globaluserstate', listener: (tags: any) => void): this;
/** Happens when you join a channel, tags contain information about user */
addListener(event: 'userstate', listener: (channel: string, tags: any) => void): this;
/** Happens when a channel hosts a target channel. Viewers is a number is started hosting, undefined if already hosting. If taget is '-', it means it stopped hosting */
addListener(event: 'host', listener: (channel: string, target: string, viewers?: number) => void): this;
on(event: string, listener: (...args: any[]) => void): this;
/** When an error has happened */
on(event: 'error', listener: (error: any) => void): this;
/** When a parse error happens */
on(event: 'parseerror', listener: (line: string, error: any) => void): this;
/** When successfully connected and authorized */
on(event: 'ready', listener: () => void): this;
/** A raw IRC event */
on(event: 'raw', listener: (raw: string, parsed: ParsedMessage) => void): this;
/** When a raw message is sent out */
on(event: 'rawSend', listener: (message: string) => void): this;
/** When you have joined a channel */
on(event: 'join', listener: (channel: string) => void): this;
/** When you have left a channel */
on(event: 'part', listener: (channel: string) => void): this;
/** When a user joined a channel */
on(event: 'userjoin', listener: (channel: string, login: string) => void): this;
/** When a user left a channel */
on(event: 'userpart', listener: (channel: string, login: string) => void): this;
/** When a message was submitted to a channel */
on(event: 'msg', listener: (channel: string, login: string, message: string, tags: any) => void): this;
/** When a channel changes its roomstate (like submode, slowmode) */
on(event: 'roomstate', listener: (channel: string, tags: any) => void): this;
/** When a usernotice has happened (like a sub, resub, giftsub) */
on(event: 'usernotice', listener: (channel: string, login: string, message: string, tags: any) => void): this;
/** When a channel notice happens (example: slow mode off) */
on(event: 'notice', listener: (channel: string, message: string, tags: any) => void): this;
/** Happens when a chat is cleared by a moderator */
on(event: 'clearchat', listener: (channel: string) => void): this;
/** User was permanently or temporarily banned (tags has ban-duration if not permanent) */
on(event: 'userban', listener: (channel: string, login: string, tags: any) => void): this;
/** Happens when a single message was removed */
on(event: 'clearmsg', listener: (channel: string, tags: any) => void): this;
/** Happens on successful login, tags contain information about user */
on(event: 'globaluserstate', listener: (tags: any) => void): this;
/** Happens when you join a channel, tags contain information about user */
on(event: 'userstate', listener: (channel: string, tags: any) => void): this;
/** Happens when a channel hosts a target channel. Viewers is a number when started hosting, undefined if already hosting. If taget is '-', it means it stopped hosting */
on(event: 'host', listener: (channel: string, target: string, viewers?: number) => void): this;
once(event: string, listener: (...args: any[]) => void): this;
/** When an error has happened */
once(event: 'error', listener: (error: any) => void): this;
/** When a parse error happens */
once(event: 'parseerror', listener: (line: string, error: any) => void): this;
/** When successfully connected and authorized */
once(event: 'ready', listener: () => void): this;
/** A raw IRC event */
once(event: 'raw', listener: (raw: string, parsed: ParsedMessage) => void): this;
/** When a raw message is sent out */
once(event: 'rawSend', listener: (message: string) => void): this;
/** When you have joined a channel */
once(event: 'join', listener: (channel: string) => void): this;
/** When you have left a channel */
once(event: 'part', listener: (channel: string) => void): this;
/** When a user joined a channel */
once(event: 'userjoin', listener: (channel: string, login: string) => void): this;
/** When a user left a channel */
once(event: 'userpart', listener: (channel: string, login: string) => void): this;
/** When a message was submitted to a channel */
once(event: 'msg', listener: (channel: string, login: string, message: string, tags: any) => void): this;
/** When a channel changes its roomstate (like submode, slowmode) */
once(event: 'roomstate', listener: (channel: string, tags: any) => void): this;
/** When a usernotice has happened (like a sub, resub, giftsub) */
once(event: 'usernotice', listener: (channel: string, login: string, message: string, tags: any) => void): this;
/** When a channel notice happens (example: slow mode off) */
once(event: 'notice', listener: (channel: string, message: string, tags: any) => void): this;
/** Happens when a chat is cleared by a moderator */
once(event: 'clearchat', listener: (channel: string) => void): this;
/** User was permanently or temporarily banned (tags has ban-duration if not permanent) */
once(event: 'userban', listener: (channel: string, login: string, tags: any) => void): this;
/** Happens when a single message was removed */
once(event: 'clearmsg', listener: (channel: string, tags: any) => void): this;
/** Happens on successful login, tags contain information about user */
once(event: 'globaluserstate', listener: (tags: any) => void): this;
/** Happens when you join a channel, tags contain information about user */
once(event: 'userstate', listener: (channel: string, tags: any) => void): this;
/** Happens when a channel hosts a target channel. Viewers is a number is started hosting, undefined if already hosting. If taget is '-', it means it stopped hosting */
once(event: 'host', listener: (channel: string, target: string, viewers?: number) => void): this;
onceIf(event: string, listener: (...args: any[]) => boolean, timeout?: number): this;
/** When an error has happened */
onceIf(event: 'error', listener: (error: any) => boolean, timeout?: number): this;
/** When a parse error happens */
onceIf(event: 'parseerror', listener: (line: string, error: any) => boolean, timeout?: number): this;
/** When successfully connected and authorized */
onceIf(event: 'ready', listener: () => boolean, timeout?: number): this;
/** A raw IRC event */
onceIf(event: 'raw', listener: (raw: string, parsed: ParsedMessage) => boolean, timeout?: number): this;
/** When a raw message is sent out */
onceIf(event: 'rawSend', listener: (message: string) => boolean, timeout?: number): this;
/** When you have joined a channel */
onceIf(event: 'join', listener: (channel: string) => boolean, timeout?: number): this;
/** When you have left a channel */
onceIf(event: 'part', listener: (channel: string) => boolean, timeout?: number): this;
/** When a user joined a channel */
onceIf(event: 'userjoin', listener: (channel: string, login: string) => boolean, timeout?: number): this;
/** When a user left a channel */
onceIf(event: 'userpart', listener: (channel: string, login: string) => boolean, timeout?: number): this;
/** When a message was submitted to a channel */
onceIf(event: 'msg', listener: (channel: string, login: string, message: string, tags: any) => boolean, timeout?: number): this;
/** When a channel changes its roomstate (like submode, slowmode) */
onceIf(event: 'roomstate', listener: (channel: string, tags: any) => boolean, timeout?: number): this;
/** When a usernotice has happened (like a sub, resub, giftsub) */
onceIf(event: 'usernotice', listener: (channel: string, login: string, message: string, tags: any) => boolean, timeout?: number): this;
/** When a channel notice happens (example: slow mode off) */
onceIf(event: 'notice', listener: (channel: string, message: string, tags: any) => boolean, timeout?: number): this;
/** Happens when a chat is cleared by a moderator */
onceIf(event: 'clearchat', listener: (channel: string) => boolean, timeout?: number): this;
/** User was permanently or temporarily banned (tags has ban-duration if not permanent) */
onceIf(event: 'userban', listener: (channel: string, login: string, tags: any) => boolean, timeout?: number): this;
/** Happens when a single message was removed */
onceIf(event: 'clearmsg', listener: (channel: string, tags: any) => boolean, timeout?: number): this;
/** Happens on successful login, tags contain information about user */
onceIf(event: 'globaluserstate', listener: (tags: any) => boolean, timeout?: number): this;
/** Happens when you join a channel, tags contain information about user */
onceIf(event: 'userstate', listener: (channel: string, tags: any) => boolean, timeout?: number): this;
/** Happens when a channel hosts a target channel. Viewers is a number is started hosting, undefined if already hosting. If taget is '-', it means it stopped hosting */
onceIf(event: 'host', listener: (channel: string, target: string, viewers?: number) => boolean, timeout?: number): this;
emit(event: string, ...args: any[]): boolean;
emit(event: 'error', error: any): boolean;
emit(event: 'parseerror', line: string, error: any): boolean;
emit(event: 'ready'): boolean;
emit(event: 'raw', raw: string, parsed: ParsedMessage): boolean;
emit(event: 'rawSend', message: string): boolean;
emit(event: 'join', channel: string): boolean;
emit(event: 'part', channel: string): boolean;
emit(event: 'userjoin', channel: string, login: string): boolean;
emit(event: 'userpart', channel: string, login: string): boolean;
emit(event: 'msg', channel: string, login: string, message: string, tags: any): boolean;
emit(event: 'roomstate', channel: string, tags: any): boolean;
emit(event: 'usernotice', channel: string, login: string, message: string, tags: any): boolean;
emit(event: 'notice', channel: string, message: string, tags: any): boolean;
emit(event: 'clearchat', channel: string): boolean;
emit(event: 'userban', channel: string, login: string, tags: any): boolean;
emit(event: 'clearmsg', channel: string, tags: any): boolean;
emit(event: 'globaluserstate', tags: any): boolean;
emit(event: 'userstate', channel: string, tags: any): boolean;
emit(event: 'host', channel: string, target: string, viewers?: number): boolean;
}
interface ParsedMessage {
tags: any;
url: string | null;
cmd: string | null;
channel: string | null;
extra: string | null;
msg: string | null;
}
/** Constructor options */
interface ChatOptionsParam {
/** The Twitch login. Use null for anonymous. Defaults to null */
login?: string | null;
/**
* The Twitch auth token. Required if login is not null.
* Example: "cfabdegwdoklmawdzdo98xt2fo512y"
* Default: null
*/
token?: string | null;
/** If you want to auto reconnect on conn loss. Defaults to true */
autoReconnect?: boolean;
/** If you want to request Twitch capabilities. Defaults to true */
requestCAP?: boolean;
/** If you want to connect automatically. Defaults to true */
autoConnect?: boolean;
/** The Twitch IRC URL. Defaults to "irc.chat.twitch.tv" */
url?: string;
/** The Twitch IRC Port. Defaults to 6667 */
port?: number;
/**
* Some of the required messages for a successful connect.
* See https://dev.twitch.tv/docs/irc/guide/#connecting-to-twitch-irc
* Do not change unless this is not updated for it.
*/
successMessages?: string[];
}
interface ChatOptions {
login: string | null;
token: string | null;
autoReconnect: boolean;
requestCAP: boolean;
autoConnect: boolean;
url: string;
port: number;
successMessages: string[];
}
declare class TwitchIRC extends ExpandedEventEmitter {
options: ChatOptions;
ready: boolean;
sendQueue: string[];
closeCalled: boolean;
socket: net.Socket | null;
dataBuffer: string;
readyList: string[];
/**
* @param options The constructor options
*/
constructor(options?: ChatOptionsParam);
/**
* Starts a connection to Twitch IRC servers using the options given in constructor
* Will reconnect if already connected
* @param callback Callback for when connection was successfully created (not ready to be used)
*/
connect(callback?: () => void): void;
/**
* @returns If connection is ready or not
*/
isReady(): boolean;
createNotificationsEmitter(): NotificationsEmitter;
createRoomTracker(): RoomTracker;
/**
* Joins a channel, callback is optional and has no parameters
* @param channel The channel to join (without #)
* @param callback Channel joined callback. Times out after 5 seconds if still not joined
*/
join(channel: string, callback?: () => void): void;
/**
* Leaves a channel, callback is optional and has no parameters
* @param channel The channel to join (without #)
* @param callback Channel joined callback. Times out after 5 seconds if still not left
*/
part(channel: string, callback?: () => void): void;
/**
* Sends a chat message in a channel
* @param channel The channel to talk in (without #)
* @param msg The message to send
*/
say(channel: string, msg: string): void;
/**
* Sends data to Twitch. Use @function Say() to send chat messages
* @param data The data to send
*/
send(data: string): void;
/**
* Tries to close the connection.
* @param callback Callback when close happened
*/
close(callback?: (err: any) => void): void;
}
export = TwitchIRC;