Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3. const http = require('http').Server(app);
  4. const io = require('socket.io')(http);
  5. var path = require('path');
  6. const tmi = require('tmi.js');
  7. const fetch = require('node-fetch');
  8. const { EmoteFetcher, EmoteParser } = require('twitch-emoticons');
  9. const fetcher = new EmoteFetcher();
  10. info = require('./info.js')
  11. const parser = new EmoteParser(fetcher, {
  12.     type: 'markdown',
  13.     match: /:(.+?):/g
  14. });
  15. const headers = {
  16.     'Client-ID': info.key
  17. };
  18. //this is the function for making the helix calls as you can see
  19. function helix(endpoint, qs) {
  20.     const queryString = new URLSearchParams(qs);
  21.     const url = `https://api.twitch.tv/helix/${endpoint}?${queryString}`;
  22.     return fetch(url, { headers })
  23.     .then(res => res.json())
  24. }
  25.  
  26. function splice(start, end, insert, message){
  27.     startStr = message.slice(0, start);
  28.     console.log(startStr);
  29.     console.log(end)
  30.     endStr =  message.slice(end);
  31.     messageOut = startStr + insert + endStr
  32. }
  33.  
  34. function emoteParse(emoteInf, msg) {
  35.  
  36. }
  37.  
  38. function getUser(id) {
  39.     return helix('users', { id })
  40.     .then(({ data: [ user ] }) => user || null);
  41. }
  42.  
  43.  
  44.  
  45. app.use(express.static('public'))
  46.  
  47. app.get('/', function(req, res) {
  48.     res.render('index.ejs');
  49. });
  50.  
  51. var config = {
  52.     identity: {
  53.         username: "charja113",
  54.         password: info.oauth,
  55.     },
  56.     channels: [
  57.         'charja113'
  58.     ]
  59. };
  60.  
  61. function onConnectedHandler (addr, port) {
  62.     console.log(`* Connected to ${addr}:${port}`);
  63. };
  64.  
  65. function onMessageHandler (channel, tags, message, self) {
  66.  
  67.     const { 'user-name': username, 'display-name': displayName, 'user-id': userID, 'subscriber': sub, 'emotes': emote } = tags;
  68.  
  69.     if(emote != null) {
  70.         console.log(emote);
  71.         emoteStr = JSON.stringify(emote);
  72.         var buf = Buffer.from(JSON.stringify(emote));
  73.         var splitLoc = buf.indexOf(':');
  74.         var midLoc = splitLoc + 3;
  75.         var endLoc = buf.indexOf(']');
  76.         var dasLoc = buf.indexOf('-');
  77.         var emotenum = emoteStr.slice(2, splitLoc-1);
  78.         var startNum = emoteStr.slice(midLoc, dasLoc);
  79.         var endNum = emoteStr.slice(dasLoc+1, endLoc-1)
  80.         endNum = parseInt(endNum, 10) +1;
  81.         const emoturl = `<img class="profImg" src="https://static-cdn.jtvnw.net/emoticons/v1/${emotenum}/1.0" alt="${emotenum}" id="itemImg">`;
  82.         console.log(startNum);
  83.         console.log(endNum);
  84.         console.log(emotenum);
  85.         console.log(midLoc);
  86.         splice(startNum, endNum, emoturl, message);
  87.      
  88.         console.log(messageOut);
  89.         var message1 = messageOut;
  90.     }
  91.     else {
  92.         console.log('i think we messed up if theres an emote');
  93.         var message1 = message;
  94.     }
  95.  
  96.     console.log('twitch', `${displayName} : ${message1}`);
  97.     //console.log(tags);
  98.     getUser(userID)
  99.         .then(user => {
  100.         if(!user) {
  101.             console.log('User not found');
  102.         }
  103.         else {
  104.             const {
  105.                 id, display_name, login,
  106.                 broadcaster_type, view_count, profile_image_url
  107.             } = user;
  108.             const name = `[${id}] ${display_name} (${login})`;
  109.             const props = `${broadcaster_type}, ${view_count} view(s), image: ${profile_image_url}`;
  110.             console.log(`${name} -- ${props}`);
  111.             const profileElment = `<img class="profImg" src="${profile_image_url}" alt="null" id="itemImg">`
  112.             io.emit('twitch', `${ profileElment} ${displayName}: ${message1}`);
  113.         }
  114.     });
  115. };
  116.  
  117. const client = new tmi.client(config);
  118.  
  119. client.on('connected', onConnectedHandler);
  120. client.on('message', onMessageHandler)
  121.  
  122. client.connect();
  123.  
  124. const server = http.listen(80, function() {
  125.     console.log('listening on *:80');
  126. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement