better way to parse message and emotes?
See original GitHub issueI was just curious, does anyone have a better method of parsing the message with the emotes array? Here’s how I do it, wondering how everyone else does it.
function parseMessage(message, emotes) {
var emoteArray = _.chain(emotes)
.map(function(emote, index) {
var charIndex = _.map(emote, function(chars) {
var indexes = chars.split("-");
return {
url: "http://static-cdn.jtvnw.net/emoticons/v1/" + index + "/1.0",
startIndex: parseInt(indexes[0]),
endIndex: parseInt(indexes[1]) + 1
};
});
return charIndex;
})
.flatten()
.sortBy(function(item) {
return -1 * item.startIndex;
})
.value();
if(emoteArray.length === 0) {
return message;
}
var newMessage = message;
_.each(emoteArray, function(emote) {
var emoteName = newMessage.substring(emote.startIndex, emote.endIndex);
var leftPart = newMessage.substring(0, emote.startIndex);
var middlePart = makeImage(emoteName, emote.url);
var rightPart = newMessage.substring(emote.endIndex);
newMessage = leftPart + middlePart + rightPart;
});
return newMessage;
}
function makeImage(name, url) {
return _s.sprintf("<img alt='%1$s' title='%1$s' src='%2$s' />", name, url);
}
It works by splitting the emote positions into a flattened array with every instance of said emote. Then I work backwards for every emote and string replace the position into an anchor tag (makeImage method).
Issue Analytics
- State:
- Created 8 years ago
- Comments:26 (24 by maintainers)
Top Results From Across the Web
How can I parse the emotes out of a Twitch IRC response into ...
I would like to parse an IRC message from Twitch to a list of dictionaries, accounting for emotes. Here is a sample of...
Read more >Parsing Twitch Emotes (JS) - Reddit
Currently i am making a Twitch site that counts the number of emotes sent in a user specified channel, and ranks them by...
Read more >Example Message Parser - Twitch Developers
The following example shows a simple parser that the example bot uses to parse Twitch IRC messages. The example parses many but not...
Read more >Simple TMI Emote Parse - npm
Bring back emotes in chat messages coming from TMI.js (Twitch). Latest version: 1.1.1, last published: a year ago.
Read more >Emote | npm.io
Emote Packages ; @mkody/twitch-emoticons. Gets Twitch, BTTV and FFZ emotes as well as parsing text to emotes! betterbttvemoji ; twitch-chat-cli. CLI for reading ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the ugly code that I had used in the previous version for a stream chat.
It essentially splits the text into individual characters and then sets all characters that are part of the emote into an empty character
''
and changes the first index into an<img>
tag. Doing it this way means going through each emote altering the indexes as given in one array, going through each emote however many times it needs to. Finally it joins it all together and returns it.Rough example:
messageWithEmotes comes out as:
I wrote a solution before finding this thread that is maybe helpful for some people.
My project relied on having an array of characters that were written to the screen like a typewriter. So it seemed like a good idea to break it up into text strings one character long, and emote ids represented as numbers. Then while typing, it was able to discern “ok this is a character, this is an emote” as it goes.
This finds all indexes of the given emote code, inside of a string. It’s basically
indexOf
but returns all indexes.The structure of my emotes is built somewhere else in my code, based on the user and the channel. It’s a plain object where keys are an available emote
code
and values are the emoteid
. For example it could look like this:I use the following function to build an array where emote codes exist at the indexes they were found in the string. I use this array while breaking the text up into characters later.
Then finally I break the string down into characters and emotes.
For me it’s exactly what I needed because then my typewriter effect just needs to go through every item in the
charArray
, check whether it is a number, and if so insert a emote. Otherwise just insert the text.