tmi-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

tmi-utils

JavaScript utilities for Twitch chat with tmi.js

This utility includes a message parser for the emote object provided by tmi.js on every chat message.

Install

npm i tmi-utils

Example Usage

import tmi from 'tmi.js';
import { getEmoteAsUrl, parseEmotesInMessage } from 'tmi-utils';

const client = new tmi.Client({ /* tmi options */ });
client.connect();

client.on('message', (channel, tags, message, self) => {
  // The emotes object is a mapping of emote id to a list
  // of it's positions in the message string. For example,
  //
  // message: staying on the map is hard LUL LUL wow
  // emotes : { 425618: ['27-29', '31-33'] }
  const { emotes } = tags

  // `parseEmotesInMessage` splits the message up into strings and emotes
  const parsedMessage = parseEmotesInMessage(emotes, message);

  // Then you can render the message with emotes however you like
  const message = document.createElement('span');

  // Safely append each part
  parsedMessage.forEach(({ type, value, raw }) => {
    if (type === 'emote') {
      const img = new Image();
      img.src = getEmoteAsUrl(value);
      img.alt = raw;
      img.title = raw;

      message.append(img);
    } else {
      message.append(value);
    }
  });

  document.querySelector('.message-list').append(message)
})

Package Sidebar

Install

npm i tmi-utils

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

11.5 kB

Total Files

15

Last publish

Collaborators

  • jzimz