@mkody/twitch-emoticons 2.7.0 → 2.8.0

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