1.1.3 • Published 4 months ago

twitch-emote v1.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

twitch-emote

npm i twitch-emote

initialization

async function initCache(
    channels?: string[],
    settings?: Partial<Settings>
): void

this function must be ran first (and awaited) before spliceMessage or splitMessage will work.

argumenttypedefaultdescription
channelsstring[][]array of channel names to load
settingsPartial<Settings>{}optional settings

Settings

keytypedefaultdescription
autoRefreshbooleantrueif true, run indefinitely and regularly check for new data
refreshIntervalnumber300000 (5 minutes)time in milliseconds
cachebooleantruecaching on the disk
cacheDirstring./cachedirectory for cache
logApiRatebooleantruelog data about api rate limits
maxRetryRateLimitnumber1maximum retry attempts when rate limited (the request retries again after the rate limit time so 1 should suffice)

usage

function spliceMessage<T>(
    message: string,
    channel: string,
    callback?: (emote: EmoteData) => string | T,
    withEmotes?: string,
    strictTwitchEmotes?: boolean
): (string | T)[]

spliceMessage returns an array with strings cut directly where the emotes are, including spaces between emotes and grouping words together.

example: ["emote:EZ", " ", "emote:clap", " too good"]

function splitMessage<T>(
    message: string,
    channel: string,
    callback?: (emote: EmoteData) => string | T,
    withEmotes?: string,
    strictTwitchEmotes?: boolean
): (string | T)[]

splitMessage returns an array of words with some words replaced with emote data.

example: ["emote:EZ", "emote:clap", "too", "good"]

argumenttypedefaultdescription
messagestringchat message
channelstringchannel name
callback(EmoteData) => string \| Thighest quality url stringconvert the emote data into something
withEmotesstring""native twitch emotes given via an IRC tag
strictTwitchEmotesbooleanfalseif true, native twitch emotes will only be applied through withEmotes; otherwise when false, all twitch and channel emotes will be checked for in the message (i.e. non-subs appear to have sub emotes)

types

export interface EmoteData {
    provider: Provider
    code: string
    urls: EmoteURL[]
}

export interface EmoteURL {
    size: '1x' | '2x' | '3x' | '4x'
    url: string
}

export enum Provider {
    'Twitch' = 0,
    '7TV' = 1,
    'BetterTTV' = 2,
    'FrankerFaceZ' = 3,
}

example

(see the examples folder for ready and functional examples)

simple

import { initCache, spliceMessage } from 'twitch-emote'

await initCache(['xqc'])

let message = spliceMessage('EZ Clap too good', 'xqc')
console.log(message)

/* 
 * [
 *  'https://cdn.betterttv.net/emote/5590b223b344e2c42a9e28e3/3x',
 *  ' ',
 *  'https://cdn.betterttv.net/emote/55b6f480e66682f576dd94f5/3x',
 *  ' too good'
 * ]
 */

let messageWords = splitMessage('EZ Clap too good', 'xqc')
console.log(messageWords)

/* 
 * [
 *  'https://cdn.betterttv.net/emote/5590b223b344e2c42a9e28e3/3x',
 *  'https://cdn.betterttv.net/emote/55b6f480e66682f576dd94f5/3x',
 *  'too'.
 *  'good'
 * ]
 */

advanced

import { initCache, spliceMessage } from 'twitch-emote'

await initCache(['xqc'])

let message = spliceMessage('EZ Clap too good', 'xqc', emote => {
    return `emote:${emote.code}`
})
console.log(message)

// [ 'emote:EZ', ' ', 'emote:Clap', ' too good' ]

let messageWords = splitMessage('EZ Clap too good', 'xqc', emote => {
    return `emote:${emote.code}`
})
console.log(messageWords)

// [ 'emote:EZ', 'emote:Clap', 'too', 'good' ]