Advertisement
caLLowCreation

Load Twitch Emotes

Apr 28th, 2020
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     let bttvEmotes = [];
  3.  
  4.     loadEmotes({ size: '2x', channels: ['callowcreation', 'marleyxo'] });
  5.  
  6.     function loadEmotes(emoteSets) {
  7.         $.getJSON("https://api.betterttv.net/2/emotes").done((data) => {
  8.             bttvEmotes = data.emotes;
  9.             for (let i = 0; i < emoteSets.channels.length; i++) {
  10.                 const channel = emoteSets.channels[i];
  11.                 $.getJSON(`https://api.betterttv.net/2/channels/${channel}`).done((data) => {
  12.                     const emotes = data.emotes.map((x) => {
  13.                         //console.log(`//cdn.betterttv.net/emote/${x.id}/2x`);
  14.                         return {
  15.                             url: `//cdn.betterttv.net/emote/${x.id}/2x`,
  16.                             width: x.width,
  17.                             height: x.height,
  18.                             imageType: x.imageType,
  19.                             regex: x.code,
  20.                             channel: x.channel
  21.                         }
  22.                     });
  23.                     bttvEmotes = bttvEmotes.concat(emotes);
  24.                 }).fail(() => { console.log(`${channel} bttv emote set not loaded`); });
  25.             }
  26.         }).fail(() => { console.log("Could not load bttv emote set"); });
  27.     }
  28.  
  29.     function formatBttvEmotes(message) {
  30.         for (let i = 0; i < bttvEmotes.length; i++) {
  31.             const emote = bttvEmotes[i];
  32.             try {
  33.                 if(typeof emote.regex === 'undefined') {
  34.                     console.log(emote);
  35.                     continue;
  36.                 }
  37.                 const re = new RegExp(`\\b${emote.regex}\\b`, "g");
  38.                 message = message.replace(re, `<img src="${emote.url}" />`);
  39.             } catch (ex) {
  40.                 //console.log(ex);
  41.             }
  42.         }
  43.         return message;
  44.     }
  45.  
  46.     function formatEmotes(message, emotes) {
  47.         let splitText = message.split('');
  48.         for (let i in emotes) {
  49.             const e = emotes[i];
  50.             for (let j in e) {
  51.                 let mote = e[j];
  52.                 if (typeof mote == 'string') {
  53.                     mote = mote.split('-');
  54.                     mote = [parseInt(mote[0]), parseInt(mote[1])];
  55.                     const length = mote[1] - mote[0],
  56.                         empty = Array.apply(null, new Array(length + 1)).map(() => { return '' });
  57.                     splitText = splitText.slice(0, mote[0]).concat(empty).concat(splitText.slice(mote[1] + 1, splitText.length));
  58.                     splitText.splice(mote[0], 1, '<img class="emoticon" src="http://static-cdn.jtvnw.net/emoticons/v1/' + i + '/3.0">');
  59.                 }
  60.             }
  61.         }
  62.         return splitText.join('');
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement