Skip to content

Commit c74c2bb

Browse files
committed
client: _updateEmoteset no longer uses .api
Also adds skipUpdatingEmotesets and updateEmotesetsTimer to either stop the API call or disable/change the automatic refresh behavior.
1 parent 4793a45 commit c74c2bb

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

lib/client.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var parse = require('./parser');
66
var Queue = require('./timer');
77
var _global = typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : {};
88
var _WebSocket = _global.WebSocket || require('ws');
9+
var _fetch = _global.fetch || require('node-fetch');
910
var _ = require('./utils');
1011

1112
// Client instance..
@@ -21,6 +22,9 @@ var client = function client(opts) {
2122
this.clientId = _.get(this.opts.options.clientId, null);
2223
this._globalDefaultChannel = _.channel(_.get(this.opts.options.globalDefaultChannel, '#tmijs'));
2324
this._skipMembership = _.get(this.opts.options.skipMembership, false);
25+
this._skipUpdatingEmotesets = _.get(this.opts.options.skipUpdatingEmotesets, false);
26+
this._updateEmotesetsTimer = null;
27+
this._updateEmotesetsTimerDelay = _.get(this.opts.options.updateEmotesetsTimer, 60000);
2428

2529
this.maxReconnectAttempts = _.get(this.opts.connection.maxReconnectAttempts, Infinity);
2630
this.maxReconnectInterval = _.get(this.opts.connection.maxReconnectInterval, 30000);
@@ -1153,7 +1157,7 @@ client.prototype._onOpen = function _onOpen() {
11531157
this.emit('logon');
11541158

11551159
let caps = 'twitch.tv/tags twitch.tv/commands';
1156-
if(this._skipMembership !== true) {
1160+
if(!this._skipMembership) {
11571161
caps += ' twitch.tv/membership';
11581162
}
11591163
this.ws.send('CAP REQ :' + caps);
@@ -1384,27 +1388,43 @@ client.prototype._sendMessage = function _sendMessage(delay, channel, message, f
13841388
};
13851389

13861390
// Grab the emote-sets object from the API..
1387-
client.prototype._updateEmoteset = function _updateEmoteset(sets) {
1388-
this.emotes = sets;
1389-
1390-
this._getToken()
1391-
.then(token =>
1392-
this.api({
1393-
url: `/chat/emoticon_images?emotesets=${sets}`,
1391+
client.prototype._updateEmoteset = async function _updateEmoteset(sets) {
1392+
let setsChanges = sets !== undefined;
1393+
if(setsChanges) {
1394+
if(sets === this.emotes) {
1395+
setsChanges = false;
1396+
}
1397+
else {
1398+
this.emotes = sets;
1399+
}
1400+
}
1401+
if(this._skipUpdatingEmotesets) {
1402+
if(setsChanges) {
1403+
this.emit('emotesets', sets, {});
1404+
}
1405+
return;
1406+
}
1407+
try {
1408+
const token = await this._getToken();
1409+
const url = `https://api.twitch.tv/kraken/chat/emoticon_images?emotesets=${sets}`;
1410+
/** @type {import('node-fetch').Response} */
1411+
const res = await _fetch(url, {
13941412
headers: {
13951413
'Accept': 'application/vnd.twitchtv.v5+json',
13961414
'Authorization': `OAuth ${_.token(token)}`,
13971415
'Client-ID': this.clientId
13981416
}
1399-
}, (err, res, body) => {
1400-
if(!err) {
1401-
this.emotesets = body['emoticon_sets'] || {};
1402-
return this.emit('emotesets', sets, this.emotesets);
1403-
}
1404-
setTimeout(() => this._updateEmoteset(sets), 60000);
1405-
})
1406-
)
1407-
.catch(() => setTimeout(() => this._updateEmoteset(sets), 60000));
1417+
});
1418+
const data = await res.json();
1419+
this.emotesets = data.emoticon_sets || {};
1420+
this.emit('emotesets', sets, this.emotesets);
1421+
} catch(err) {
1422+
// TODO: Potentially send error to logger
1423+
}
1424+
if(this._updateEmotesetsTimerDelay > 0) {
1425+
clearTimeout(this._updateEmotesetsTimer);
1426+
this._updateEmotesetsTimer = setTimeout(() => this._updateEmoteset(), this._updateEmotesetsTimerDelay);
1427+
}
14081428
};
14091429

14101430
// Get current username..

0 commit comments

Comments
 (0)