Skip to content

Instantly share code, notes, and snippets.

@Stapleton
Last active December 3, 2018 02:10
Show Gist options
  • Save Stapleton/57b7890e682012d02054cb62cea65e5e to your computer and use it in GitHub Desktop.
Save Stapleton/57b7890e682012d02054cb62cea65e5e to your computer and use it in GitHub Desktop.
TSD-JSDoc will actually work.
/**
* @typedef {object} UserNoticeMessageParam
* @property {?string} displayName
* @property {?string} login
* @property {?string} months
* @property {?string} recipientDisplayName
* @property {?string} recipientId
* @property {?string} recipientUserName
* @property {?string} subPlan
* @property {?string} subPlanName
* @property {?string} viewerCount
* @property {?string} ritualName
*/
declare type UserNoticeMessageParam = {
displayName: string;
login: string;
months: string;
recipientDisplayName: string;
recipientId: string;
recipientUserName: string;
subPlan: string;
subPlanName: string;
viewerCount: string;
ritualName: string;
};
/**
* @external CLEARCHAT
* @see {@link https://dev.twitch.tv/docsc/irc/tags/#clearchat-twitch-tags}
* @typedef {object} ClearChatTags
* @property {string} banDuration
* @property {string} banReason
*/
declare type ClearChatTags = {
banDuration: string;
banReason: string;
};
/**
* @external CLEARMSG
* @see {@link https://dev.twitch.tv/docs/irc/tags/#clearmsg-twitch-tags}
* @typedef {object} ClearMessageTags
* @property {string} login
* @property {string} message
* @property {string} targetMsgId
*/
declare type ClearMessageTags = {
login: string;
message: string;
targetMsgId: string;
};
/**
* @external GLOBALUSERSTATE
* @see {@link https://dev.twitch.tv/docs/irc/tags/#globaluserstate-twitch-tags}
* @typedef {object} GlobalUserState
* @property {string} raw Raw IRC response
* @property {string} badges
* @property {string} color
* @property {string} displayName
* @property {string} emoteSets
*/
declare type GlobalUserState = {
raw: string;
badges: string;
color: string;
displayName: string;
emoteSets: string;
};
/**
* @external PRIVMSG
* @see {@link https://dev.twitch.tv/docs/irc/tags/#privmsg-twitch-tags}
* @typedef {object} PrivateMessage
* @property {string} badges
* @property {?string} bits
* @property {string} color
* @property {string} displayName
* @property {string} emotes
* @property {string} id
* @property {string} message
* @property {string} mod
* @property {string} roomId
* @property {string} subscriber
* @property {string} tmiSentTS
* @property {string} turbo
* @property {string} userId
* @property {string} userType
*/
declare type PrivateMessage = {
badges: string;
bits: string;
color: string;
displayName: string;
emotes: string;
id: string;
message: string;
mod: string;
roomId: string;
subscriber: string;
tmiSentTS: string;
turbo: string;
userId: string;
userType: string;
};
/**
* @external ROOMSTATE
* @see {@link https://dev.twitch.tv/docs/irc/tags/#roomstate-twitch-tags}
* @typedef {object} RoomState
* @property {string} broadcasterLang
* @property {string} emoteOnly
* @property {string} followersOnly
* @property {string} r9k
* @property {string} slow
* @property {string} subsOnly
*/
declare type RoomState = {
broadcasterLang: string;
emoteOnly: string;
followersOnly: string;
r9k: string;
slow: string;
subsOnly: string;
};
/**
* @external USERNOTICE
* @see {@link https://dev.twitch.tv/docs/irc/tags/#usernotice-twitch-tags}
* @typedef {object} UserNotice
* @property {string} badges
* @property {string} color
* @property {string} displayName
* @property {string} emotes
* @property {string} id
* @property {string} login
* @property {string} message
* @property {string} mod
* @property {string} msgId
* @property {UserNoticeMessageParam} msgParam
* @property {string} roomId
* @property {string} subscriber
* @property {string} systemMsg
* @property {string} tmiSentTS
* @property {string} turbo
* @property {string} userId
* @property {string} userType
*/
declare type UserNotice = {
badges: string;
color: string;
displayName: string;
emotes: string;
id: string;
login: string;
message: string;
mod: string;
msgId: string;
msgParam: UserNoticeMessageParam;
roomId: string;
subscriber: string;
systemMsg: string;
tmiSentTS: string;
turbo: string;
userId: string;
userType: string;
};
/**
* @external USERSTATE
* @see {@link https://dev.twitch.tv/docs/irc/tags/#userstate-twitch-tags}
* @typedef {object} UserState
* @property {string} badges
* @property {string} color
* @property {string} displayName
* @property {string} emotes
* @property {string} mod
* @property {string} subscriber
* @property {string} turbo
* @property {string} userType
*/
declare type UserState = {
badges: string;
color: string;
displayName: string;
emotes: string;
mod: string;
subscriber: string;
turbo: string;
userType: string;
};
/**
* @typedef {object} ChannelState
* @property {string} channel
* @property {RoomState} roomState
* @property {UserState} userState
*/
declare type ChannelState = {
channel: string;
roomState: RoomState;
userState: UserState;
};
/**
* @typedef {object} ChatOptions
* @property {string} [username]
* @property {string} [token] OAuth token (use {@link https://twitchtokengenerator.com/} to generate one)
* @property {number} [connectionTimeout=CONNECTION_TIMEOUT]
* @property {number} [joinTimeout=JOIN_TIMEOUT]
* @property {object} log
* @property {Function} [onAuthenticationFailure]
*/
declare type ChatOptions = {
username?: string;
token?: string;
connectionTimeout?: number;
joinTimeout?: number;
log: object;
onAuthenticationFailure?: ()=>any;
};
/**
* @typedef {object} ApiOptions
* @property {string} [clientId] Optional if token is defined.
* @property {string} [token] Optional if clientId is defined.
* @property {object} [log] Log options
* @property {Function} [onAuthenticationFailure]
*/
declare type ApiOptions = {
clientId?: string;
token?: string;
log?: object;
onAuthenticationFailure?: ()=>any;
};
/**
* @class
* @public
* @extends external:EventEmitter3
* @classdesc Twitch Chat Client
* @emits Chat#*
* @emits Chat#CLEARCHAT
* @emits Chat#CLEARCHAT/USER_BANNED
* @emits Chat#GLOBALUSERSTATE
* @emits Chat#HOSTTARGET
* @emits Chat#JOIN
* @emits Chat#MODE
* @emits Chat#NAMES
* @emits Chat#NAMES_END
* @emits Chat#NOTICE
* @emits Chat#NOTICE/ROOM_MODS
* @emits Chat#PART
* @emits Chat#PRIVMSG
* @emits Chat#PRIVMSG/CHEER
* @emits Chat#ROOMSTATE
* @emits Chat#USERNOTICE
* @emits Chat#USERNOTICE/ANON_GIFT_PAID_UPGRADE
* @emits Chat#USERNOTICE/GIFT_PAID_UPGRADE
* @emits Chat#USERNOTICE/RAID
* @emits Chat#USERNOTICE/RESUBSCRIPTION
* @emits Chat#USERNOTICE/RITUAL
* @emits Chat#USERNOTICE/SUBSCRIPTION
* @emits Chat#USERNOTICE/SUBSCRIPTION_GIFT
* @emits Chat#USERSTATE
* @example <caption>Connecting to Twitch and joining #dallas</caption>
* const token = 'cfabdegwdoklmawdzdo98xt2fo512y'
* const username = 'ronni'
* const channel = '#dallas'
* const { chat } = new TwitchJs({ token, username })
* chat.connect().then(globalUserState => {
* // Listen to all messages
* chat.on('*', message => {
* // Do stuff with message ...
* })
* // Listen to PRIVMSG
* chat.on('PRIVMSG', privateMessage => {
* // Do stuff with privateMessage ...
* })
* // Do other stuff ...
* chat.join(channel).then(channelState => {
* // Do stuff with channelState...
* })
* })
*/
declare class Chat {
constructor(options: ChatOptions);
/**
* @type {ChatOptions}
* @public
*/
options: ChatOptions;
/**
* @type {any}
* @public
*/
log: any;
/**
* @type {number}
* @public
*/
_readyState: number;
/**
* @type {number}
* @public
*/
_connectionAttempts: number;
/**
* @type {?GlobalUserState}
* @public
*/
_userState: GlobalUserState;
/**
* @type {ChannelState}
* @public
*/
_channelState: ChannelState;
/**
* @type {?Promise}
* @public
*/
_connectPromise: Promise;
/**
* @function Chat#getOptions
* @public
* @description Retrieves the current [ChatOptions]{@link Chat#ChatOptions}
* @return {ChatOptions} Options of the client
*/
getOptions(): ChatOptions;
/**
* @function Chat#setOptions
* @public
* @description Validates the passed options before changing `_options`
* @param {ChatOptions} options
*/
setOptions(options: ChatOptions): void;
/**
* @function Chat#getReadyState
* @public
* @description Retrieves the current `_readyState` of the client.
* @return {number} Ready state
*/
getReadyState(): number;
/**
* @function Chat#getUserState
* @public
* @description Retrieves the current `_userState` of the client.
* @return {?GlobalUserState} User state tags
*/
getUserState(): GlobalUserState;
/**
* @function Chat#updateOptions
* @public
* @description Updates the clients options after first instantiation.
* @param {ApiOptions} options New client options. To update `token` or `username`, use [**api.reconnect()**]{@link Chat#reconnect}.
*/
updateOptions(options: ApiOptions): void;
/**
* @function Chat#getChannels
* @public
* @description Retrieves all channels the client is connected to.
* @return {Array<string>} Array of channel names
*/
getChannels(): string[];
/**
* @function Chat#getChannelState
* @public
* @description Retrieves and return the internal `_channelState` object.
* @param {string} channel
* @return {ChannelState} Internal `_channelState` object.
*/
getChannelState(channel: string): ChannelState;
/**
* @function Chat#setChannelState
* @public
* @description Sets the state of a specific channel in the client.
* @param {string} channel
* @param {object} state
*/
setChannelState(channel: string, state: object): void;
/**
* @function Chat#removeChannelState
* @public
* @description Removes the state of a specific channel from the client.
* @param {string} channel
* @return {ChannelState} Clients `_channelState` with the requested channel state removed.
*/
removeChannelState(channel: string): ChannelState;
/**
* @function Chat#clearChannelState
* @public
* @description Clears the client `_channelState`.
*/
clearChannelState(): void;
/**
* @function Chat#connect
* @public
* @description Connect to Twitch.
* @return {Promise<?GlobalUserState, string>}
*/
connect(): any;
/**
* @function Chat#send
* @public
* @description Sends a raw message to Twitch.
* @param {string} message - Message to send.
* @return {Promise} Resolves on success, rejects on failure.
*/
send(message: string): Promise;
/**
* @function Chat#disconnect
* @public
* @description Disconnected from Twitch.
*/
disconnect(): void;
/**
* @function Chat#reconnect
* @public
* @description Reconnect to Twitch.
* @param {object} newOptions Provide new options to client.
* @return {Promise<Array<ChannelState>, string>}
*/
reconnect(newOptions: object): any;
/**
* @function Chat#join
* @public
* @description Join a channel.
* @param {string} channel
* @return {Promise<ChannelState, string>}
* @example <caption>Joining #dallas</caption>
* const channel = '#dallas'
* chat.join(channel).then(channelState => {
* // Do stuff with channelState...
* })
* @example <caption>Joining multiple channels</caption>
* const channels = ['#dallas', '#ronni']
* Promise.all(channels.map(channel => chat.join(channel)))
* .then(channelStates => {
* // Listen to all PRIVMSG
* chat.on('PRIVMSG', privateMessage => {
* // Do stuff with privateMessage ...
* })
* // Listen to PRIVMSG from #dallas ONLY
* chat.on('PRIVMSG/#dallas', privateMessage => {
* // Do stuff with privateMessage ...
* })
* // Listen to all PRIVMSG from #ronni ONLY
* chat.on('PRIVMSG/#ronni', privateMessage => {
* // Do stuff with privateMessage ...
* })
* })
*/
join(channel: string): any;
/**
* @function Chat#part
* @public
* @description Depart from a channel.
* @param {string} channel
*/
part(channel: string): void;
/**
* @function Chat#say
* @public
* @description Send a message to a channel.
* @param {string} channel
* @param {string} message
* @return {Promise<?UserStateMessage, string>}
*/
say(channel: string, message: string): any;
/**
* @function Chat#whisper
* @public
* @description Whisper to another user.
* @param {string} user
* @param {string} message
* @return {Promise<undefined>}
*/
whisper(user: string, message: string): any;
/**
* @function Chat#broadcast
* @public
* @description Broadcast message to all connected channels.
* @param {string} message
* @return {Promise<Array<UserStateMessage>>}
*/
broadcast(message: string): any;
/**
* @function Chat#emit
* @public
* @param {string} eventName
* @param {string} message
*/
emit(eventName: string, message: string): void;
/**
* @function Chat#isUserAuthenticated
* @public
* @description Ensure the user is authenticated.
* @return {Promise}
*/
isUserAuthenticated(): Promise;
}
/**
* @function handleConnectSuccess
* @public
* @param {GlobalUserState} globalUserState
*/
declare function handleConnectSuccess(globalUserState: GlobalUserState): void;
/**
* @function handleConnectRetry
* @public
* @param {Error} error
* @return {Promise<?GlobalUserState, string>}
*/
declare function handleConnectRetry(error: Error): any;
/**
* @function handleMessage
* @public
* @param {object} baseMessage
*/
declare function handleMessage(baseMessage: object): void;
/**
* @function handleDisconnect
* @public
*/
declare function handleDisconnect(): void;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment