Advertisement
Guest User

Untitled

a guest
May 19th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const _ = require('lodash'), // Just to handle a couple of things more easily
  4. tmi = require('tmi.js'),
  5.  
  6. channel = ''; // Your channel
  7.  
  8. let mainConfig = {
  9. options: {
  10. debug: true // Debug to console
  11. },
  12. connection: {
  13. reconnect: true, // Make sure to reconnect on connection error
  14. secure: true // Secure servers are cooler
  15. },
  16. channels: [channel] // Yooouurrr channel
  17. },
  18. identities = {
  19. A: {
  20. username: '', // botA's username and password
  21. password: ''
  22. },
  23. B: {
  24. username: '', // botB's username and password
  25. password: ''
  26. },
  27. },
  28. botAConfig = _.defaults({ identity: identities.A }, mainConfig),
  29. botBConfig = _.defaults({ identity: identities.B }, mainConfig),
  30. botA = new tmi.client(botAConfig),
  31. botB = new tmi.client(botBConfig);
  32.  
  33. function reply(channel) {
  34. return message => this.say(channel, message);
  35. }
  36.  
  37. function handleCommand(chan, user, message) {
  38. let parameters = message.split(' ').filter(n => n),
  39. command = parameters.shift().toLowerCase().replace(/^!+/, ''),
  40.  
  41. replyA = reply.call(botA, chan), // Reply as botA
  42. replyB = reply.call(botB, chan); // Reply as botB
  43.  
  44. if(command == 'time') { // botA will respond to this
  45. let d = new Date();
  46. replyA(user.displayName + ', the time is ' + [
  47. (d.getHours()%12 || 12),
  48. d.getMinutes(),
  49. d.getSeconds()
  50. ]
  51. .map(n => n.toString().length == 1 ? '0' + n : n)
  52. .join(':'));
  53. }
  54. else if(command == 'date') { // botB will respond to this
  55. let d = new Date();
  56. replyB(user.displayName + ', the date is ' + [
  57. d.getMonth() + 1,
  58. d.getDate(),
  59. d.getFullYear()
  60. ]
  61. .join('/'));
  62. }
  63. }
  64.  
  65. function handleMessage(channel, _user, message, fromSelf) {
  66. if(fromSelf) { // Don't respond to self
  67. return false;
  68. }
  69. let chan = channel.replace('#', ''), // #channel -> channel
  70. user = _.mapKeys(_user, _.rearg(_.camelCase, 1)); // user.['user-type'] -> user.userType
  71. if(message.startsWith('!')) { // Possible commands
  72. handleCommand(chan, user, message); // Send to command handler
  73. }
  74. }
  75.  
  76. botA.on('message', handleMessage); // Only 1 bot needs to listen to the chat
  77.  
  78. [botA, botB].forEach(n => n.connect()); // Connect multiple bots
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement