Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. const tmi = require('tmi.js');
  2. const appConfig = require('../config');
  3. const songRequest = require('./songRequest');
  4. const rank = require('./rank');
  5. const gamble = require('./gamble');
  6. const command = require('./command');
  7.  
  8. const channel = appConfig.twitchChannel;
  9.  
  10. const config = {
  11. options: {
  12. debug: true //TODO: turn this off
  13. },
  14. connection: {
  15. cluster: 'aws',
  16. reconnect: true
  17. },
  18. identity: {
  19. username: appConfig.botOauth.username,
  20. password: appConfig.botOauth.oAuthKey
  21. },
  22. channels: [channel]
  23. };
  24.  
  25. const client = null;
  26.  
  27. const bot = {
  28. socketApi: null,
  29.  
  30. connect: () => {
  31. client = new tmi.client(config);
  32. client.connect();
  33.  
  34. init();
  35. }
  36. };
  37.  
  38. function init() {
  39. rank.init();
  40.  
  41. client.on("connected", function (address, port) {
  42. // client.say(channel, 'Bienvenidos al canal.');
  43. });
  44.  
  45. client.on("chat", function (channel, userstate, message, self) {
  46. const isModerator = userstate.mod;
  47. const isBroadcaster = userstate.badges && userstate.badges.broadcaster;
  48. const isModeratorOrBroadcaster = isModerator || isBroadcaster;
  49.  
  50. // Don't listen to my own messages..
  51. if (self) return;
  52.  
  53. const user = userstate['display-name'];
  54.  
  55. if (message.includes('!songrequest') || message.includes('!sr')) {
  56. const query = message.replace('!songrequest', '').replace('!sr', '').trim();
  57.  
  58. if (query) {
  59. songRequest.addSong(user, query, function(songTitle) {
  60. client.say(channel, 'La canción ' + songTitle + ' ha sido agregada a la lista de reproducción por ' + user);
  61. });
  62. }
  63. }
  64.  
  65. if (message.includes('!skip')) {
  66.  
  67. songRequest.getCurrentSong(function(currentSong) {
  68. if (currentSong) {
  69. let skips = [];
  70. if (currentSong.skips) {
  71. skips = JSON.parse(currentSong.skips);
  72. }
  73.  
  74. if (!skips.includes(user)) {
  75. skips.push(user);
  76.  
  77. if (skips.length < songRequest.skipLimit) {
  78. songRequest.updateSong(currentSong, skips);
  79. } else {
  80. songRequest.deleteSong(currentSong._id, function() {
  81. bot.socketApi.sendMessage('!skip', true);
  82. });
  83. }
  84.  
  85. client.say(channel, user + ' ha votado por saltar la canción actual. Votos: ' + skips.length + '/' + songRequest.skipLimit);
  86. }
  87. }
  88. });
  89. }
  90.  
  91. if (message.includes('!volume') && isModeratorOrBroadcaster) {
  92. if (bot.socketApi) {
  93. const volume = message.replace('!volume', '').trim();
  94. if (volume.length && volume >= 0 && volume <= 100) {
  95. bot.socketApi.sendMessage('!volume', volume);
  96. client.say(channel, user + " ha cambiado el volumen de la música a " + volume);
  97. }
  98. }
  99. }
  100.  
  101. if (message.includes('!stop') && isModeratorOrBroadcaster) {
  102. if (bot.socketApi) {
  103. bot.socketApi.sendMessage('!stop', true);
  104. client.say(channel, user + " ha detenido la canción actual." );
  105. }
  106. }
  107.  
  108. if (message.includes('!play') && isModeratorOrBroadcaster) {
  109. if (bot.socketApi) {
  110. bot.socketApi.sendMessage('!play', true);
  111. }
  112. }
  113.  
  114. if (message.includes('!pause') && isModeratorOrBroadcaster) {
  115. if (bot.socketApi) {
  116. bot.socketApi.sendMessage('!pause', true);
  117. client.say(channel, user + " ha pausado la canción actual." );
  118. }
  119. }
  120.  
  121. if (message.includes('!currentsong')) {
  122. songRequest.getCurrentSong(function(currentSong) {
  123. if (currentSong) {
  124. client.say(channel, "La canción actual es " + currentSong.title + " y fue agregada por " + currentSong.userName);
  125. }
  126. });
  127. }
  128.  
  129. if (message.includes('!points')) {
  130. rank.getPoints(user, function(points) {
  131. client.say(channel, user + ' tiene ' + points + ' puntos.');
  132. });
  133. }
  134.  
  135. if (message.includes('!gamble')) {
  136. const points = message.replace('!gamble', '').trim();
  137. command.get('gamble', function (data) {
  138. if (data && data.enabled) {
  139. if (parseInt(points) > 0) {
  140. gamble.run(user, points, function(result, win, points, currentPoints, nextGamble) {
  141. if (nextGamble) {
  142. client.say(channel, user + ' no puedes apostar en este momento, debes esperar al menos '+ nextGamble +' minutos.');
  143. } else if (result === -1) {
  144. client.say(channel, user + ' no tiene los puntos suficientes para apostar.');
  145. } else {
  146. const outcome = win ? ' ganó ' : ' perdió ',
  147. msg = 'Cayó ' + result + '. ' + user + outcome + points + ' y ahora tiene ' + currentPoints + ' puntos.';
  148. client.say(channel, msg);
  149. }
  150. });
  151. }
  152. }
  153. });
  154. }
  155.  
  156. });
  157.  
  158. client.on("hosted", function (channel, username, viewers, autohost) {
  159. client.say(channel, "Gracias " + username + " por darnos host. Todos denle follow a " +
  160. username + " (" + channel + ").");
  161. });
  162. }
  163.  
  164. module.exports = bot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement