@mkody/twitch-emoticons 2.9.6 → 3.0.0-beta.2
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.
- package/README.md +530 -169
- package/dist/TwitchEmoticons.cjs +950 -0
- package/dist/TwitchEmoticons.esm.min.js +7 -0
- package/dist/TwitchEmoticons.esm.min.js.map +7 -0
- package/dist/TwitchEmoticons.min.js +2 -20
- package/dist/TwitchEmoticons.min.js.map +4 -4
- package/package.json +99 -72
- package/src/index.js +38 -12
- package/src/struct/BTTVEmote.js +72 -74
- package/src/struct/Channel.js +48 -48
- package/src/struct/Emote.js +71 -71
- package/src/struct/EmoteFetcher.js +358 -322
- package/src/struct/EmoteParser.js +95 -84
- package/src/struct/EmoteTypeMapper.js +9 -9
- package/src/struct/FFZEmote.js +91 -93
- package/src/struct/SevenTVEmote.js +88 -90
- package/src/struct/TwitchEmote.js +72 -71
- package/src/util/Collection.js +69 -55
- package/src/util/Constants.js +30 -30
- package/typings/index.d.cts +254 -0
- package/typings/index.d.ts +216 -99
- package/.jsdoc.json +0 -46
- package/.nvmrc +0 -1
- package/docs/BTTVEmote.html +0 -1532
- package/docs/Channel.html +0 -824
- package/docs/Collection.html +0 -798
- package/docs/Emote.html +0 -832
- package/docs/EmoteFetcher.html +0 -3741
- package/docs/EmoteParser.html +0 -1187
- package/docs/FFZEmote.html +0 -1686
- package/docs/SevenTVEmote.html +0 -1596
- package/docs/TwitchEmote.html +0 -1528
- package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +0 -978
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +0 -1049
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
- package/docs/index.html +0 -233
- package/docs/scripts/collapse.js +0 -39
- package/docs/scripts/commonNav.js +0 -28
- package/docs/scripts/linenumber.js +0 -25
- package/docs/scripts/nav.js +0 -12
- package/docs/scripts/polyfill.js +0 -4
- package/docs/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/docs/scripts/prettify/lang-css.js +0 -2
- package/docs/scripts/prettify/prettify.js +0 -28
- package/docs/scripts/search.js +0 -99
- package/docs/struct_BTTVEmote.js.html +0 -162
- package/docs/struct_Channel.js.html +0 -132
- package/docs/struct_Emote.js.html +0 -159
- package/docs/struct_EmoteFetcher.js.html +0 -429
- package/docs/struct_EmoteParser.js.html +0 -172
- package/docs/struct_FFZEmote.js.html +0 -185
- package/docs/struct_SevenTVEmote.js.html +0 -180
- package/docs/struct_TwitchEmote.js.html +0 -159
- package/docs/styles/jsdoc.css +0 -776
- package/docs/styles/prettify.css +0 -80
- package/docs/util_Collection.js.html +0 -151
- package/eslint.config.mjs +0 -215
- package/jest.config.js +0 -198
- package/test/BTTV.test.js +0 -48
- package/test/FFZ.test.js +0 -71
- package/test/SevenTV.test.js +0 -71
- package/test/ToFromObject.test.js +0 -156
- package/test/Twitch.test.js +0 -64
- package/test/__snapshots__/ToFromObject.test.js.snap +0 -121
- package/test/other.test.js +0 -72
package/README.md
CHANGED
|
@@ -1,169 +1,530 @@
|
|
|
1
|
-
# twitch-emoticons
|
|
2
|
-
|
|
3
|
-
Gets Twitch, BTTV, FFZ and 7TV emotes as well as parsing text to emotes!
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
//
|
|
152
|
-
//
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
1
|
+
# twitch-emoticons
|
|
2
|
+
|
|
3
|
+
Gets Twitch, BTTV, FFZ and 7TV emotes as well as parsing text to emotes!
|
|
4
|
+
|
|
5
|
+
> [!IMPORTANT]
|
|
6
|
+
> This branch is for the **currently pre-released** 3.0 version.
|
|
7
|
+
> If you want to see the latest stable version (as of this commit),
|
|
8
|
+
> [switch to the `version/2.9` branch](https://github.com/mkody/twitch-emoticons/tree/version/2.9).
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Migrate to 3.x
|
|
12
|
+
|
|
13
|
+
**This release introduces breaking changes!**
|
|
14
|
+
|
|
15
|
+
<details>
|
|
16
|
+
<summary>Click here to toggle for details</summary>
|
|
17
|
+
|
|
18
|
+
### List of breaking changes from 2.x to 3.x
|
|
19
|
+
|
|
20
|
+
- Node.js 20 is required; we've set the minimum to 20.18.1.
|
|
21
|
+
*This library is now an ECMAScript module, so you can use proper `import {...} from '...'` imports.*
|
|
22
|
+
- The initialization of `EmoteFetcher` changed to only use an object as the first parameter for options.
|
|
23
|
+
- API keys for Twitch must now be set with `twitchAppID` and `twitchAppSecret` properties.
|
|
24
|
+
- The previously available `apiClient` is now set in this object too.
|
|
25
|
+
- The defaults for `EmoteParser` changed to use the `html` template, and it doesn't require `:colons:` by default (using `/(\w+)/` to match any words).
|
|
26
|
+
- The default `html` template doesn't have `twitch-emote-{size}` anymore in its `class` attribute.
|
|
27
|
+
*The `size` isn't consistent between the different sources, so it can't reliably be used.*
|
|
28
|
+
- The `EmoteFetcher.fetchSevenTVEmotes()`, `Emote.toLink()`, and `EmoteParse.parse()` methods now have their options as an object.
|
|
29
|
+
- `fetcher.fetchSevenTVEmotes(null, { format: 'avif' })` - The first parameter is still the Twitch user ID (or `null` for global).
|
|
30
|
+
- `emote.toLink({ size: 1, forceStatic: true, themeMode: 'light' })`
|
|
31
|
+
- `parser.parse('Kappa', { size: 2, forceStatic: true, themeMode: 'dark' })` - The first parameter is still the input text.
|
|
32
|
+
- The `owner` getter for `Emote`s has been removed.
|
|
33
|
+
*It isn't reliable to get the `Channel` object, more so with 3rd-party providers since emotes might be owned by a channel that we never fetched (for shared/public emotes).*
|
|
34
|
+
- If you've exported 7TV emotes, do note that the `sizes` array changed to not include the leading `x.<format>`.
|
|
35
|
+
- **More may come for the final release, as this is still a work in progress.**
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### Example of code changes
|
|
39
|
+
|
|
40
|
+
Our examples are running in an ESM-based project (`"type": "module"`).
|
|
41
|
+
|
|
42
|
+
We export a CommonJS-compatible build, so you can still use `require(...)`:
|
|
43
|
+
```js
|
|
44
|
+
const { EmoteFetcher, EmoteParser } = require('@mkody/twitch-emoticons')
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
#### Before (2.x)
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import TwitchEmoticons from '@mkody/twitch-emoticons' // This actually still works, for compatibility's sake,
|
|
52
|
+
const { EmoteFetcher, EmoteParser } = TwitchEmoticons // but you should move away from this...
|
|
53
|
+
|
|
54
|
+
const fetcher = new EmoteFetcher('<your app ID>', '<your app secret>') // <- The first two parameters were for the Twitch app ID/secret
|
|
55
|
+
|
|
56
|
+
// Those next three lines didn't have breaking changes
|
|
57
|
+
await fetcher.fetchTwitchEmotes() // Do note that CommonJS doesn't handle `await` at the top level
|
|
58
|
+
const parser = new EmoteParser(fetcher)
|
|
59
|
+
const emote = emoteFetcher.emotes.get('Kappa')
|
|
60
|
+
|
|
61
|
+
console.log(emote.toLink(2)) // <- Only the size was available and was set as the first parameter
|
|
62
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/25/default/dark/3.0
|
|
63
|
+
|
|
64
|
+
console.log(parser.parse('Hello :CoolCat:!')) // <- Used colons and returned Markdown by default
|
|
65
|
+
// Hello !
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
#### After (3.x)
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { EmoteFetcher, EmoteParser } from '@mkody/twitch-emoticons' // <- Proper imports!
|
|
73
|
+
|
|
74
|
+
const fetcher = new EmoteFetcher({ // <- Uses an object!
|
|
75
|
+
twitchAppID: '<your app ID>',
|
|
76
|
+
twitchAppSecret: '<your app secret>',
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
// Those next three lines didn't have breaking changes
|
|
80
|
+
await fetcher.fetchTwitchEmotes()
|
|
81
|
+
const parser = new EmoteParser(fetcher)
|
|
82
|
+
const emote = emoteFetcher.emotes.get('Kappa')
|
|
83
|
+
|
|
84
|
+
console.log(emote.toLink({ size: 2 })) // <- Uses an object!
|
|
85
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/25/default/dark/3.0
|
|
86
|
+
|
|
87
|
+
console.log(parser.parse('Hello CoolCat!')) // <- Doesn't require :colons: and returns HTML by default!
|
|
88
|
+
// Hello <img alt="CoolCat" title="CoolCat" class="twitch-emote" src="https://static-cdn.jtvnw.net/emoticons/v2/58127/default/dark/1.0">
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
</details>
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
## Prerequisites
|
|
95
|
+
|
|
96
|
+
To fetch "native" Twitch emotes, you need to [create an app here](https://dev.twitch.tv/console/apps/create), it's free.
|
|
97
|
+
If you are only using BetterTTV, FrankerFaceZ and 7TV you don't need to provide Twitch app keys.
|
|
98
|
+
|
|
99
|
+
You must use a Twitch user ID instead of the username to fetch users' emotes.
|
|
100
|
+
You can use [this page to manually convert them](https://s.kdy.ch/twitchid/).
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## Install
|
|
104
|
+
|
|
105
|
+
From [npm] ([browse on npmx]; use the `@next` tag):
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
npm install @mkody/twitch-emoticons@next
|
|
109
|
+
# or
|
|
110
|
+
pnpm add @mkody/twitch-emoticons@next
|
|
111
|
+
# or
|
|
112
|
+
yarn add @mkody/twitch-emoticons@next
|
|
113
|
+
# or
|
|
114
|
+
deno add npm:@mkody/twitch-emoticons@next
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
From [jsr]:
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
npx jsr add @mkody/twitch-emoticons
|
|
121
|
+
# or
|
|
122
|
+
pnpm add jsr:@mkody/twitch-emoticons
|
|
123
|
+
# or
|
|
124
|
+
yarn add jsr:@mkody/twitch-emoticons
|
|
125
|
+
# or (version has to be specified while it's a pre-release)
|
|
126
|
+
deno add jsr:@mkody/twitch-emoticons@3.0.0-beta.1
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
[npm]: https://www.npmjs.com/package/@mkody/twitch-emoticons/v/3.0.0-beta.1
|
|
130
|
+
[browse on npmx]: https://npmx.dev/package/@mkody/twitch-emoticons/v/3.0.0-beta.1
|
|
131
|
+
[jsr]: https://jsr.io/@mkody/twitch-emoticons@3.0.0-beta.1
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
## Quick docs
|
|
135
|
+
|
|
136
|
+
<details>
|
|
137
|
+
<summary>Click here to toggle the docs</summary>
|
|
138
|
+
|
|
139
|
+
Here's some quick documentation to explain our two classes, the principal methods, and the settings.
|
|
140
|
+
|
|
141
|
+
> **NOTE:**
|
|
142
|
+
> If you want a more complete documentation, see: https://mkody.github.io/twitch-emoticons/
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
### Grab emotes with `EmoteFetcher`
|
|
146
|
+
|
|
147
|
+
First, you need to load a list of emotes, and for that you create a new `EmoteFetcher` object:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
const fetcher = new EmoteFetcher({
|
|
151
|
+
// If you want to use emotes from twitch.tv, you'll need to be authenticated to use their API. You have two options:
|
|
152
|
+
// Option 1: Provide your app ID and secret here (get them at https://dev.twitch.tv/console/apps).
|
|
153
|
+
twitchAppID, // <string>
|
|
154
|
+
twitchAppSecret, // <string>
|
|
155
|
+
// Option 2: If you need a different way to auth or already use `@twurple/api`, you can provide your ApiClient object here.
|
|
156
|
+
apiClient, // <ApiClient>
|
|
157
|
+
|
|
158
|
+
// Force emotes to be static (non-animated).
|
|
159
|
+
forceStatic, // <boolean> - Default: false
|
|
160
|
+
|
|
161
|
+
// Theme mode (background color) preference for Twitch emotes.
|
|
162
|
+
twitchThemeMode // <'dark' | 'light'> - Default: 'dark'
|
|
163
|
+
})
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
And then you need to make the calls to load what you want.
|
|
167
|
+
|
|
168
|
+
There is one method per platform; all of them return Promises (so you can use `await` or callback chains):
|
|
169
|
+
|
|
170
|
+
- `fetcher.fetchTwitchEmotes()`
|
|
171
|
+
- `fetcher.fetchBTTVEmotes()`
|
|
172
|
+
- `fetcher.fetchFFZEmotes()`
|
|
173
|
+
- `fetcher.fetchSevenTVEmotes()`
|
|
174
|
+
|
|
175
|
+
The first parameter is the Twitch user ID of the channel you want to load emotes from.
|
|
176
|
+
If not provided or it's `null`/falsy, it loads what we call "global emotes", which are available to all users of the platform.
|
|
177
|
+
|
|
178
|
+
Do note that `fetchSevenTVEmotes()` accepts a second parameter with an object:
|
|
179
|
+
|
|
180
|
+
- `format`: Set the image type used, either `webp` or `avif`. Default: `webp`
|
|
181
|
+
|
|
182
|
+
So if you want to load all global emotes, you can do it that way:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
await fetcher.fetchTwitchEmotes()
|
|
186
|
+
await fetcher.fetchBTTVEmotes()
|
|
187
|
+
await fetcher.fetchFFZEmotes()
|
|
188
|
+
await fetcher.fetchSevenTVEmotes(null, { format: 'avif' }) // Example of loading images in AVIF here
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
### Parse strings to include emotes with `EmoteParser`
|
|
193
|
+
|
|
194
|
+
And now that we have our list of emotes that we can expect to find, let's use it!
|
|
195
|
+
Let's create our `EmoteParser` object:
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
const parser = new EmoteParser(
|
|
199
|
+
// The first parameter is our EmoteFetcher, the object that holds the list of fetched emotes.
|
|
200
|
+
fetcher, // <EmoteFetcher>
|
|
201
|
+
|
|
202
|
+
// The second parameter is an *optional* object with settings.
|
|
203
|
+
{
|
|
204
|
+
// What output should be used when you parse messages? There are two ways to set that up:
|
|
205
|
+
// Option 1: Use one of the provided templates:
|
|
206
|
+
// - `html`: `<img alt="{name}" title="{name}" class="twitch-emote" src="{link}">`
|
|
207
|
+
// - `markdown`: ``
|
|
208
|
+
// - `bbcode`: `[img]{link}[/img]`
|
|
209
|
+
// - `plain`: `{link}`
|
|
210
|
+
type, // <'html' | 'markdown' | 'bbcode' | 'plain'> - Default: 'html'
|
|
211
|
+
// Option 2: Make your own template; it has priority over option 1. You can use those: `{link}`, `{name}`, `{size}`, `{creator}`.
|
|
212
|
+
template, // <string> - Default: ''
|
|
213
|
+
|
|
214
|
+
// You can customize the regular expression used to find possible emotes.
|
|
215
|
+
match, // <RegExp>
|
|
216
|
+
},
|
|
217
|
+
)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Now that you have an EmoteParser object, you can do two things with it:
|
|
221
|
+
look up an `Emote` or parse text and get the output as set by `type` or `template`.
|
|
222
|
+
|
|
223
|
+
To grab a specific emote, you can do `fetcher.emotes.get('...')`, with the case-sensitive name of the emote in place of the ellipsis.
|
|
224
|
+
From there, you can read properties like `.code`, `.animated`, `.imageType`, or `.type`,
|
|
225
|
+
but you can also use the `.toLink()` method to… get a link!
|
|
226
|
+
|
|
227
|
+
To parse text, you use the `parser.parse()` method.
|
|
228
|
+
The first parameter is the input text. There's also an optional second parameter
|
|
229
|
+
where you can provide a few settings (which can overwrite the same ones set in the `EmoteFetcher`).
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
const parsed = parser.parse(
|
|
233
|
+
// The text to parse.
|
|
234
|
+
text, // <string>
|
|
235
|
+
|
|
236
|
+
// The second parameter is an *optional* object with the settings.
|
|
237
|
+
{
|
|
238
|
+
// Size (scale) for emotes. It varies by provider, and not all share the same resolution in pixels. Play with this value if you'd like, but no guarantees!
|
|
239
|
+
size, // <number>
|
|
240
|
+
|
|
241
|
+
// Force emotes to be static (non-animated).
|
|
242
|
+
forceStatic, // <boolean> - Default: what's in EmoteFetcher or false
|
|
243
|
+
|
|
244
|
+
// Theme mode (background color) preference for Twitch emotes.
|
|
245
|
+
twitchThemeMode // <'dark' | 'light'> - Default: what's in EmoteFetcher or 'dark'
|
|
246
|
+
},
|
|
247
|
+
)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
And that's it for the essentials!
|
|
251
|
+
You can go through the examples below if you need to see more complete code and direct usage.
|
|
252
|
+
|
|
253
|
+
</details>
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
## Examples
|
|
257
|
+
|
|
258
|
+
<details>
|
|
259
|
+
<summary>Click here to toggle the examples</summary>
|
|
260
|
+
|
|
261
|
+
### 1. Basic Twitch emote parsing
|
|
262
|
+
|
|
263
|
+
```js
|
|
264
|
+
import { EmoteFetcher, EmoteParser } from '@mkody/twitch-emoticons'
|
|
265
|
+
|
|
266
|
+
const fetcher = new EmoteFetcher({
|
|
267
|
+
// Put your Twitch app keys here.
|
|
268
|
+
twitchAppID: '<your app ID>',
|
|
269
|
+
twitchAppSecret: '<your app secret>',
|
|
270
|
+
})
|
|
271
|
+
const parser = new EmoteParser(fetcher, {
|
|
272
|
+
type: 'markdown', // Can be `html` (default), `markdown`, `bbcode`, or `plain`.
|
|
273
|
+
// You can also set your own output, see example 3.
|
|
274
|
+
match: /:(.+?):/g, // This means your emotes must be between colons (:Kappa:).
|
|
275
|
+
// The default is /(\w+)/g and matches any word characters,
|
|
276
|
+
// similar to regular Twitch chat.
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
await fetcher.fetchTwitchEmotes(null) // `null` or a missing parameter will load "global" emotes.
|
|
280
|
+
|
|
281
|
+
const kappa = fetcher.emotes.get('Kappa').toLink()
|
|
282
|
+
console.log(kappa)
|
|
283
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/25/default/dark/1.0
|
|
284
|
+
|
|
285
|
+
const text = 'Hello :CoolCat:!'
|
|
286
|
+
const parsed = parser.parse(text)
|
|
287
|
+
console.log(parsed)
|
|
288
|
+
// Hello !
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
### 2. Bring your own `@twurple/api`
|
|
293
|
+
|
|
294
|
+
If you already use [Twurple](https://twurple.js.org/) in your project and manage authentication
|
|
295
|
+
(i.e., with user tokens), you can reuse it in this project by setting `apiClient` with your
|
|
296
|
+
[ApiClient](https://twurple.js.org/reference/api/classes/ApiClient.html) object.
|
|
297
|
+
|
|
298
|
+
```js
|
|
299
|
+
import { ApiClient } from '@twurple/api'
|
|
300
|
+
import { StaticAuthProvider } from '@twurple/auth'
|
|
301
|
+
import { EmoteFetcher } from '@mkody/twitch-emoticons'
|
|
302
|
+
|
|
303
|
+
const authProvider = new StaticAuthProvider('<your app ID>', '<access token>')
|
|
304
|
+
const myCustomApiClient = new ApiClient({ authProvider })
|
|
305
|
+
|
|
306
|
+
const fetcher = new EmoteFetcher({
|
|
307
|
+
apiClient: myCustomApiClient,
|
|
308
|
+
})
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
### 3. All providers, global + channel, custom template and match pattern
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
import { EmoteFetcher, EmoteParser } from '@mkody/twitch-emoticons'
|
|
316
|
+
|
|
317
|
+
// Your channel ID
|
|
318
|
+
const channelId = 44317909
|
|
319
|
+
|
|
320
|
+
const fetcher = new EmoteFetcher({
|
|
321
|
+
twitchAppID: '<your app ID>',
|
|
322
|
+
twitchAppSecret: '<your app secret>',
|
|
323
|
+
forceStatic: false,
|
|
324
|
+
twitchThemeMode: 'dark',
|
|
325
|
+
})
|
|
326
|
+
const parser = new EmoteParser(fetcher, {
|
|
327
|
+
// Custom HTML format
|
|
328
|
+
template: '<img class="emote" alt="{name}" src="{link}">',
|
|
329
|
+
// Otherwise, just use our provided template
|
|
330
|
+
// type: 'html',
|
|
331
|
+
// Matches words (like \w) but also dashes
|
|
332
|
+
match: /([a-zA-Z0-9_\-]+)/g,
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
// Fetch all emotes at once and wait for all of them to complete
|
|
336
|
+
Promise.all([
|
|
337
|
+
// Twitch global
|
|
338
|
+
fetcher.fetchTwitchEmotes(),
|
|
339
|
+
// Twitch channel
|
|
340
|
+
fetcher.fetchTwitchEmotes(channelId),
|
|
341
|
+
// BTTV global
|
|
342
|
+
fetcher.fetchBTTVEmotes(),
|
|
343
|
+
// BTTV channel
|
|
344
|
+
fetcher.fetchBTTVEmotes(channelId),
|
|
345
|
+
// 7TV global
|
|
346
|
+
fetcher.fetchSevenTVEmotes(),
|
|
347
|
+
// 7TV channel
|
|
348
|
+
fetcher.fetchSevenTVEmotes(channelId),
|
|
349
|
+
// FFZ global
|
|
350
|
+
fetcher.fetchFFZEmotes(),
|
|
351
|
+
// FFZ channel
|
|
352
|
+
fetcher.fetchFFZEmotes(channelId),
|
|
353
|
+
])
|
|
354
|
+
.then(() => {
|
|
355
|
+
const globalEmotes = parser.parse('EZ Clap way too easy LUL now for the last boss monkaS LaterSooner')
|
|
356
|
+
console.log(globalEmotes)
|
|
357
|
+
// <img class="emote" alt="EZ" src="https://cdn.7tv.app/emote/63071b80942ffb69e13d700f/1x.webp"> <img class="emote" alt="Clap" src="https://cdn.7tv.app/emote/62fc0a0c4a75fd54bd3520a9/1x.webp"> way too easy <img class="emote" alt="LUL" src="https://static-cdn.jtvnw.net/emoticons/v2/425618/default/dark/1.0"> now for the last boss <img class="emote" alt="monkaS" src="https://cdn.betterttv.net/emote/56e9f494fff3cc5c35e5287e/1x.webp"> <img class="emote" alt="LaterSooner" src="https://cdn.frankerfacez.com/emote/149346/1">
|
|
358
|
+
|
|
359
|
+
const channelEmotes = parser.parse('KEKW that was 3Head TeriPoint')
|
|
360
|
+
console.log(channelEmotes)
|
|
361
|
+
// <img class="emote" alt="KEKW" src="https://cdn.betterttv.net/emote/5e9c6c187e090362f8b0b9e8/1x.webp"> that was <img class="emote" alt="3Head" src="https://cdn.frankerfacez.com/emote/274406/1"> <img class="emote" alt="TeriPoint" src="https://cdn.7tv.app/emote/61dc299b600369a98b38ebef/1x.webp">
|
|
362
|
+
})
|
|
363
|
+
.catch((err) => {
|
|
364
|
+
console.error('Error loading emotes...')
|
|
365
|
+
console.error(err)
|
|
366
|
+
})
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
### 4. Force static images
|
|
371
|
+
|
|
372
|
+
All providers let you ask to only get static versions of their emotes.
|
|
373
|
+
|
|
374
|
+
By default, we allow animated emotes to be used,
|
|
375
|
+
but you can override this at the `EmoteFetcher` level:
|
|
376
|
+
|
|
377
|
+
```js
|
|
378
|
+
import { EmoteFetcher } from '@mkody/twitch-emoticons'
|
|
379
|
+
|
|
380
|
+
const fetcher = new EmoteFetcher({
|
|
381
|
+
twitchAppID: '<your app ID>',
|
|
382
|
+
twitchAppSecret: '<your app secret>',
|
|
383
|
+
forceStatic: true, // <- Here!
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
await fetcher.fetchTwitchEmotes()
|
|
387
|
+
const dinoDance = fetcher.emotes.get('DinoDance').toLink()
|
|
388
|
+
console.log(dinoDance)
|
|
389
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/emotesv2_dcd06b30a5c24f6eb871e8f5edbd44f7/static/dark/1.0
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
> **NOTE:**
|
|
393
|
+
> *Q:* Why set this to the `EmoteFetcher` and not `EmoteParser`?
|
|
394
|
+
> *A:* Because `Emote.toLink()` (that you get from the fetcher) uses that info!
|
|
395
|
+
|
|
396
|
+
It is also possible to force that using the `Emote`'s `toLink()` method:
|
|
397
|
+
|
|
398
|
+
```js
|
|
399
|
+
const dinoDance = fetcher.emotes.get('DinoDance').toLink({ forceStatic: true })
|
|
400
|
+
console.log(dinoDance)
|
|
401
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/emotesv2_dcd06b30a5c24f6eb871e8f5edbd44f7/static/dark/1.0
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
Or when using the `EmoteParser`'s `parse()`:
|
|
405
|
+
|
|
406
|
+
```js
|
|
407
|
+
const dinoDance = parser.parse('DinoDance', { forceStatic: true })
|
|
408
|
+
console.log(dinoDance)
|
|
409
|
+
// <img alt="DinoDance" title="DinoDance" class="twitch-emote" src="https://static-cdn.jtvnw.net/emoticons/v2/emotesv2_dcd06b30a5c24f6eb871e8f5edbd44f7/static/dark/1.0">
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
> **WARNING:**
|
|
413
|
+
> Forcing static images might make the `imageType` of the `Emote` not match with your expectations!
|
|
414
|
+
> (Twitch: `gif` => `png`; BTTV: `webp` => `png`.)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
### 5. Twitch "theme mode" preference
|
|
418
|
+
|
|
419
|
+
Some Twitch emotes are optimized for light or dark backgrounds.
|
|
420
|
+
|
|
421
|
+
By default, emotes are fetched for dark backgrounds,
|
|
422
|
+
but you can specify a preference at the `EmoteFetcher` level:
|
|
423
|
+
|
|
424
|
+
```js
|
|
425
|
+
import { EmoteFetcher } from '@mkody/twitch-emoticons'
|
|
426
|
+
|
|
427
|
+
// For dark backgrounds (default)
|
|
428
|
+
const fetcherDark = new EmoteFetcher({
|
|
429
|
+
twitchAppID: '<your app ID>',
|
|
430
|
+
twitchAppSecret: '<your app secret>',
|
|
431
|
+
twitchThemeMode: 'dark', // <- Here!
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
// For light backgrounds
|
|
435
|
+
const fetcherLight = new EmoteFetcher({
|
|
436
|
+
twitchAppID: '<your app ID>',
|
|
437
|
+
twitchAppSecret: '<your app secret>',
|
|
438
|
+
twitchThemeMode: 'light', // <- Here!
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
await fetcherLight.fetchTwitchEmotes()
|
|
442
|
+
const kappa = fetcherLight.emotes.get('Kappa').toLink()
|
|
443
|
+
console.log(kappa)
|
|
444
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/25/default/light/1.0
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
> **NOTE:**
|
|
448
|
+
> *Q:* Why set this to the `EmoteFetcher` and not `EmoteParser`?
|
|
449
|
+
> *A:* Because `Emote.toLink()` (that you get from the fetcher) uses that info!
|
|
450
|
+
|
|
451
|
+
It is also possible to force that using the `Emote`'s `toLink()` method:
|
|
452
|
+
|
|
453
|
+
```js
|
|
454
|
+
const kappa = fetcher.emotes.get('Kappa').toLink({ themeMode: 'light' })
|
|
455
|
+
console.log(kappa)
|
|
456
|
+
// https://static-cdn.jtvnw.net/emoticons/v2/25/default/light/1.0
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
Or when using the `EmoteParser`'s `parse()`:
|
|
460
|
+
|
|
461
|
+
```js
|
|
462
|
+
const kappa = parser.parse('Kappa', { themeMode: 'light' })
|
|
463
|
+
console.log(kappa)
|
|
464
|
+
// <img alt="Kappa" title="Kappa" class="twitch-emote" src="https://static-cdn.jtvnw.net/emoticons/v2/25/default/light/1.0">
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
### 6. 7TV formats
|
|
469
|
+
|
|
470
|
+
7TV v3 delivers emotes in either WEBP or AVIF.
|
|
471
|
+
|
|
472
|
+
By default we'll return WEBP emotes, but you can override this.
|
|
473
|
+
|
|
474
|
+
```js
|
|
475
|
+
// (setup)
|
|
476
|
+
import { EmoteFetcher } from '@mkody/twitch-emoticons'
|
|
477
|
+
const fetcher = new EmoteFetcher()
|
|
478
|
+
|
|
479
|
+
// Fetch global emotes in AVIF (channel id has to be `null` for global)
|
|
480
|
+
await fetcher.fetchSevenTVEmotes(null, { format: 'avif' })
|
|
481
|
+
|
|
482
|
+
// Fetch 0kody's emotes with the package's default format (WEBP)
|
|
483
|
+
await fetcher.fetchSevenTVEmotes(44317909)
|
|
484
|
+
|
|
485
|
+
// ... which is currently the same as
|
|
486
|
+
await fetcher.fetchSevenTVEmotes(44317909, { format: 'webp' })
|
|
487
|
+
|
|
488
|
+
// Fetch Anatole's emotes in AVIF
|
|
489
|
+
await fetcher.fetchSevenTVEmotes(24377667, { format: 'avif' })
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
### 7. Export and import emote data
|
|
494
|
+
|
|
495
|
+
This can be useful to save the emotes in a cache or for offline content.
|
|
496
|
+
|
|
497
|
+
```js
|
|
498
|
+
// (setup)
|
|
499
|
+
import { EmoteFetcher } from '@mkody/twitch-emoticons'
|
|
500
|
+
const fetcher = new EmoteFetcher()
|
|
501
|
+
|
|
502
|
+
// First fetch some emotes
|
|
503
|
+
await fetcher.fetchSevenTVEmotes(null, { format: 'avif' })
|
|
504
|
+
|
|
505
|
+
// Then you can use .toObject() on an `Emote` to export its data.
|
|
506
|
+
// Here's a map to get them all in a single array.
|
|
507
|
+
const emotes = fetcher.emotes.map((emote) => emote.toObject())
|
|
508
|
+
|
|
509
|
+
// Later, with or without a fresh `EmoteFetcher`, you can use .fromObject() on the fetcher.
|
|
510
|
+
fetcher.fromObject(emotes)
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
> **NOTE:**
|
|
514
|
+
> For offline content, you'll still need to download emotes and proxy their URLs.
|
|
515
|
+
|
|
516
|
+
</details>
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
## Links
|
|
520
|
+
|
|
521
|
+
- [Github](https://github.com/mkody/twitch-emoticons)
|
|
522
|
+
- [Documentation](https://mkody.github.io/twitch-emoticons/)
|
|
523
|
+
- [Changelog](https://github.com/mkody/twitch-emoticons/releases)
|
|
524
|
+
|
|
525
|
+
This library uses the following:
|
|
526
|
+
|
|
527
|
+
- [Twurple](https://twurple.js.org/) and the [Twitch API](https://dev.twitch.tv/)
|
|
528
|
+
- [BetterTTV API](https://betterttv.com/developers/api)
|
|
529
|
+
- [FrankerFaceZ API](https://api.frankerfacez.com/docs/)
|
|
530
|
+
- [7TV API](https://7tv.io/)
|