UNPKG

twitch-js

Version:

Javascript library for the Twitch Messaging Interface.

3 lines (2 loc) 37.5 kB
import t from"eventemitter3";import e from"lodash/get";import n from"lodash/toLower";import r from"lodash/uniq";import o from"pino";import{stringify as i}from"qs";import s from"ws";import a from"p-queue";import{parse as c}from"tekko/dist/parse";import u from"camelcase-keys";import _ from"lodash/gt";import O from"lodash/isEmpty";import E from"lodash/isFinite";import S from"lodash/toNumber";import T from"lodash/toUpper";import m from"lodash/camelCase";import N from"lodash/replace";import l from"invariant";import d from"lodash/conformsTo";import f from"lodash/defaults";import p from"lodash/isString";import h from"lodash/isFunction";import A from"lodash/isBoolean";import I from"lodash/isNil";import R from"lodash/random";import C from"lodash/includes";import v from"node-fetch";import D from"form-data";import g from"lodash/isUndefined";var U=function(t,e){return(U=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function y(t,e){function n(){this.constructor=t}U(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var L,M,b,P,F,w,G,H,B,W,Y=function(){return(Y=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function j(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function k(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{c(r.next(t))}catch(t){i(t)}}function a(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){t.done?o(t.value):new n((function(e){e(t.value)})).then(s,a)}c((r=r.apply(t,e||[])).next())}))}function V(t,e){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,r=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=(o=s.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=e.call(t,s)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}function x(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;var r=Array(t),o=0;for(e=0;e<n;e++)for(var i=arguments[e],s=0,a=i.length;s<a;s++,o++)r[o]=i[s];return r}!function(t){t.Helix="helix",t.Kraken="kraken"}(L||(L={})),function(t){t.tags="twitch.tv/tags",t.commands="twitch.tv/commands",t.membership="twitch.tv/membership"}(M||(M={})),function(t){t.JOIN="JOIN",t.MODE="MODE",t.PART="PART",t.NAMES="353",t.NAMES_END="366"}(b||(b={})),function(t){t.CLEAR_CHAT="CLEARCHAT",t.GLOBAL_USER_STATE="GLOBALUSERSTATE",t.PRIVATE_MESSAGE="PRIVMSG",t.ROOM_STATE="ROOMSTATE",t.USER_NOTICE="USERNOTICE",t.USER_STATE="USERSTATE"}(P||(P={})),function(t){t.WELCOME="001",t.PING="PING",t.PONG="PONG",t.WHISPER="PRIVMSG #jtv"}(F||(F={})),function(t){t.CLEAR_CHAT="CLEARCHAT",t.HOST_TARGET="HOSTTARGET",t.NOTICE="NOTICE",t.RECONNECT="RECONNECT",t.ROOM_STATE="ROOMSTATE",t.USER_NOTICE="USERNOTICE",t.USER_STATE="USERSTATE"}(w||(w={})),function(t){t.WELCOME="001",t.PING="PING",t.PONG="PONG",t.RECONNECT="RECONNECT",t.WHISPER="PRIVMSG #jtv",t.JOIN="JOIN",t.MODE="MODE",t.PART="PART",t.NAMES="353",t.NAMES_END="366",t.CLEAR_CHAT="CLEARCHAT",t.GLOBAL_USER_STATE="GLOBALUSERSTATE",t.HOST_TARGET="HOSTTARGET",t.NOTICE="NOTICE",t.PRIVATE_MESSAGE="PRIVMSG",t.ROOM_STATE="ROOMSTATE",t.USER_NOTICE="USERNOTICE",t.USER_STATE="USERSTATE"}(G||(G={})),function(t){t.RAW="RAW",t.ALL="*",t.CONNECTED="CONNECTED",t.DISCONNECTED="DISCONNECTED",t.RECONNECT="RECONNECT",t.AUTHENTICATION_FAILED="AUTHENTICATION_FAILED",t.ERROR_ENCOUNTERED="ERROR_ENCOUNTERED",t.PARSE_ERROR_ENCOUNTERED="PARSE_ERROR_ENCOUNTERED",t.ANON_GIFT_PAID_UPGRADE="ANON_GIFT_PAID_UPGRADE",t.GIFT_PAID_UPGRADE="GIFT_PAID_UPGRADE",t.RAID="RAID",t.RESUBSCRIPTION="RESUBSCRIPTION",t.RITUAL="RITUAL",t.SUBSCRIPTION="SUBSCRIPTION",t.SUBSCRIPTION_GIFT="SUBSCRIPTION_GIFT",t.SUBSCRIPTION_GIFT_COMMUNITY="SUBSCRIPTION_GIFT_COMMUNITY",t.ROOM_MODS="ROOM_MODS",t.MOD_GAINED="MOD_GAINED",t.MOD_LOST="MOD_LOST",t.USER_BANNED="USER_BANNED",t.CHEER="CHEER",t.HOST_ON="HOST_ON",t.HOST_OFF="HOST_OFF",t.HOSTED="HOSTED",t.HOSTED_WITHOUT_VIEWERS="HOSTED/WITHOUT_VIEWERS",t.HOSTED_WITH_VIEWERS="HOSTED/WITH_VIEWERS",t.HOSTED_AUTO="HOSTED/AUTO"}(H||(H={})),function(t){t.BAN="ban",t.CLEAR="clear",t.COLOR="color",t.COMMERCIAL="commercial",t.EMOTE_ONLY="emoteonly",t.EMOTE_ONLY_OFF="emoteonlyoff",t.FOLLOWERS_ONLY="followers",t.FOLLOWERS_ONLY_OFF="followersoff",t.HELP="help",t.HOST="host",t.MARKER="marker",t.ME="me",t.MOD="mod",t.MODS="mods",t.R9K="r9kbeta",t.R9K_OFF="r9kbetaoff",t.RAID="raid",t.SLOW="slow",t.SLOW_OFF="slowoff",t.SUBSCRIBERS="subscribers",t.SUBSCRIBERS_OFF="subscribersoff",t.TIMEOUT="timeout",t.UNBAN="unban",t.UNHOST="unhost",t.UNMOD="unmod",t.UNRAID="unraid"}(B||(B={})),function(t){t.ALREADY_BANNED="already_banned",t.ALREADY_EMOTE_ONLY_OFF="already_emote_only_off",t.ALREADY_EMOTE_ONLY_ON="already_emote_only_on",t.ALREADY_R9K_OFF="already_r9k_off",t.ALREADY_R9K_ON="already_r9k_on",t.ALREADY_SUBS_OFF="already_subs_off",t.ALREADY_SUBS_ON="already_subs_on",t.BAD_HOST_HOSTING="bad_host_hosting",t.BAD_MOD_MOD="bad_mod_mod",t.BAN_SUCCESS="ban_success",t.BAD_UNBAN_NO_BAN="bad_unban_no_ban",t.COLOR_CHANGED="color_changed",t.CMDS_AVAILABLE="cmds_available",t.COMMERCIAL_SUCCESS="commercial_success",t.EMOTE_ONLY_OFF="emote_only_off",t.EMOTE_ONLY_ON="emote_only_on",t.FOLLOWERS_OFF="followers_off",t.FOLLOWERS_ON="followers_on",t.FOLLOWERS_ON_ZERO="followers_on_zero",t.HOST_OFF="host_off",t.HOST_ON="host_on",t.HOSTS_REMAINING="hosts_remaining",t.MSG_CHANNEL_SUSPENDED="msg_channel_suspended",t.MOD_SUCCESS="mod_success",t.R9K_OFF="r9k_off",t.R9K_ON="r9k_on",t.ROOM_MODS="room_mods",t.SLOW_OFF="slow_off",t.SLOW_ON="slow_on",t.SUBS_OFF="subs_off",t.SUBS_ON="subs_on",t.TIMEOUT_SUCCESS="timeout_success",t.UNBAN_SUCCESS="unban_success",t.UNRAID_SUCCESS="unraid_success",t.UNRECOGNIZED_CMD="unrecognized_cmd"}(W||(W={}));var K,J,z=Object.keys(W).reduce((function(t,e){var n;return Y(Y({},t),((n={})[e]=e,n[G.NOTICE+"/"+e]=e,n))}),{});!function(t){t.CHEER="CHEER",t.HOSTED_WITHOUT_VIEWERS="HOSTED_WITHOUT_VIEWERS",t.HOSTED_WITH_VIEWERS="HOSTED_WITH_VIEWERS",t.HOSTED_AUTO="HOSTED_AUTO"}(K||(K={})),function(t){t.ANON_GIFT_PAID_UPGRADE="anongiftpaidupgrade",t.GIFT_PAID_UPGRADE="giftpaidupgrade",t.RAID="raid",t.RESUBSCRIPTION="resub",t.RITUAL="ritual",t.SUBSCRIPTION="sub",t.SUBSCRIPTION_GIFT="subgift",t.SUBSCRIPTION_GIFT_COMMUNITY="submysterygift"}(J||(J={}));var Z,q,Q=Object.keys(J).reduce((function(t,e){var n;return Y(Y({},t),((n={})[e]=e,n[G.USER_NOTICE+"/"+e]=e,n))}),{}),$=Y(Y(Y(Y(Y(Y(Y(Y({},b),P),F),w),H),z),K),Q);!function(t){t[t.admin=0]="admin",t[t.broadcaster=1]="broadcaster",t[t.globalMod=2]="globalMod",t[t.moderator=3]="moderator",t[t.partner=4]="partner",t[t.premium=5]="premium",t[t.staff=6]="staff",t[t.subGifter=7]="subGifter",t[t.turbo=8]="turbo",t[t.vip=9]="vip"}(Z||(Z={})),function(t){t[t.bits=0]="bits",t[t.bitsLeader=1]="bitsLeader",t[t.subscriber=2]="subscriber"}(q||(q={}));var X,tt,et,nt,rt,ot=function(t){void 0===t&&(t={});var e=t.name,n=j(t,["name"]),r=["TwitchJS"].concat(e||[]).join("/"),i=o(Y({name:r,prettyPrint:!0,level:"info"},n));return i.profile=function(t){var e=Date.now();return t&&i.info(t),{done:function(t,n){var r=t+" ("+(Date.now()-e)+"ms)";n?i.error(r,n):i.info(r)}}},i},it=function(t,e){return new Promise((function(n){return t.once(e,n)}))},st=function(t){return t.reduce((function(t,e){return t.then(e)}),Promise.resolve())},at=function(t,e){return new Promise((function(n,r){return setTimeout(r,t,e)}))},ct="ERROR: connect timed out",ut="ERROR: join timed out",_t=new RegExp("^msgParam(\\w+)"),Ot=/:.+@jtv\.tmi\.twitch\.tv PRIVMSG #?(\w+) :(\w+) is now (?:(auto) )?hosting[A-z ]+(\d+)?/,Et=new RegExp("^justinfan(\\d+)$"),St=function(t){return void 0!==t&&t.command===G.NOTICE&&""===t.channel&&"Login authentication failed"===t.message},Tt=function(t){return Et.test(t)},mt=function(t){var e=this;void 0===t&&(t={}),this.push=function(t){var n=t.fn,r=t.priority,o=void 0===r?100:r;return e._q.add(n,{priority:o})},this._q=new a(Y({intervalCap:20,interval:3e4,carryoverConcurrencyCount:!0,concurrency:1},t))},Nt=function(t){return"string"==typeof t?N(t,/\\[sn]/g," "):void 0},lt=function(t){var e=parseInt(t,10);return E(e)?e:void 0},dt=function(t){return"1"===t},ft=function(t){var e=new Date(parseInt(t,10));return"Invalid Date"!==e.toString()?e:new Date},pt=function(t){return Object.entries(t).reduce((function(t,e){var n,r,o,i,s,a,c=e[0],u=e[1];switch(c){case"followersOnly":return Y(Y({},t),((n={})[c]=0===(a=parseInt(u,10))||a>0&&a,n));case"broadcasterLang":return Y(Y({},t),((r={})[c]=Nt(u),r));case"slow":return Y(Y({},t),((o={})[c]=lt(u),o));case"emoteOnly":case"r9k":case"subsOnly":return Y(Y({},t),((i={})[c]=dt(u),i));default:return Y(Y({},t),((s={})[c]=u,s))}}),{})},ht=function(t){return Y(Y({},t),{badges:(i=t.badges,"string"==typeof i?i.split(",").reduce((function(t,e){var n,r,o,i=e.split("/"),s=i[0],a=i[1],c=m(s);return"boolean"===Z[c]?Y(Y({},t),((n={})[c]=dt(a),n)):"number"===q[c]?Y(Y({},t),((r={})[c]=parseInt(a,10),r)):Y(Y({},t),((o={})[c]=a,o))}),{}):{}),bits:lt(t.bits),color:t.color,displayName:t.displayName,emotes:(o=t.emotes,"string"!=typeof o?[]:o.split("/").reduce((function(t,e){var n=e.split(":"),r=n[0],o=n[1];return r?x(t,o.split(",").map((function(t){var e=t.split("-"),n=e[0],o=e[1];return{id:r,start:parseInt(n,10),end:parseInt(o,10)}}))):t}),[])),emoteSets:(r=t.emoteSets,"string"==typeof r?r.split(","):[]),userType:(e=t.userType,"string"==typeof e?e:void 0),username:t.displayName?n(t.displayName):void 0});var e,r,o,i},At=function(t){return Y(Y({},t),ht(t))},It=ht,Rt=function(t,e){return t.split(/\r?\n/g).reduce((function(t,r){if(!r.length)return t;var o=c(r),i=o.command,s=o.tags,a=void 0===s?{}:s,_=o.prefix,E=void 0===_?{name:void 0,user:void 0,host:void 0}:_,S=E.name,T=E.user,m=E.host,N=o.params,l=N[0],d=N[1],f=String(a["tmi-sent-ts"])||Date.now().toString(),p=O(a)?{}:u(a),h=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return t.reduce((function(t,e){return"string"!=typeof e?t:"tmi.twitch.tv"===e?"tmi.twitch.tv":n(e).split(".")[0]}),void 0)}(m,S,T,p.login,p.username,p.displayName);return x(t,[{_raw:r,timestamp:ft(f),command:i,event:i,channel:"*"!==l?l:"",username:h,isSelf:"string"==typeof h&&n(e)===h,tags:p,message:d}])}),[])},Ct=function(t){var e=t.tags,n=j(t,["tags"]);return Y(Y({},n),{command:G.GLOBAL_USER_STATE,event:G.GLOBAL_USER_STATE,tags:At(e)})},vt=function(t){var e=t.tags,n=j(t,["tags"]);return Y(Y({},n),{command:G.USER_STATE,event:G.USER_STATE,tags:ht(e)})},Dt=function(t){var e=G.USER_NOTICE,n=Y(Y({},It(t.tags)),{systemMsg:Nt(t.tags.systemMsg)}),r=Nt(t.tags.systemMsg),o=function(t){return Object.entries(t).reduce((function(t,e){var n,r,o=e[0],i=e[1],s=(_t.exec(o)||[])[1];switch(s){case"Months":case"MassGiftCount":case"PromoGiftTotal":case"SenderCount":case"ViewerCount":return Y(Y({},t),((n={})[m(s)]=lt(i),n));case void 0:return t;default:return Y(Y({},t),((r={})[m(s)]=Nt(i),r))}}),{})}(n);switch(n.msgId){case J.ANON_GIFT_PAID_UPGRADE:return Y(Y({},t),{command:e,event:H.ANON_GIFT_PAID_UPGRADE,parameters:o,tags:n,systemMessage:r});case J.GIFT_PAID_UPGRADE:return Y(Y({},t),{command:e,event:H.GIFT_PAID_UPGRADE,parameters:o,tags:n,systemMessage:r});case J.RAID:return Y(Y({},t),{command:e,event:H.RAID,parameters:o,tags:n,systemMessage:r});case J.RESUBSCRIPTION:return Y(Y({},t),{command:e,event:H.RESUBSCRIPTION,parameters:o,tags:n,systemMessage:r});case J.RITUAL:return Y(Y({},t),{command:e,event:H.RITUAL,parameters:o,tags:n,systemMessage:r});case J.SUBSCRIPTION_GIFT_COMMUNITY:return Y(Y({},t),{command:e,event:H.SUBSCRIPTION_GIFT_COMMUNITY,parameters:o,tags:n,systemMessage:r});case J.SUBSCRIPTION_GIFT:return Y(Y({},t),{command:e,event:H.SUBSCRIPTION_GIFT,parameters:o,tags:n,systemMessage:r});case J.SUBSCRIPTION:return Y(Y({},t),{command:e,event:H.SUBSCRIPTION,parameters:o,tags:n,systemMessage:r});default:return Y(Y({},t),{command:e,event:T(n.msgId),tags:n,parameters:o,systemMessage:r})}},gt=function(t){return null==t?"#":t.startsWith("#")?t:n("#"+t)},Ut=function(t){return null==t?"TWITCHJS":t.startsWith("oauth:")?t:"oauth:"+t},yt=function(t){return O(t)||"justinfan"===t?"justinfan"+R(8e4,81e3):t},Lt=function(t){var e={username:p,token:p,server:p,port:E,ssl:A,isKnown:A,isVerified:A},n=f(Y(Y({},t),{username:yt(t.username),token:Ut(t.token)}),{server:"irc-ws.chat.twitch.tv",port:443,ssl:!0,isKnown:!1,isVerified:!1});return l(d(n,e),"[twitch-js/Chat/Client] options: Expected valid options"),n},Mt=function(t){function e(n){var r=t.call(this,n)||this;return r.timestamp=new Date,Object.setPrototypeOf(r,e.prototype),Error.captureStackTrace&&Error.captureStackTrace(r,e),r.message="[TwitchJS] "+n,r}return y(e,t),e}(Error),bt=function(t){function e(n,r){var o=this,i=n instanceof Error?n.message:n;return o=t.call(this,i+" [Chat]")||this,Object.setPrototypeOf(o,e.prototype),void 0!==r&&"string"!=typeof r&&(o.command=r.command),o}return y(e,t),e}(Mt),Pt=function(t){function e(n,r){var o=t.call(this,"Authentication error encountered",r)||this;return Object.setPrototypeOf(o,e.prototype),Object.assign(o,n),Object.assign(o,r),o}return y(e,t),e}(bt),Ft=function(t){function e(n,r){var o=t.call(this,"Parse error encountered")||this;return Object.setPrototypeOf(o,e.prototype),Object.assign(o,n),o._raw=r,o.command=H.PARSE_ERROR_ENCOUNTERED,o}return y(e,t),e}(bt),wt=(function(t){function e(n){void 0===n&&(n="Error: join");var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}y(e,t)}(bt),function(t){function e(n){void 0===n&&(n="Error: timeout");var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}return y(e,t),e}(bt)),Gt=function(t){function n(n){var r=t.call(this)||this;r.isReady=function(){return 1===e(r,"_ws.readyState")},r.send=function(t,e){var n=void 0===e?{}:e,o=n.priority,i=void 0===o?1:o,s=n.isModerator,a=void 0!==s&&s;return k(r,void 0,void 0,(function(){var e;return V(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),e=this._ws.send.bind(this._ws,t),[4,(a?this._moderatorQueue:this._queue).push({fn:e,priority:i})];case 1:return n.sent(),this._log.debug("<",t),[3,3];case 2:return n.sent(),this._log.error("<",t),[3,3];case 3:return[2]}}))}))},r.disconnect=function(){r._handleKeepAliveReset(),r._ws.close()},r._options=Lt(n);var o=r._options,i=o.ssl,a=o.server,c=o.port,u=o.log;r._log=ot(Y({name:"Chat/Client"},u));var _=i?"wss":"ws";return r._ws=new s(_+"://"+a+":"+c),r._ws.onopen=r._handleOpen.bind(r),r._ws.onmessage=r._handleMessage.bind(r),r._ws.onerror=r._handleError.bind(r),r._ws.onclose=r._handleClose.bind(r),r._queue=r._createQueue(r._options),r._moderatorQueue=r._options.isVerified?r._queue:r._createQueue({isModerator:!0}),r}return y(n,t),n.prototype._createQueue=function(t){var e=t.isModerator,n=void 0!==e&&e,r=t.isVerified,o=void 0!==r&&r,i=t.isKnown,s=void 0!==i&&i;return n?new mt({intervalCap:100}):o?new mt({intervalCap:7500}):s?new mt({intervalCap:50}):new mt},n.prototype._isUserAnonymous=function(){return Tt(e(this,"_options.username"))},n.prototype._handleOpen=function(){this.send("CAP REQ :"+Object.values(M).join(" "),{priority:100});var t=this._options,e=t.token,n=t.username;this.send("PASS "+e,{priority:100}),this.send("NICK "+n,{priority:100})},n.prototype._handleMessage=function(t){var e=this,n=t.data.toString();try{this._handleKeepAlive(),Rt(n,this._options.username).forEach((function(t){var n=t.command||"";e._log.debug("> %s %s",n,JSON.stringify(Y(Y({},t),{_raw:void 0}))),St(t)?(e.emit(H.AUTHENTICATION_FAILED,Y(Y({},t),{event:H.AUTHENTICATION_FAILED})),e.disconnect()):(t.command===G.PING&&e.send("PONG :tmi.twitch.tv",{priority:100}),e._isUserAnonymous()?t.command===G.WELCOME&&e.emit(H.CONNECTED,Y(Y({},t),{event:H.CONNECTED})):t.command===G.GLOBAL_USER_STATE&&e.emit(H.CONNECTED,Y(Y({},t),{event:H.CONNECTED})),t.command===G.RECONNECT&&e.emit(H.RECONNECT,Y(Y({},t),{event:H.RECONNECT}))),e.emit(H.ALL,t)}))}catch(t){var r=i({title:"Parsing error encountered",body:n});this._log.error("Parsing error encountered. Please create an issue: %s","https://github.com/twitch-js/twitch-js/issues/new?"+r,t);var o=new Ft(t,n);throw this.emit(o.command,o),this.emit(H.ALL,o),o}finally{var s={_raw:n,timestamp:new Date};this.emit(H.RAW,s)}},n.prototype._handleError=function(t){var e={timestamp:new Date,event:H.ERROR_ENCOUNTERED,messageEvent:t};this.emit(H.ERROR_ENCOUNTERED,e),this.emit(H.ALL,e)},n.prototype._handleClose=function(t){var e={timestamp:new Date,event:H.DISCONNECTED,messageEvent:t};this.emit(H.DISCONNECTED,e),this.emit(H.ALL,e)},n.prototype._handleKeepAlive=function(){var t=this;this._handleKeepAliveReset(),this.isReady()&&(this._pingTimeoutId=setTimeout((function(){return t.send(G.PING,{priority:100})}),15e4)),this._reconnectTimeoutId=setTimeout((function(){return t.emit(H.RECONNECT,{})}),2e5)},n.prototype._handleKeepAliveReset=function(){clearTimeout(this._pingTimeoutId),clearTimeout(this._reconnectTimeoutId),this._pingTimeoutId=void 0,this._reconnectTimeoutId=void 0},n}(t),Ht=function(t){Object.entries(B).forEach((function(e){var n=e[0],r=e[1];t[m(n)]=function(e){for(var n=[],o=1;o<arguments.length;o++)n[o-1]=arguments[o];return t.say.apply(t,x([e,"/"+r],n))}}))},Bt=function(t){return function(e,n){var r=(/^\/(.+)/.exec(n)||[])[1],o=Object.entries(W).reduce((function(t,e){var n,r=e[0],o=e[1];return Y(Y({},t),((n={})[r]=T(o),n))}),{});switch(r){case B.BAN:return[it(t,o.BAN_SUCCESS+"/"+e),it(t,o.ALREADY_BANNED+"/"+e)];case B.CLEAR:return[it(t,G.CLEAR_CHAT+"/"+e)];case B.COLOR:return[it(t,o.COLOR_CHANGED+"/"+e)];case B.COMMERCIAL:return[it(t,o.COMMERCIAL_SUCCESS+"/"+e)];case B.EMOTE_ONLY:return[it(t,o.EMOTE_ONLY_ON+"/"+e),it(t,o.ALREADY_EMOTE_ONLY_ON+"/"+e)];case B.EMOTE_ONLY_OFF:return[it(t,o.EMOTE_ONLY_OFF+"/"+e),it(t,o.ALREADY_EMOTE_ONLY_OFF+"/"+e)];case B.FOLLOWERS_ONLY:return[it(t,o.FOLLOWERS_ON_ZERO+"/"+e),it(t,o.FOLLOWERS_ON+"/"+e)];case B.FOLLOWERS_ONLY_OFF:return[it(t,o.FOLLOWERS_OFF+"/"+e)];case B.HELP:return[it(t,o.CMDS_AVAILABLE+"/"+e)];case B.HOST:return[it(t,o.HOST_ON+"/"+e)];case B.MARKER:return[Promise.resolve()];case B.MOD:return[it(t,o.MOD_SUCCESS+"/"+e),it(t,o.BAD_MOD_MOD+"/"+e)];case B.MODS:return[it(t,o.ROOM_MODS+"/"+e)];case B.R9K:return[it(t,o.R9K_ON+"/"+e),it(t,o.ALREADY_R9K_ON+"/"+e)];case B.R9K_OFF:return[it(t,o.R9K_OFF+"/"+e),it(t,o.ALREADY_R9K_OFF+"/"+e)];case B.RAID:return[Promise.resolve()];case B.SLOW:return[it(t,o.SLOW_ON+"/"+e)];case B.SLOW_OFF:return[it(t,o.SLOW_OFF+"/"+e)];case B.SUBSCRIBERS:return[it(t,o.SUBS_ON+"/"+e),it(t,o.ALREADY_SUBS_ON+"/"+e)];case B.SUBSCRIBERS_OFF:return[it(t,o.SUBS_OFF+"/"+e),it(t,o.ALREADY_SUBS_OFF+"/"+e)];case B.TIMEOUT:return[it(t,o.TIMEOUT_SUCCESS+"/"+e)];case B.UNBAN:return[it(t,o.UNBAN_SUCCESS+"/"+e),it(t,o.BAD_UNBAN_NO_BAN+"/"+e)];case B.UNHOST:case B.UNMOD:return[it(t,o.HOST_OFF+"/"+e)];case B.UNRAID:return[it(t,o.UNRAID_SUCCESS+"/"+e)];default:return[it(t,G.USER_STATE+"/"+e)]}}};!function(t){t[t.NOT_READY=0]="NOT_READY",t[t.CONNECTING=1]="CONNECTING",t[t.RECONNECTING=2]="RECONNECTING",t[t.CONNECTED=3]="CONNECTED",t[t.DISCONNECTING=4]="DISCONNECTING",t[t.DISCONNECTED=5]="DISCONNECTED"}(X||(X={})),function(t){t.ALREADY_BANNED="NOTICE/ALREADY_BANNED",t.ALREADY_EMOTE_ONLY_OFF="NOTICE/ALREADY_EMOTE_ONLY_OFF",t.ALREADY_EMOTE_ONLY_ON="NOTICE/ALREADY_EMOTE_ONLY_ON",t.ALREADY_R9K_OFF="NOTICE/ALREADY_R9K_OFF",t.ALREADY_R9K_ON="NOTICE/ALREADY_R9K_ON",t.ALREADY_SUBS_OFF="NOTICE/ALREADY_SUBS_OFF",t.ALREADY_SUBS_ON="NOTICE/ALREADY_SUBS_ON",t.BAD_HOST_HOSTING="NOTICE/BAD_HOST_HOSTING",t.BAD_MOD_MOD="NOTICE/BAD_MOD_MOD",t.BAN_SUCCESS="NOTICE/BAN_SUCCESS",t.BAD_UNBAN_NO_BAN="NOTICE/BAD_UNBAN_NO_BAN",t.COLOR_CHANGED="NOTICE/COLOR_CHANGED",t.CMDS_AVAILABLE="NOTICE/CMDS_AVAILABLE",t.COMMERCIAL_SUCCESS="NOTICE/COMMERCIAL_SUCCESS",t.EMOTE_ONLY_OFF="NOTICE/EMOTE_ONLY_OFF",t.EMOTE_ONLY_ON="NOTICE/EMOTE_ONLY_ON",t.FOLLOWERS_OFF="NOTICE/FOLLOWERS_OFF",t.FOLLOWERS_ON="NOTICE/FOLLOWERS_ON",t.FOLLOWERS_ON_ZERO="NOTICE/FOLLOWERS_ON_ZERO",t.HOST_OFF="NOTICE/HOST_OFF",t.HOST_ON="NOTICE/HOST_ON",t.HOSTS_REMAINING="NOTICE/HOSTS_REMAINING",t.MSG_CHANNEL_SUSPENDED="NOTICE/MSG_CHANNEL_SUSPENDED",t.MOD_SUCCESS="NOTICE/MOD_SUCCESS",t.R9K_OFF="NOTICE/R9K_OFF",t.R9K_ON="NOTICE/R9K_ON",t.ROOM_MODS="NOTICE/ROOM_MODS",t.SLOW_OFF="NOTICE/SLOW_OFF",t.SLOW_ON="NOTICE/SLOW_ON",t.SUBS_OFF="NOTICE/SUBS_OFF",t.SUBS_ON="NOTICE/SUBS_ON",t.TIMEOUT_SUCCESS="NOTICE/TIMEOUT_SUCCESS",t.UNBAN_SUCCESS="NOTICE/UNBAN_SUCCESS",t.UNRAID_SUCCESS="NOTICE/UNRAID_SUCCESS",t.UNRECOGNIZED_CMD="NOTICE/UNRECOGNIZED_CMD"}(tt||(tt={})),function(t){t.CHEER="PRIVMSG/CHEER",t.HOSTED_WITHOUT_VIEWERS="PRIVMSG/HOSTED_WITHOUT_VIEWERS",t.HOSTED_WITH_VIEWERS="PRIVMSG/HOSTED_WITH_VIEWERS",t.HOSTED_AUTO="PRIVMSG/HOSTED_AUTO"}(et||(et={})),function(t){t.ANON_GIFT_PAID_UPGRADE="USERNOTICE/ANON_GIFT_PAID_UPGRADE",t.GIFT_PAID_UPGRADE="USERNOTICE/GIFT_PAID_UPGRADE",t.RAID="USERNOTICE/RAID",t.RESUBSCRIPTION="USERNOTICE/RESUBSCRIPTION",t.RITUAL="USERNOTICE/RITUAL",t.SUBSCRIPTION="USERNOTICE/SUBSCRIPTION",t.SUBSCRIPTION_GIFT="USERNOTICE/SUBSCRIPTION_GIFT",t.SUBSCRIPTION_GIFT_COMMUNITY="USERNOTICE/SUBSCRIPTION_GIFT_COMMUNITY"}(nt||(nt={}));var Wt,Yt,jt=function(t){function o(n){var r=t.call(this)||this;return r._readyState=X.NOT_READY,r._connectionAttempts=0,r._channelState={},r._isDisconnecting=!1,r.connect=function(){return r._isDisconnecting=!1,r._connectionInProgress?r._connectionInProgress:(r._connectionInProgress=Promise.race([at(r.options.connectionTimeout,new wt(ct)),r._handleConnectionAttempt()]).then(r._handleConnectSuccess.bind(r)).catch(r._handleConnectRetry.bind(r)),r._connectionInProgress)},r.send=function(t,e){return r._client.send(t,e)},r.disconnect=function(){r._isDisconnecting=!0,r._readyState=X.DISCONNECTING,r._clearChannelState(),r._client.disconnect()},r.reconnect=function(t){t&&(r.options=Y(Y({},r.options),t)),r._connectionInProgress=null,r._readyState=X.RECONNECTING;var e=r._getChannels();return r.disconnect(),r.connect().then((function(){return Promise.all(e.map((function(t){return r.join(t)})))}))},r.join=function(t){var e=gt(t),n=r._log.profile("Joining "+e),o=r.connect(),i=it(r,G.ROOM_STATE+"/"+e),s=Tt(r.options.username)?Promise.resolve():it(r,G.USER_STATE+"/"+e),a=Promise.all([o,i,s]).then((function(t){var o=t[1],i=t[2],s={roomState:o.tags,userState:i?i.tags:null};return r._setChannelState(o.channel,s),n.done("Joined "+e),s}));return r.send(G.JOIN+" "+e).then((function(){return Promise.race([at(r.options.joinTimeout,new wt(ut)),a])}))},r.part=function(t){var e=gt(t);r._log.info("Parting "+e),r._removeChannelState(e),r.send(G.PART+" "+e)},r.say=function(t,n){for(var o=[],i=2;i<arguments.length;i++)o[i-2]=arguments[i];var s=gt(t),a=o.length?x([""],o).join(" "):"",c="PRIVMSG/"+s+" :"+n+a,u=e(r,["_channelState",s,"isModerator"]),_=Bt(r)(s,n),O=function(){return Promise.race(x(_))};return st([r._isUserAuthenticated.bind(r),r.send.bind(r,G.PRIVATE_MESSAGE+" "+s+" :"+n+a,{isModerator:u}),O]).then((function(t){return r._log.info(c),t})).catch((function(t){throw r._log.error(c,t),t}))},r.whisper=function(t,e){return st([r._isUserAuthenticated.bind(r),r.send.bind(r,G.WHISPER+" :/w "+t+" "+e)])},r.broadcast=function(t){return st([r._isUserAuthenticated.bind(r),function(){return Promise.all(r._getChannels().map((function(e){return r.say(e,t)})))}])},r.options=n,r._log=ot(Y({name:"Chat"},r.options.log)),Object.assign(r,Ht(r)),r}return y(o,t),Object.defineProperty(o.prototype,"options",{get:function(){return this._options},set:function(t){this._options=function(t){var e={username:p,token:function(t){return I(t)||p(t)},isKnown:A,isVerified:A,connectionTimeout:E,joinTimeout:E,onAuthenticationFailure:h},n=f(Y(Y({},t),{username:yt(t.username),token:Ut(t.token)}),{isKnown:!1,isVerified:!1,connectionTimeout:5e3,joinTimeout:1e3,onAuthenticationFailure:function(){return Promise.reject()}});return l(d(n,e),"[twitch-js/Chat] options: Expected valid options"),n}(t)},enumerable:!0,configurable:!0}),o.prototype.updateOptions=function(t){var e=this.options,n=e.token,r=e.username;this.options=Y(Y({},t),{token:n,username:r})},o.prototype._handleConnectionAttempt=function(){var t=this;return new Promise((function(e,n){var r=t._log.profile("Connecting ...");t._readyState=X.CONNECTING,t._connectionAttempts+=1,t._client&&t._client.removeAllListeners(),t._client=new Gt(t.options),t._client.on($.ALL,t._handleMessage,t),t._client.on($.DISCONNECTED,t._handleDisconnect,t),t._client.once($.RECONNECT,(function(){return t.reconnect()})),t._client.once($.AUTHENTICATION_FAILED,n),t._client.once($.CONNECTED,(function(n){t._handleJoinsAfterConnect(),r.done("Connected"),e(n)}))}))},o.prototype._handleConnectSuccess=function(t){return this._readyState=X.CONNECTED,this._connectionAttempts=0,Ct(t)},o.prototype._handleJoinsAfterConnect=function(){return k(this,void 0,void 0,(function(){var t,e=this;return V(this,(function(n){switch(n.label){case 0:return t=this._getChannels(),[4,Promise.all(t.map((function(t){return e.join(t)})))];case 1:return n.sent(),[2]}}))}))},o.prototype._handleConnectRetry=function(t){return k(this,void 0,void 0,(function(){var e,n;return V(this,(function(r){switch(r.label){case 0:if(this._connectionInProgress=null,this._isDisconnecting)return[2,Promise.resolve()];if(this._readyState=X.CONNECTING,this._log.info("Retrying ..."),t.event!==$.AUTHENTICATION_FAILED)return[3,6];r.label=1;case 1:return r.trys.push([1,5,,6]),[4,this.options.onAuthenticationFailure()];case 2:return(e=r.sent())?(this.options=Y(Y({},this.options),{token:e}),[4,(o=this.options.connectionTimeout,new Promise((function(t){return setTimeout(t,o)})))]):[3,4];case 3:return r.sent(),[2,this.connect()];case 4:return[3,6];case 5:throw n=r.sent(),this._log.error("Connection failed"),new Pt(n,t);case 6:return[2,this.connect()]}var o}))}))},o.prototype._isUserAuthenticated=function(){var t=this;return new Promise((function(e,n){Tt(t.options.username)?n(new Error("Not authenticated")):e()}))},o.prototype._emit=function(n,o){var i=this;if(n){var s=r(n.split("/")),a=e(o,"tags.displayName")||e(o,"username")||"",c=e(o,"message")||"";this._log.info(""+s.join("/"),a+(c?":":""),c),s.filter((function(t){return"#"!==t})).reduce((function(e,n){var r=x(e,[n]);return r.length>1&&t.prototype.emit.call(i,n,o),t.prototype.emit.call(i,r.join("/"),o),r}),[])}t.prototype.emit.call(this,$.ALL,o)},o.prototype._getChannels=function(){return Object.keys(this._channelState)},o.prototype._getChannelState=function(t){return this._channelState[t]},o.prototype._setChannelState=function(t,e){this._channelState[t]=e},o.prototype._removeChannelState=function(t){this._channelState=Object.entries(this._channelState).reduce((function(e,n){var r,o=n[0],i=n[1];return o===t?e:Y(Y({},e),((r={})[o]=i,r))}),{})},o.prototype._clearChannelState=function(){this._channelState={}},o.prototype._handleMessage=function(t){var e=gt(t.channel),r=t,o=r.command,i=r;switch(r.command){case $.JOIN:o=(i=function(t){var e=/:(.+)!(.+)@(.+).tmi.twitch.tv JOIN (#.+)/g.exec(t._raw),n=e[1],r=e[4];return Y(Y({},t),{channel:r,command:G.JOIN,event:G.JOIN,username:n})}(r)).command+"/"+e;break;case $.PART:o=(i=function(t){var e=/:(.+)!(.+)@(.+).tmi.twitch.tv PART (#.+)/g.exec(t._raw),n=e[1],r=e[4];return Y(Y({},t),{channel:r,command:G.PART,event:G.PART,username:n})}(r)).command+"/"+e;break;case $.NAMES:o=(i=function(t){var e=/:(.+).tmi.twitch.tv 353 (.+) = (#.+) :(.+)/g.exec(t._raw),n=e[3],r=e[4].split(" ");return Y(Y({},t),{channel:n,command:G.NAMES,event:G.NAMES,usernames:r})}(r)).command+"/"+e;break;case $.NAMES_END:o=(i=function(t){var e=/:(.+).tmi.twitch.tv 366 (.+) (#.+) :(.+)/g.exec(t._raw),n=e[1],r=e[3];return Y(Y({},t),{channel:r,command:G.NAMES_END,event:G.NAMES_END,username:n})}(r)).command+"/"+e;break;case $.CLEAR_CHAT:o=(i=function(t){var e=t.tags,n=t.message,r=j(t,["tags","message"]);return Y(Y({},r),void 0!==n?{tags:Y(Y({},e),{banReason:Nt(e.banReason),banDuration:lt(e.banDuration)}),command:G.CLEAR_CHAT,event:H.USER_BANNED,username:n}:{command:G.CLEAR_CHAT,event:G.CLEAR_CHAT})}(r)).event?i.command+"/"+i.event+"/"+e:i.command+"/"+e;break;case $.HOST_TARGET:o=(i=function(t){var e=/:tmi.twitch.tv HOSTTARGET (#[^\s]+) :([^\s]+)?\s?(\d+)?/g.exec(t._raw),n=e[1],r=e[2],o=e[3],i="-"===r;return Y(Y({},t),{channel:n,username:r,command:G.HOST_TARGET,event:i?H.HOST_OFF:H.HOST_ON,numberOfViewers:E(S(o))?parseInt(o,10):void 0,message:void 0})}(r)).command+"/"+e;break;case $.MODE:if(o=(i=function(t){var e=/:[^\s]+ MODE (#[^\s]+) (-|\+)o ([^\s]+)/g.exec(t._raw),n=e[1],r=e[2],o=e[3],i="+"===r,s=Y(Y({},t),{command:G.MODE,channel:n,username:o});return Y(Y({},s),i?{event:H.MOD_GAINED,message:"+o",isModerator:!0}:{event:H.MOD_LOST,message:"-o",isModerator:!1})}(r)).command+"/"+e,n(this.options.username)===n(i.username)){var s=this._getChannelState(e);this._setChannelState(e,Y(Y({},s),{userState:Y(Y({},s.userState),{isModerator:i.isModerator})}))}break;case $.GLOBAL_USER_STATE:i=Ct(r),this._userState=i.tags;break;case $.USER_STATE:o=(i=vt(r)).command+"/"+e,this._setChannelState(e,Y(Y({},this._getChannelState(e)),{userState:i.tags}));break;case $.ROOM_STATE:o=(i=function(t){var e=t.tags,n=j(t,["tags"]);return Y(Y({},n),{command:G.ROOM_STATE,event:G.ROOM_STATE,tags:pt(e)})}(r)).command+"/"+e,this._setChannelState(e,Y(Y({},this._getChannelState(e)),{roomState:i.roomState}));break;case $.NOTICE:o=(i=function(t){var e,r=t.tags,o=j(t,["tags"]),i=St(t)?Y(Y({},r),{msgId:n($.AUTHENTICATION_FAILED)}):r,s=T(i.msgId);switch(i.msgId){case W.ROOM_MODS:return Y(Y({},o),{command:G.NOTICE,event:z.ROOM_MODS,tags:i,mods:(e=o.message,e.split(": ")[1].split(", "))});default:return Y(Y({},o),{command:G.NOTICE,event:s,tags:i})}}(r)).command+"/"+i.event+"/"+e;break;case $.USER_NOTICE:o=(i=Dt(r)).command+"/"+i.event+"/"+e;break;case $.PRIVATE_MESSAGE:o=(i=function(t){var e=t._raw,n=t.tags;if(_(n.bits,0))return Y(Y({},vt(t)),{command:G.PRIVATE_MESSAGE,event:H.CHEER,bits:lt(n.bits)});var r=Ot.exec(e)||[],o=r[0],i=r[1],s=r[2],a=r[3],c=r[4];return o?Y(Y({},t),a?{command:G.PRIVATE_MESSAGE,event:H.HOSTED_AUTO,channel:"#"+i,tags:{displayName:s},numberOfViewers:lt(c)}:c?{command:G.PRIVATE_MESSAGE,event:H.HOSTED_WITH_VIEWERS,channel:"#"+i,tags:{displayName:s},numberOfViewers:lt(c)}:{command:G.PRIVATE_MESSAGE,event:H.HOSTED_WITHOUT_VIEWERS,channel:"#"+i,tags:{displayName:s}}):Y(Y({},vt(t)),{command:G.PRIVATE_MESSAGE,event:G.PRIVATE_MESSAGE})}(r)).event?i.command+"/"+i.event+"/"+e:i.command+"/"+e;break;default:var a=function(t){return void 0!==t?t.command||t.event:$.ALL}(r);o="#"===e?a:a+"/"+e}this._emit(o,i)},o.prototype._handleDisconnect=function(){this._connectionInProgress=null,this._readyState=X.DISCONNECTED,this._isDisconnecting=!1},o.Commands=G,o.Events=$,o.CompoundEvents=((rt={})[$.NOTICE]=tt,rt[$.PRIVATE_MESSAGE]=et,rt[$.USER_NOTICE]=nt,rt),o}(t),kt=Object.freeze({__proto__:null,default:jt,get ChatReadyStates(){return X},get NoticeCompounds(){return tt},get PrivateMessageCompounds(){return et},get UserNoticeCompounds(){return nt}}),Vt=function(t){function e(n,r){var o=t.call(this,n.url+" "+n.statusText)||this;return Object.setPrototypeOf(o,e.prototype),o.ok=!1,o.status=n.status,o.statusText=n.statusText,o.url=n.url,o.body=r,o}return y(e,t),e}(Mt),xt=function(t){function e(n,r){var o=t.call(this,n,r)||this;return Object.setPrototypeOf(o,e.prototype),o}return y(e,t),e}(Vt),Kt=function(t){return k(void 0,void 0,void 0,(function(){var e;return V(this,(function(n){switch(n.label){case 0:return[4,t.json()];case 1:if(e=n.sent(),!t.ok)throw new(401===t.status?xt:Vt)(t,e);return[2,u(e,{deep:!0})]}}))}))};!function(t){t[t.NOT_READY=0]="NOT_READY",t[t.READY=1]="READY",t[t.INITIALIZED=2]="INITIALIZED"}(Yt||(Yt={}));var Jt=((Wt={})[L.Helix]={baseUrl:"https://api.twitch.tv/helix",authorizationHeader:"Bearer"},Wt[L.Kraken]={baseUrl:"https://api.twitch.tv/kraken",authorizationHeader:"OAuth"},Wt),zt=function(){function t(t){void 0===t&&(t={}),this._readyState=Yt.READY,this.options=t,this._log=ot(Y({name:"Api"},this.options.log))}return Object.defineProperty(t.prototype,"options",{get:function(){return this._options},set:function(t){this._options=function(t){var e={clientId:function(t){return g(t)||p(t)},token:function(t){return g(t)||p(t)},onAuthenticationFailure:h},n=f(Y({},t),{clientId:void 0,token:void 0,onAuthenticationFailure:function(){return new Promise((function(t,e){return e()}))}});return l(d(n,e),"[twitch-js/Api] options: Expected valid options"),n}(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"readyState",{get:function(){return this._readyState},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"status",{get:function(){return this._status},enumerable:!0,configurable:!0}),t.prototype.updateOptions=function(t){var e=this.options,n=e.clientId,r=e.token;this.options=Y(Y({},t),{clientId:n,token:r})},t.prototype.initialize=function(t){return k(this,void 0,void 0,(function(){var e;return V(this,(function(n){switch(n.label){case 0:return t&&(this.options=Y(Y({},this.options),t)),t||2!==this.readyState?[4,this.get("",{version:L.Kraken})]:[2,Promise.resolve()];case 1:return e=n.sent(),this._readyState=Yt.INITIALIZED,this._status=e,[2,e]}}))}))},t.prototype.hasScope=function(t){var e=this;return new Promise((function(n,r){return 2===e.readyState&&e.status&&C(e.status.token.authorization.scopes,t)?n(!0):r(!1)}))},t.prototype.get=function(t,e){return void 0===t&&(t=""),this._handleFetch(t,e)},t.prototype.post=function(t,e){return this._handleFetch(t,Y(Y({},e),{method:"post"}))},t.prototype.put=function(t,e){return this._handleFetch(t,Y(Y({},e),{method:"put"}))},t.prototype._isVersionHelix=function(t){return n(t)===L.Helix},t.prototype._getBaseUrl=function(t){return Jt[t].baseUrl},t.prototype._getHeaders=function(t){var e=this.options,n=e.clientId,r=e.token,o=this._isVersionHelix(t);l(!o||!(O(n)&&O(r)),"[twitch-js/Api] To call a Helix endpoint, a `clientId` or `token` must be provided");var i=o?{"Client-ID":n}:{Accept:"application/vnd.twitchtv.v5+json","Client-ID":n};if(r){var s=Jt[t].authorizationHeader+" "+r;return Y(Y({},i),{Authorization:s})}return i},t.prototype._handleFetch=function(t,e){return void 0===t&&(t=""),void 0===e&&(e={}),k(this,void 0,void 0,(function(){var n,r,o,s,a,c,u,_,O,E,S=this;return V(this,(function(m){switch(m.label){case 0:n=e.version,r=void 0===n?L.Helix:n,o=j(e,["version"]),s=this._getBaseUrl(r),a=s+"/"+t,c=(T(o.method)||"GET")+" "+a,u=this._log.profile(),_=function(){return function(t,e,n){return void 0===e&&(e={}),k(void 0,void 0,void 0,(function(){var r,o,s,a,c,u;return V(this,(function(_){switch(_.label){case 0:return r=e.body&&!(e.body instanceof D)&&"object"==typeof e.body,o=r?JSON.stringify(e.body):e.body,s=r?Y(Y({},e.headers),{"Content-Type":"application/json"}):e.headers,a="object"==typeof e.search?"?"+i(e.search,n):"",c=Y(Y({},e),o?{method:e.method||"get",headers:s,body:o}:{method:e.method||"get",headers:s}),[4,v(""+t+a,c)];case 1:return u=_.sent(),[2,Kt(u)]}}))}))}(a,Y(Y({},o),{headers:Y(Y({},o.headers),S._getHeaders(r))}))},m.label=1;case 1:return m.trys.push([1,3,9,10]),[4,_()];case 2:return[2,m.sent()];case 3:return(O=m.sent())instanceof xt?[4,this.options.onAuthenticationFailure()]:[3,8];case 4:return(E=m.sent())?[4,this.initialize({token:E})]:[3,6];case 5:m.sent(),this._log.info(c+" ... re-initializing with new token"),m.label=6;case 6:return this._log.info(c+" ... retrying"),[4,_()];case 7:return[2,m.sent()];case 8:throw new Vt(O,c);case 9:return u.done(c),[7];case 10:return[2]}}))}))},t}(),Zt=Object.freeze({__proto__:null,default:zt,get ApiReadyStates(){return Yt},Settings:Jt}),qt=function(){function t(t){var e=t.token,n=t.username,r=t.clientId,o=t.log,i=t.onAuthenticationFailure,s=t.chat,a=t.api;this.chat=new jt(Y(Y({log:o},s),{token:e,username:n,onAuthenticationFailure:i})),this.api=new zt(Y(Y({log:o},a),{token:e,clientId:r,onAuthenticationFailure:i}))}return t.prototype.updateOptions=function(t){var e=t.chat,n=t.api;e&&this.chat.updateOptions(e),n&&this.api.updateOptions(n)},t.Chat=jt,t.Api=zt,t}();export default qt;export{zt as Api,Zt as ApiTypes,L as ApiVersions,w as BaseCommands,Z as BooleanBadges,M as Capabilities,jt as Chat,B as ChatCommands,H as ChatEvents,kt as ChatTypes,G as Commands,$ as Events,W as KnownNoticeMessageIds,J as KnownUserNoticeMessageIds,b as MembershipCommands,z as NoticeEvents,q as NumberBadges,F as OtherCommands,K as PrivateMessageEvents,P as TagCommands,Q as UserNoticeEvents}; //# sourceMappingURL=index.js.map