Guest User

Untitled

a guest
May 16th, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. var chatClient = function chatClient(options){
  2. this.username = options.username;
  3. this.password = options.password;
  4. this.channel = options.channel;
  5.  
  6. this.server = 'irc-ws.chat.twitch.tv';
  7. this.port = 443;
  8. }
  9.  
  10. chatClient.prototype.open = function open(){
  11. this.webSocket = new WebSocket('wss://' + this.server + ':' + this.port + '/', 'irc');
  12.  
  13. this.webSocket.onmessage = this.onMessage.bind(this);
  14. this.webSocket.onerror = this.onError.bind(this);
  15. this.webSocket.onclose = this.onClose.bind(this);
  16. this.webSocket.onopen = this.onOpen.bind(this);
  17. };
  18.  
  19. chatClient.prototype.onError = function onError(message){
  20. console.log('Error: ' + message);
  21. };
  22.  
  23. /* This is an example of a leaderboard scoring system. When someone sends a message to chat, we store
  24. that value in local storage. It will show up when you click Populate Leaderboard in the UI.
  25. */
  26. chatClient.prototype.onMessage = function onMessage(message){
  27. if(message !== null){
  28. var parsed = this.parseMessage(message.data);
  29. if(parsed !== null){
  30. console.log(parsed)
  31. if(parsed.command === "PRIVMSG") {
  32. userPoints = localStorage.getItem(parsed.username);
  33.  
  34. if(userPoints === null){
  35. localStorage.setItem(parsed.username, 10);
  36. }
  37. else {
  38. localStorage.setItem(parsed.username, parseFloat(userPoints) + 0.25);
  39. }
  40. } else if(parsed.command === "PING") {
  41. this.webSocket.send("PONG :" + parsed.message);
  42. }
  43. }
  44. }
  45. };
  46.  
  47. chatClient.prototype.message = function message(){
  48. var socket = this.webSocket;
  49.  
  50. console.log($(".message-text").val())
  51.  
  52. if (socket !== null && socket.readyState === 1) {
  53. console.log("socket not null")
  54. socket.send("PRIVMSG " + this.channel + " :" + $(".message-text").val() + "rn")
  55. console.log("PRIVMSG " + this.channel + " :" + $(".message-text").val())
  56. }
  57. }
  58.  
  59. chatClient.prototype.names = function names(){
  60. var socket = this.webSocket;
  61.  
  62. if (socket !== null && socket.readyState === 1) {
  63. socket.send('NAMES')
  64. }
  65. }
  66.  
  67. chatClient.prototype.onOpen = function onOpen(){
  68. var socket = this.webSocket;
  69.  
  70. if (socket !== null && socket.readyState === 1) {
  71. console.log('Initializing connection and authenticating...');
  72. console.log('username: ' + this.username);
  73. console.log('password: ' + this.password);
  74. console.log('channel: ' + this.channel);
  75.  
  76. socket.send('PASS ' + this.password);
  77. socket.send('NICK ' + this.username);
  78. socket.send('JOIN ' + this.channel);
  79. socket.send('CAP REQ :twitch.tv/tags');
  80. socket.send('CAP REQ :twitch.tv/commands');
  81. socket.send('CAP REQ :twitch.tv/membership');
  82. }
  83. };
  84.  
  85. chatClient.prototype.onClose = function onClose(){
  86. console.log('Disconnected from the chat server.');
  87. };
  88.  
  89. chatClient.prototype.close = function close(){
  90. if(this.webSocket){
  91. this.webSocket.close();
  92. }
  93. };
  94.  
  95. /* This is an example of an IRC message with tags. I split it across
  96. multiple lines for readability. The spaces at the beginning of each line are
  97. intentional to show where each set of information is parsed. */
  98.  
  99. //@badges=global_mod/1,turbo/1;color=#0D4200;display-name=TWITCH_UserNaME;emotes=25:0-4,12-16/1902:6-10;mod=0;room-id=1337;subscriber=0;turbo=1;user-id=1337;user-type=global_mod
  100. // :twitch_username!twitch_username@twitch_username.tmi.twitch.tv
  101. // PRIVMSG
  102. // #channel
  103. // :Kappa Keepo Kappa
  104.  
  105. chatClient.prototype.parseMessage = function parseMessage(rawMessage) {
  106. var parsedMessage = {
  107. message: null,
  108. tags: null,
  109. command: null,
  110. original: rawMessage,
  111. channel: null,
  112. username: null
  113. };
  114.  
  115. if(rawMessage[0] === '@'){
  116. var tagIndex = rawMessage.indexOf(' '),
  117. userIndex = rawMessage.indexOf(' ', tagIndex + 1),
  118. commandIndex = rawMessage.indexOf(' ', userIndex + 1),
  119. channelIndex = rawMessage.indexOf(' ', commandIndex + 1),
  120. messageIndex = rawMessage.indexOf(':', channelIndex + 1);
  121.  
  122. parsedMessage.tags = rawMessage.slice(0, tagIndex);
  123. parsedMessage.username = rawMessage.slice(tagIndex + 2, rawMessage.indexOf('!'));
  124. parsedMessage.command = rawMessage.slice(userIndex + 1, commandIndex);
  125. parsedMessage.channel = rawMessage.slice(commandIndex + 1, channelIndex);
  126. parsedMessage.message = rawMessage.slice(messageIndex + 1);
  127. } else if(rawMessage.startsWith("PING")) {
  128. parsedMessage.command = "PING";
  129. parsedMessage.message = rawMessage.split(":")[1];
  130. }
  131.  
  132. return parsedMessage;
  133. }
  134.  
  135. /* Builds out the top 10 leaderboard in the UI using a jQuery template. */
  136. function buildLeaderboard(){
  137. var chatKeys = Object.keys(localStorage),
  138. outputTemplate = $('#entry-template').html(),
  139. leaderboard = $('.leaderboard-output'),
  140. sortedData = chatKeys.sort(function(a,b){
  141. return localStorage[b]-localStorage[a]
  142. });
  143.  
  144. leaderboard.empty();
  145.  
  146. for(var i = 0; i < 10; i++){
  147. var viewerName = sortedData[i],
  148. template = $(outputTemplate);
  149.  
  150. template.find('.rank').text(i + 1);
  151. template.find('.user-name').text(viewerName);
  152. template.find('.user-points').text(localStorage[viewerName]);
  153.  
  154. leaderboard.append(template);
  155. }
  156. }
Add Comment
Please, Sign In to add comment