Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. const tmi = require('tmi.js')
  2. const haikudos = require('haikudos')
  3. var net = require('net');
  4.  
  5. // Valid commands start with:
  6. let commandPrefix = '!'
  7. // Define configuration options:
  8. let opts = {
  9. identity: {
  10. username: 'BOT_NAME',
  11. password: 'oauth:' + 'YOUR_OAUTH_GOES_HERE'
  12. },
  13. channels: [
  14. 'deadmau5'
  15. ]
  16. }
  17.  
  18. // These are the commands the bot knows (defined below):
  19. let knownCommands = { echo, haiku }
  20.  
  21. // Function called when the "echo" command is issued:
  22. function echo (target, context, params) {
  23. // If there's something to echo:
  24. if (params.length) {
  25. // Join the params into a string:
  26. var msg = params.join(' ')
  27. // Interrupt attempted slash and dot commands:
  28. if (msg.charAt(0) == '/' || msg.charAt(0) == '.') {
  29. //msg = 'Nice try...'
  30. }
  31. // Send it back to the correct place:
  32. //sendMessage(target, context, msg)
  33. } else { // Nothing to echo
  34. console.log(`* Nothing to echo`)
  35. }
  36. }
  37.  
  38. // Function called when the "haiku" command is issued:
  39. function haiku (target, context) {
  40. // Generate a new haiku:
  41. haikudos((newHaiku) => {
  42. // Split it line-by-line:
  43. newHaiku.split('\n').forEach((h) => {
  44. // Send each line separately:
  45. //sendMessage(target, context, h)
  46. })
  47. })
  48. }
  49.  
  50. // Helper function to send the correct type of message:
  51. function sendMessage (target, context, message) {
  52. if (context['message-type'] === 'whisper') {
  53. client.whisper(target, message)
  54. } else {
  55. client.say(target, message)
  56. }
  57. }
  58.  
  59. // Create a client with our options:
  60. let client = new tmi.client(opts)
  61.  
  62. // Register our event handlers (defined below):
  63. client.on('message', onMessageHandler)
  64. client.on('connected', onConnectedHandler)
  65. client.on('disconnected', onDisconnectedHandler)
  66.  
  67. // Connect to Twitch:
  68. client.connect()
  69.  
  70. // Called every time a message comes in:
  71. function onMessageHandler (target, context, msg, self) {
  72. if (self) { return } // Ignore messages from the bot
  73.  
  74. // This isn't a command since it has no prefix:
  75. if (msg.substr(0, 1) !== commandPrefix) {
  76.  
  77. //var twmsg = `[${target} (${context['message-type']})] ${context.username}: ${msg}`
  78. var twmsg = `${context.username}: ${msg}`
  79.  
  80. var client = new net.Socket();
  81. client.setNoDelay();
  82. client.connect(1337, 'localhost', function(){
  83. //console.log('Connected to TD Server')
  84.  
  85. client.write(twmsg + "\r\n")
  86.  
  87. });
  88. client.on('error',()=>{
  89. console.log("An error has occured");
  90. console.log("Open TouchDesigner Server before starting.");
  91. });
  92. client.on('data', (data) => {
  93. console.log(data.toString());
  94. client.end();
  95. });
  96. client.on('end', () => {
  97. console.log('disconnected from server');
  98. });
  99.  
  100. console.log(twmsg)
  101. return
  102. }
  103.  
  104. // Split the message into individual words:
  105. const parse = msg.slice(1).split(' ')
  106. // The command name is the first (0th) one:
  107. const commandName = parse[0]
  108. // The rest (if any) are the parameters:
  109. const params = parse.splice(1)
  110.  
  111. // If the command is known, let's execute it:
  112. if (commandName in knownCommands) {
  113. // Retrieve the function by its name:
  114. const command = knownCommands[commandName]
  115. // Then call the command with parameters:
  116. command(target, context, params)
  117. console.log(`* Executed ${commandName} command for ${context.username}`)
  118. } else {
  119. console.log(`* Unknown command ${commandName} from ${context.username}`)
  120. }
  121. }
  122.  
  123. // Called every time the bot connects to Twitch chat:
  124. function onConnectedHandler (addr, port) {
  125. console.log(`* Connected to ${addr}:${port}`)
  126. }
  127.  
  128. // Called every time the bot disconnects from Twitch:
  129. function onDisconnectedHandler (reason) {
  130. console.log(`Disconnected: ${reason}`)
  131. process.exit(1)
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement