tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.56k stars 211 forks source link

Generate userstate.emotes (or emotes-raw) for self messages #149

Closed mccxiv closed 8 years ago

mccxiv commented 8 years ago

When listening to https://docs.tmijs.org/v1.0.0/Events.html#chat the userstate object won't contain any emotes for self messages.

Today we have the technology to make it happen! I wanted to make a PR but I'm too newb to figure out where things are in the codebase...

Here's the unoptimized function I'm using to generate them, if you need inspiration:

function generateEmotesProperty(message, twitchEmotes) {
  const sets = twitchEmotes.emoticon_sets;
  const emotes = {};
  Object.keys(sets).forEach(setId => {
    sets[setId].forEach(emote => {
      message.replace(new RegExp(emote.code, 'g'), (match, index) => {
        const start = index;
        const end = index + match.length - 1;
        emotes[emote.id] = emotes[emote.id] || [];
        emotes[emote.id].push(start + '-' + end);
        return match;
      });
    });
  });
  return emotes;
}

twitchEmotes being the data from this endpoint using the current user's emote sets.

AlcaDesign commented 8 years ago

The BetterTTV extension has code for this as it completely takes over the chat. Perhaps we could learn from how it's set up there.

Schmoopiie commented 8 years ago

Yes, it would be possible to add this feature to tmi.js.

I think there is an issue with your code though.. I'm pretty sure your function would generate two indexes if you try with "KappaKappa" but it shouldn't because it's not a valid emote.

mccxiv commented 8 years ago

Another issue with my implementation, twitch seems to assume the string has already been html escaped... Not sure if you took that into account in the commit

{
  id: 9,
  code: "\&lt\;3"
},
Schmoopiie commented 8 years ago

Oh, right.. I will fix this :+1:

mccxiv commented 8 years ago

Except it's not that simple, the character ranges should be for the unescaped version of the message. Realizing these things as I try to fix my code :cry:

Schmoopiie commented 8 years ago

Hmm, if I try to send test <3 Kappa.. the ranges seem to be okay:

{ badges: { broadcaster: '1' },
  color: '#FFFFFF',
  'display-name': 'Schmoopiie',
  'emote-sets': '0,27',
  mod: true,
  subscriber: false,
  turbo: false,
  'user-type': 'mod',
  'badges-raw': 'broadcaster/1',
  username: 'schmoopiie',
  emotes: { '9': [ '5-6' ], '25': [ '8-12' ] },
  'emotes-raw': '9:5-6/25:8-12',
  'message-type': 'chat' }
mccxiv commented 8 years ago

Cool, yours works :+1: Time for me to pull from #next

Schmoopiie commented 8 years ago

:+1:

Schmoopiie commented 8 years ago

Added in v1.0.1 and v1.1.0

Thanks :smile: