@mkody/twitch-emoticons 2.6.2 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.eslintrc.json +143 -143
  2. package/.github/codeql/codeql-config.yml +4 -4
  3. package/.github/workflows/codeql.yml +50 -50
  4. package/.github/workflows/eslint.yml +50 -50
  5. package/.github/workflows/release.yml +22 -0
  6. package/.github/workflows/yarn-test.yml +17 -17
  7. package/.jsdoc.json +39 -39
  8. package/.nvmrc +1 -0
  9. package/LICENSE +22 -22
  10. package/README.md +13 -8
  11. package/docs/BTTVEmote.html +83 -5
  12. package/docs/Channel.html +1 -1
  13. package/docs/Collection.html +1 -1
  14. package/docs/Emote.html +3 -3
  15. package/docs/EmoteFetcher.html +271 -19
  16. package/docs/EmoteParser.html +1 -1
  17. package/docs/FFZEmote.html +162 -6
  18. package/docs/SevenTV.html +1190 -1190
  19. package/docs/SevenTVEmote.html +84 -6
  20. package/docs/TwitchEmote.html +83 -5
  21. package/docs/index.html +15 -10
  22. package/docs/struct_BTTVEmote.js.html +61 -55
  23. package/docs/struct_Channel.js.html +56 -56
  24. package/docs/struct_Emote.js.html +67 -66
  25. package/docs/struct_EmoteFetcher.js.html +324 -284
  26. package/docs/struct_EmoteParser.js.html +82 -81
  27. package/docs/struct_FFZEmote.js.html +75 -62
  28. package/docs/struct_SevenTVEmote.js.html +70 -64
  29. package/docs/struct_TwitchEmote.js.html +59 -53
  30. package/docs/util_Collection.js.html +79 -79
  31. package/package.json +57 -57
  32. package/src/index.js +12 -12
  33. package/src/struct/BTTVEmote.js +60 -54
  34. package/src/struct/Channel.js +55 -55
  35. package/src/struct/Emote.js +66 -65
  36. package/src/struct/EmoteFetcher.js +323 -283
  37. package/src/struct/EmoteParser.js +81 -80
  38. package/src/struct/FFZEmote.js +74 -61
  39. package/src/struct/SevenTVEmote.js +69 -63
  40. package/src/struct/TwitchEmote.js +58 -52
  41. package/src/util/Collection.js +78 -78
  42. package/src/util/Constants.js +31 -25
  43. package/test/index.js +174 -166
  44. package/typings/index.d.ts +98 -93
@@ -29,7 +29,7 @@
29
29
  <nav >
30
30
 
31
31
 
32
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BTTVEmote.html">BTTVEmote</a><ul class='methods'><li data-type='method'><a href="BTTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="BTTVEmote.html#toString">toString</a></li></ul></li><li><a href="Channel.html">Channel</a><ul class='methods'><li data-type='method'><a href="Channel.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="Channel.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="Channel.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li></ul></li><li><a href="Collection.html">Collection</a><ul class='methods'><li data-type='method'><a href="Collection.html#filter">filter</a></li><li data-type='method'><a href="Collection.html#find">find</a></li><li data-type='method'><a href="Collection.html#map">map</a></li></ul></li><li><a href="Emote.html">Emote</a><ul class='methods'><li data-type='method'><a href="Emote.html#toString">toString</a></li></ul></li><li><a href="EmoteFetcher.html">EmoteFetcher</a><ul class='methods'><li data-type='method'><a href="EmoteFetcher.html#_cacheBTTVEmote">_cacheBTTVEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_cacheFFZEmote">_cacheFFZEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_cacheSevenTVEmote">_cacheSevenTVEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_cacheTwitchEmote">_cacheTwitchEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawBTTVEmotes">_getRawBTTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawFFZEmotes">_getRawFFZEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawSevenTVEmotes">_getRawSevenTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawTwitchEmotes">_getRawTwitchEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchTwitchEmotes">fetchTwitchEmotes</a></li></ul></li><li><a href="EmoteParser.html">EmoteParser</a><ul class='methods'><li data-type='method'><a href="EmoteParser.html#parse">parse</a></li></ul></li><li><a href="FFZEmote.html">FFZEmote</a><ul class='methods'><li data-type='method'><a href="FFZEmote.html#toLink">toLink</a></li><li data-type='method'><a href="FFZEmote.html#toString">toString</a></li></ul></li><li><a href="SevenTVEmote.html">SevenTVEmote</a><ul class='methods'><li data-type='method'><a href="SevenTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="SevenTVEmote.html#toString">toString</a></li></ul></li><li><a href="TwitchEmote.html">TwitchEmote</a><ul class='methods'><li data-type='method'><a href="TwitchEmote.html#toLink">toLink</a></li><li data-type='method'><a href="TwitchEmote.html#toString">toString</a></li></ul></li></ul>
32
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BTTVEmote.html">BTTVEmote</a><ul class='methods'><li data-type='method'><a href="BTTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="BTTVEmote.html#toString">toString</a></li></ul></li><li><a href="Channel.html">Channel</a><ul class='methods'><li data-type='method'><a href="Channel.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="Channel.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="Channel.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li></ul></li><li><a href="Collection.html">Collection</a><ul class='methods'><li data-type='method'><a href="Collection.html#filter">filter</a></li><li data-type='method'><a href="Collection.html#find">find</a></li><li data-type='method'><a href="Collection.html#map">map</a></li></ul></li><li><a href="Emote.html">Emote</a><ul class='methods'><li data-type='method'><a href="Emote.html#toString">toString</a></li></ul></li><li><a href="EmoteFetcher.html">EmoteFetcher</a><ul class='methods'><li data-type='method'><a href="EmoteFetcher.html#_cacheBTTVEmote">_cacheBTTVEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_cacheFFZEmote">_cacheFFZEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_cacheSevenTVEmote">_cacheSevenTVEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_cacheTwitchEmote">_cacheTwitchEmote</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawBTTVEmotes">_getRawBTTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawFFZEmoteSet">_getRawFFZEmoteSet</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawFFZEmotes">_getRawFFZEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawSevenTVEmotes">_getRawSevenTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#_getRawTwitchEmotes">_getRawTwitchEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchBTTVEmotes">fetchBTTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchFFZEmotes">fetchFFZEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchSevenTVEmotes">fetchSevenTVEmotes</a></li><li data-type='method'><a href="EmoteFetcher.html#fetchTwitchEmotes">fetchTwitchEmotes</a></li></ul></li><li><a href="EmoteParser.html">EmoteParser</a><ul class='methods'><li data-type='method'><a href="EmoteParser.html#parse">parse</a></li></ul></li><li><a href="FFZEmote.html">FFZEmote</a><ul class='methods'><li data-type='method'><a href="FFZEmote.html#toLink">toLink</a></li><li data-type='method'><a href="FFZEmote.html#toString">toString</a></li></ul></li><li><a href="SevenTVEmote.html">SevenTVEmote</a><ul class='methods'><li data-type='method'><a href="SevenTVEmote.html#toLink">toLink</a></li><li data-type='method'><a href="SevenTVEmote.html#toString">toString</a></li></ul></li><li><a href="TwitchEmote.html">TwitchEmote</a><ul class='methods'><li data-type='method'><a href="TwitchEmote.html#toLink">toLink</a></li><li data-type='method'><a href="TwitchEmote.html#toString">toString</a></li></ul></li></ul>
33
33
 
34
34
  </nav>
35
35
 
@@ -45,289 +45,329 @@
45
45
 
46
46
  <section>
47
47
  <article>
48
- <pre class="prettyprint source linenums"><code>const BTTVEmote = require('./BTTVEmote');
49
- const Channel = require('./Channel');
50
- const Collection = require('../util/Collection');
51
- const Constants = require('../util/Constants');
52
- const FFZEmote = require('./FFZEmote');
53
- const SevenTVEmote = require('./SevenTVEmote');
54
-
55
- const got = require('got');
56
- const TwitchEmote = require('./TwitchEmote');
57
- const { ApiClient } = require('@twurple/api');
58
- const { AppTokenAuthProvider } = require('@twurple/auth');
59
-
60
- const options = {
61
- responseType: 'json'
62
- };
63
-
64
- class EmoteFetcher {
65
- /**
66
- * Fetches and caches emotes.
67
- * @param {string} clientId The client id for the twitch api.
68
- * @param {string} clientSecret The client secret for the twitch api.
69
- */
70
- constructor(clientId, clientSecret) {
71
- if (clientId !== undefined &amp;&amp; clientSecret !== undefined) {
72
- const authProvider = new AppTokenAuthProvider(clientId, clientSecret);
73
-
74
- /**
75
- * Twitch api client.
76
- */
77
- this.apiClient = new ApiClient({ authProvider });
78
- }
79
-
80
- /**
81
- * Cached emotes.
82
- * Collectionped by emote code to Emote instance.
83
- * @type {Collection&lt;string, Emote>}
84
- */
85
- this.emotes = new Collection();
86
-
87
- /**
88
- * Cached channels.
89
- * Collectionped by name to Channel instance.
90
- * @type {Collection&lt;string, Channel>}
91
- */
92
- this.channels = new Collection();
93
- }
94
-
95
- /**
96
- * The global channel for Twitch, BTTV and 7TV.
97
- * @readonly
98
- * @type {?Channel}
99
- */
100
- get globalChannel() {
101
- return this.channels.get(null);
102
- }
103
-
104
- /**
105
- * Gets the raw Twitch emotes data for a channel.
106
- * @private
107
- * @param {int} id - ID of the channel.
108
- * @returns {Promise&lt;Object[]>}
109
- */
110
- _getRawTwitchEmotes(id) {
111
- if (!this.apiClient) {
112
- throw new Error('Client id or client secret not provided.');
113
- }
114
-
115
- if (id) {
116
- return this.apiClient.chat.getChannelEmotes(id);
117
- } else {
118
- return this.apiClient.chat.getGlobalEmotes();
119
- }
120
- }
121
-
122
- /**
123
- * Converts and caches a raw twitch emote.
124
- * @private
125
- * @param {int} channel_id - ID of the channel.
126
- * @param {Object} data - Raw data.
127
- * @returns {TwitchEmote}
128
- */
129
- _cacheTwitchEmote(channel_id, data) {
130
- let channel = this.channels.get(channel_id);
131
- if (!channel) {
132
- channel = new Channel(this, channel_id);
133
- this.channels.set(channel_id, channel);
134
- }
135
-
136
- const emote = new TwitchEmote(channel, data.id, data);
137
- this.emotes.set(emote.code, emote);
138
- channel.emotes.set(emote.code, emote);
139
- return emote;
140
- }
141
-
142
- /**
143
- * Gets the raw BTTV emotes data for a channel.
144
- * Use `null` for the global emotes channel.
145
- * @private
146
- * @param {int} [id=null] - ID of the channel.
147
- * @returns {Promise&lt;Object[]>}
148
- */
149
- _getRawBTTVEmotes(id) {
150
- const endpoint = !id
151
- ? Constants.BTTV.Global
152
- : Constants.BTTV.Channel(id); // eslint-disable-line new-cap
153
-
154
- return got(endpoint, options).then(req => {
155
- // Global emotes
156
- if (req.body instanceof Array) return req.body;
157
- // Channel emotes
158
- return [...req.body.channelEmotes, ...req.body.sharedEmotes];
159
- });
160
- }
161
-
162
- /**
163
- * Converts and caches a raw BTTV emote.
164
- * @private
165
- * @param {int} channel_id - ID of the channel.
166
- * @param {Object} data - Raw data.
167
- * @returns {BTTVEmote}
168
- */
169
- _cacheBTTVEmote(channel_id, data) {
170
- let channel = this.channels.get(channel_id);
171
- if (!channel) {
172
- channel = new Channel(this, channel_id);
173
- this.channels.set(channel_id, channel);
174
- }
175
-
176
- const emote = new BTTVEmote(channel, data.id, data);
177
- this.emotes.set(emote.code, emote);
178
- channel.emotes.set(emote.code, emote);
179
- return emote;
180
- }
181
-
182
- /**
183
- * Gets the raw FFZ emotes data for a channel.
184
- * @private
185
- * @param {int} id - ID of the channel.
186
- * @returns {Promise&lt;Object[]>}
187
- */
188
- _getRawFFZEmotes(id) {
189
- const endpoint = Constants.FFZ.Channel(id); // eslint-disable-line new-cap
190
-
191
- return got(endpoint, options).then(req => {
192
- const emotes = [];
193
- for (const key of Object.keys(req.body.sets)) {
194
- const set = req.body.sets[key];
195
- emotes.push(...set.emoticons);
196
- }
197
-
198
- return emotes;
199
- });
200
- }
201
-
202
- /**
203
- * Converts and caches a raw FFZ emote.
204
- * @private
205
- * @param {int} channel_id - ID of the channel.
206
- * @param {Object} data - Raw data.
207
- * @returns {FFZEmote}
208
- */
209
- _cacheFFZEmote(channel_id, data) {
210
- let channel = this.channels.get(channel_id);
211
- if (!channel) {
212
- channel = new Channel(this, channel_id);
213
- this.channels.set(channel_id, channel);
214
- }
215
-
216
- const emote = new FFZEmote(channel, data.id, data);
217
- this.emotes.set(emote.code, emote);
218
- channel.emotes.set(emote.code, emote);
219
- return emote;
220
- }
221
-
222
- /**
223
- * Gets the raw 7TV emotes data for a channel.
224
- * @private
225
- * @param {int} [id=null] - ID of the channel.
226
- * @returns {Promise&lt;Object[]>}
227
- */
228
- _getRawSevenTVEmotes(id) {
229
- const endpoint = !id
230
- ? Constants.SEVENTV.Global
231
- : Constants.SEVENTV.Channel(id); // eslint-disable-line new-cap
232
-
233
- return got(endpoint, options).then(req => req.body);
234
- }
235
-
236
- /**
237
- * Converts and caches a raw 7TV emote.
238
- * @private
239
- * @param {int} channel_id - ID of the channel.
240
- * @param {Object} data - Raw data.
241
- * @param {string} format - The type file format to use (webp/avif).
242
- * @returns {SevenTVEmote}
243
- */
244
- _cacheSevenTVEmote(channel_id, data, format) {
245
- let channel = this.channels.get(channel_id);
246
- if (!channel) {
247
- channel = new Channel(this, channel_id);
248
- this.channels.set(channel_id, channel);
249
- }
250
- channel.format = format;
251
-
252
- const emote = new SevenTVEmote(channel, data.id, data);
253
- this.emotes.set(emote.code, emote);
254
- channel.emotes.set(emote.code, emote);
255
- return emote;
256
- }
257
-
258
- /**
259
- * Fetches the Twitch emotes for a channel.
260
- * Use `null` for the global emotes channel.
261
- * @param {int} [channel=null] - ID of the channel.
262
- * @returns {Promise&lt;Collection&lt;string, TwitchEmote>>}
263
- */
264
- fetchTwitchEmotes(channel = null) {
265
- return this._getRawTwitchEmotes(channel).then(rawEmotes => {
266
- for (const emote of rawEmotes) {
267
- this._cacheTwitchEmote(channel, { code: emote.name, id: emote.id });
268
- }
269
-
270
- return this.channels.get(channel).emotes.filter(e => e.type === 'twitch');
271
- });
272
- }
273
-
274
- /**
275
- * Fetches the BTTV emotes for a channel.
276
- * Use `null` for the global emotes channel.
277
- * @param {int} [channel=null] - ID of the channel.
278
- * @returns {Promise&lt;Collection&lt;string, BTTVEmote>>}
279
- */
280
- fetchBTTVEmotes(channel = null) {
281
- return this._getRawBTTVEmotes(channel).then(rawEmotes => {
282
- for (const data of rawEmotes) {
283
- this._cacheBTTVEmote(channel, data);
284
- }
285
-
286
- return this.channels.get(channel).emotes.filter(e => e.type === 'bttv');
287
- });
288
- }
289
-
290
- /**
291
- * Fetches the FFZ emotes for a channel.
292
- * @param {int} channel - ID of the channel.
293
- * @returns {Promise&lt;Collection&lt;string, FFZEmote>>}
294
- */
295
- fetchFFZEmotes(channel) {
296
- return this._getRawFFZEmotes(channel).then(rawEmotes => {
297
- for (const data of rawEmotes) {
298
- this._cacheFFZEmote(channel, data);
299
- }
300
-
301
- return this.channels.get(channel).emotes.filter(e => e.type === 'ffz');
302
- });
303
- }
304
-
305
- /**
306
- * Fetches the 7TV emotes for a channel.
307
- * @param {int} [channel=null] - ID of the channel.
308
- * @param {('webp'|'avif')} [format='webp'] - The type file format to use (webp/avif).
309
- * @returns {Promise&lt;Collection&lt;string, SevenTVEmote>>}
310
- */
311
- fetchSevenTVEmotes(channel = null, format = 'webp') {
312
- return this._getRawSevenTVEmotes(channel).then(rawEmotes => {
313
- if ('emotes' in rawEmotes) {
314
- // From an emote set (like "global")
315
- for (const data of rawEmotes.emotes) {
316
- this._cacheSevenTVEmote(channel, data.data, format);
317
- }
318
- } else {
319
- // From users
320
- for (const data of rawEmotes.emote_set.emotes) {
321
- this._cacheSevenTVEmote(channel, data.data, format);
322
- }
323
- }
324
-
325
- return this.channels.get(channel).emotes.filter(e => e.type === '7tv');
326
- });
327
- }
328
- }
329
-
330
- module.exports = EmoteFetcher;
48
+ <pre class="prettyprint source linenums"><code>const BTTVEmote = require('./BTTVEmote');
49
+ const Channel = require('./Channel');
50
+ const Collection = require('../util/Collection');
51
+ const Constants = require('../util/Constants');
52
+ const FFZEmote = require('./FFZEmote');
53
+ const SevenTVEmote = require('./SevenTVEmote');
54
+ const TwitchEmote = require('./TwitchEmote');
55
+
56
+ const axios = require('axios');
57
+ const { ApiClient } = require('@twurple/api');
58
+ const { AppTokenAuthProvider } = require('@twurple/auth');
59
+
60
+ class EmoteFetcher {
61
+ /**
62
+ * Fetches and caches emotes.
63
+ * @param {string} clientId The client id for the twitch api.
64
+ * @param {string} clientSecret The client secret for the twitch api.
65
+ */
66
+ constructor(clientId, clientSecret) {
67
+ if (clientId !== undefined &amp;&amp; clientSecret !== undefined) {
68
+ const authProvider = new AppTokenAuthProvider(clientId, clientSecret);
69
+
70
+ /**
71
+ * Twitch api client.
72
+ */
73
+ this.apiClient = new ApiClient({ authProvider });
74
+ }
75
+
76
+ /**
77
+ * Cached emotes.
78
+ * Collectionped by emote code to Emote instance.
79
+ * @type {Collection&lt;string, Emote>}
80
+ */
81
+ this.emotes = new Collection();
82
+
83
+ /**
84
+ * Cached channels.
85
+ * Collectionped by name to Channel instance.
86
+ * @type {Collection&lt;string, Channel>}
87
+ */
88
+ this.channels = new Collection();
89
+
90
+ /**
91
+ * Save if we fetched FFZ's modifier emotes once.
92
+ * @type {boolean}
93
+ */
94
+ this.ffzModifiersFetched = false;
95
+ }
96
+
97
+ /**
98
+ * The global channel for Twitch, BTTV and 7TV.
99
+ * @readonly
100
+ * @type {?Channel}
101
+ */
102
+ get globalChannel() {
103
+ return this.channels.get(null);
104
+ }
105
+
106
+ /**
107
+ * Gets the raw Twitch emotes data for a channel.
108
+ * @private
109
+ * @param {int} id - ID of the channel.
110
+ * @returns {Promise&lt;Object[]>}
111
+ */
112
+ _getRawTwitchEmotes(id) {
113
+ if (!this.apiClient) {
114
+ throw new Error('Client id or client secret not provided.');
115
+ }
116
+
117
+ if (id) {
118
+ return this.apiClient.chat.getChannelEmotes(id);
119
+ } else {
120
+ return this.apiClient.chat.getGlobalEmotes();
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Converts and caches a raw twitch emote.
126
+ * @private
127
+ * @param {int} channel_id - ID of the channel.
128
+ * @param {Object} data - Raw data.
129
+ * @returns {TwitchEmote}
130
+ */
131
+ _cacheTwitchEmote(channel_id, data) {
132
+ let channel = this.channels.get(channel_id);
133
+ if (!channel) {
134
+ channel = new Channel(this, channel_id);
135
+ this.channels.set(channel_id, channel);
136
+ }
137
+
138
+ const emote = new TwitchEmote(channel, data.id, data);
139
+ this.emotes.set(emote.code, emote);
140
+ channel.emotes.set(emote.code, emote);
141
+ return emote;
142
+ }
143
+
144
+ /**
145
+ * Gets the raw BTTV emotes data for a channel.
146
+ * Use `null` for the global emotes channel.
147
+ * @private
148
+ * @param {int} [id=null] - ID of the channel.
149
+ * @returns {Promise&lt;Object[]>}
150
+ */
151
+ _getRawBTTVEmotes(id) {
152
+ const endpoint = !id
153
+ ? Constants.BTTV.Global
154
+ : Constants.BTTV.Channel(id); // eslint-disable-line new-cap
155
+
156
+ return axios.get(endpoint).then(req => {
157
+ // Global emotes
158
+ if (req.data instanceof Array) return req.data;
159
+ // Channel emotes
160
+ return [...req.data.channelEmotes, ...req.data.sharedEmotes];
161
+ });
162
+ }
163
+
164
+ /**
165
+ * Converts and caches a raw BTTV emote.
166
+ * @private
167
+ * @param {int} channel_id - ID of the channel.
168
+ * @param {Object} data - Raw data.
169
+ * @returns {BTTVEmote}
170
+ */
171
+ _cacheBTTVEmote(channel_id, data) {
172
+ let channel = this.channels.get(channel_id);
173
+ if (!channel) {
174
+ channel = new Channel(this, channel_id);
175
+ this.channels.set(channel_id, channel);
176
+ }
177
+
178
+ const emote = new BTTVEmote(channel, data.id, data);
179
+ this.emotes.set(emote.code, emote);
180
+ channel.emotes.set(emote.code, emote);
181
+ return emote;
182
+ }
183
+
184
+ /**
185
+ * Gets the raw FFZ emote data from a set.
186
+ * @private
187
+ * @param {int} id - ID of the set.
188
+ * @returns {Promise&lt;Object[]>}
189
+ */
190
+ _getRawFFZEmoteSet(id) {
191
+ const endpoint = Constants.FFZ.Set(id); // eslint-disable-line new-cap
192
+
193
+ return axios.get(endpoint).then(req => {
194
+ return req.data.set.emoticons;
195
+ });
196
+ }
197
+
198
+ /**
199
+ * Gets the raw FFZ emotes data for a channel.
200
+ * @private
201
+ * @param {int} id - ID of the channel.
202
+ * @returns {Promise&lt;Object[]>}
203
+ */
204
+ _getRawFFZEmotes(id) {
205
+ const endpoint = Constants.FFZ.Channel(id); // eslint-disable-line new-cap
206
+
207
+ return axios.get(endpoint).then(req => {
208
+ const emotes = [];
209
+ for (const key of Object.keys(req.data.sets)) {
210
+ const set = req.data.sets[key];
211
+ emotes.push(...set.emoticons);
212
+ }
213
+
214
+ return emotes;
215
+ });
216
+ }
217
+
218
+ /**
219
+ * Converts and caches a raw FFZ emote.
220
+ * @private
221
+ * @param {int} channel_id - ID of the channel.
222
+ * @param {Object} data - Raw data.
223
+ * @returns {FFZEmote}
224
+ */
225
+ _cacheFFZEmote(channel_id, data) {
226
+ let channel = this.channels.get(channel_id);
227
+ if (!channel) {
228
+ channel = new Channel(this, channel_id);
229
+ this.channels.set(channel_id, channel);
230
+ }
231
+
232
+ const emote = new FFZEmote(channel, data.id, data);
233
+ this.emotes.set(emote.code, emote);
234
+ channel.emotes.set(emote.code, emote);
235
+ return emote;
236
+ }
237
+
238
+ /**
239
+ * Gets the raw 7TV emotes data for a channel.
240
+ * @private
241
+ * @param {int} [id=null] - ID of the channel.
242
+ * @returns {Promise&lt;Object[]>}
243
+ */
244
+ _getRawSevenTVEmotes(id) {
245
+ const endpoint = !id
246
+ ? Constants.SevenTV.Global
247
+ : Constants.SevenTV.Channel(id); // eslint-disable-line new-cap
248
+
249
+ return axios.get(endpoint).then(req => req.data);
250
+ }
251
+
252
+ /**
253
+ * Converts and caches a raw 7TV emote.
254
+ * @private
255
+ * @param {int} channel_id - ID of the channel.
256
+ * @param {Object} data - Raw data.
257
+ * @param {string} format - The type file format to use (webp/avif).
258
+ * @returns {SevenTVEmote}
259
+ */
260
+ _cacheSevenTVEmote(channel_id, data, format) {
261
+ let channel = this.channels.get(channel_id);
262
+ if (!channel) {
263
+ channel = new Channel(this, channel_id);
264
+ this.channels.set(channel_id, channel);
265
+ }
266
+ channel.format = format;
267
+
268
+ const emote = new SevenTVEmote(channel, data.id, data);
269
+ this.emotes.set(emote.code, emote);
270
+ channel.emotes.set(emote.code, emote);
271
+ return emote;
272
+ }
273
+
274
+ /**
275
+ * Fetches the Twitch emotes for a channel.
276
+ * Use `null` for the global emotes channel.
277
+ * @param {int} [channel=null] - ID of the channel.
278
+ * @returns {Promise&lt;Collection&lt;string, TwitchEmote>>}
279
+ */
280
+ fetchTwitchEmotes(channel = null) {
281
+ return this._getRawTwitchEmotes(channel).then(rawEmotes => {
282
+ for (const emote of rawEmotes) {
283
+ this._cacheTwitchEmote(channel, {
284
+ code: emote.name, id: emote.id, formats: emote.formats
285
+ });
286
+ }
287
+
288
+ return this.channels.get(channel).emotes.filter(e => e.type === 'twitch');
289
+ });
290
+ }
291
+
292
+ /**
293
+ * Fetches the BTTV emotes for a channel.
294
+ * Use `null` for the global emotes channel.
295
+ * @param {int} [channel=null] - ID of the channel.
296
+ * @returns {Promise&lt;Collection&lt;string, BTTVEmote>>}
297
+ */
298
+ fetchBTTVEmotes(channel = null) {
299
+ return this._getRawBTTVEmotes(channel).then(rawEmotes => {
300
+ for (const data of rawEmotes) {
301
+ this._cacheBTTVEmote(channel, data);
302
+ }
303
+
304
+ return this.channels.get(channel).emotes.filter(e => e.type === 'bttv');
305
+ });
306
+ }
307
+
308
+ /**
309
+ * Fetches the FFZ emotes for a channel.
310
+ * @param {int} [channel=null] - ID of the channel.
311
+ * @returns {Promise&lt;Collection&lt;string, FFZEmote>>}
312
+ */
313
+ async fetchFFZEmotes(channel) {
314
+ // Fetch modifier emotes at least once
315
+ if (!this.ffzModifiersFetched) {
316
+ this.ffzModifiersFetched = true;
317
+
318
+ await this._getRawFFZEmoteSet(Constants.FFZ.sets.Modifiers).then(rawEmotes => {
319
+ for (const data of rawEmotes) {
320
+ this._cacheFFZEmote(null, data);
321
+ }
322
+ });
323
+ }
324
+
325
+ // If no channel specified, fetch the Global set
326
+ if (!channel) {
327
+ return this._getRawFFZEmoteSet(Constants.FFZ.sets.Global).then(rawEmotes => {
328
+ for (const data of rawEmotes) {
329
+ this._cacheFFZEmote(channel, data);
330
+ }
331
+
332
+ return this.channels.get(channel).emotes.filter(e => e.type === 'ffz');
333
+ });
334
+ }
335
+
336
+ return this._getRawFFZEmotes(channel).then(rawEmotes => {
337
+ for (const data of rawEmotes) {
338
+ this._cacheFFZEmote(channel, data);
339
+ }
340
+
341
+ return this.channels.get(channel).emotes.filter(e => e.type === 'ffz');
342
+ });
343
+ }
344
+
345
+ /**
346
+ * Fetches the 7TV emotes for a channel.
347
+ * @param {int} [channel=null] - ID of the channel.
348
+ * @param {('webp'|'avif')} [format='webp'] - The type file format to use (webp/avif).
349
+ * @returns {Promise&lt;Collection&lt;string, SevenTVEmote>>}
350
+ */
351
+ fetchSevenTVEmotes(channel = null, format = 'webp') {
352
+ return this._getRawSevenTVEmotes(channel).then(rawEmotes => {
353
+ if ('emotes' in rawEmotes) {
354
+ // From an emote set (like "global")
355
+ for (const data of rawEmotes.emotes) {
356
+ this._cacheSevenTVEmote(channel, data.data, format);
357
+ }
358
+ } else {
359
+ // From users
360
+ for (const data of rawEmotes.emote_set.emotes) {
361
+ this._cacheSevenTVEmote(channel, data.data, format);
362
+ }
363
+ }
364
+
365
+ return this.channels.get(channel).emotes.filter(e => e.type === '7tv');
366
+ });
367
+ }
368
+ }
369
+
370
+ module.exports = EmoteFetcher;
331
371
  </code></pre>
332
372
  </article>
333
373
  </section>