Advertisement
Guest User

Untitled

a guest
Jul 4th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. const exec = require('child_process').exec;
  2. const spawn = require('child_process').spawn
  3.  
  4.  
  5. // I think twtich bot process has an issue without how much
  6. //data is being passed to stdout or stderr and I need
  7. //to specify how much data is allowed
  8.  
  9. twitchBot = exec('node ./twitchbot-api/index.js',
  10. function(error, stdout, stderr){
  11. console.log('stdout: ' + stdout);
  12. console.log('stderr: ' + stderr);
  13. if(error !== null){
  14. console.log('exec error: ' + error);
  15. }
  16.  
  17.  
  18. })
  19. twitchBot.stdout.on('data', (data) => {
  20. console.log(`twitchBot stdout:n${data}`);
  21. });
  22.  
  23. //whenver I recieve an error from quoteDataBase it also displays in the parent process
  24. twitchBot.stderr.on('data', (data) => {
  25. console.error(`twitchBot stderr:n${data}`);
  26. });
  27.  
  28. //this throws an error the first time I try a command saying not connected to server , I think I need to do this as exec
  29. // let twitchBot = spawn('node ./twitchbot-api/index.js',{
  30. // stdio: 'inherit',
  31. // shell: true,
  32. // detached: true,
  33. // })
  34.  
  35. // //twitchBot.unref();
  36.  
  37. // twitchBot.on('error', (error)=>{
  38. // console.log(`the erorr ${error}`)
  39. // })
  40.  
  41.  
  42.  
  43.  
  44. //starts up a child exec process for my quotes database
  45. let quoteDataBase = exec('node ./quotes-api/index.js',
  46. function(error, stdout, stderr){
  47. console.log('stdout: ' + stdout);
  48. console.log('stderr: ' + stderr);
  49. if(error !== null){
  50. console.log('exec error: ' + error);
  51. }
  52.  
  53.  
  54. })
  55.  
  56.  
  57. //whatever the child exec process quoteDatabase pushs to the standard out (console) displays on the parent process
  58. // aka the terminal I run apiStart from
  59. quoteDataBase.stdout.on('data', (data) => {
  60. console.log(`quoteDataBase stdout:n${data}`);
  61. });
  62.  
  63. //whenver I recieve an error from quoteDataBase it also displays in the parent process
  64. quoteDataBase.stderr.on('data', (data) => {
  65. console.error(`quoteDataBase stderr:n${data}`);
  66. });
  67.  
  68.  
  69. //starts up a child process for my user Authentication
  70. let AuthenDataBase = exec('node ./authen-api/index.js',
  71. function(error, stdout, stderr){
  72. console.log('stdout: ' + stdout);
  73. console.log('stderr: ' + stderr);
  74. if(error !== null){
  75. console.log('exec error: ' + error);
  76. }
  77.  
  78.  
  79. })
  80.  
  81. const tmi = require('tmi.js')
  82. const haikudos = require('haikudos')
  83. require('dotenv').config()
  84. require('es6-promise').polyfill();
  85. require('isomorphic-fetch');
  86. // Valid commands start with:
  87. let commandPrefix = '!'
  88. // Define configuration options:
  89. let opts = {
  90. identity: {
  91. username: process.env.user,
  92. password: process.env.pass
  93. },
  94. channels: [
  95. "dshrops1"
  96. ]
  97. }
  98.  
  99. // These are the commands the bot knows (defined below):
  100. let knownCommands = { echo, haiku, quote }
  101.  
  102. // Function called when the "echo" command is issued:
  103. function echo (target, context, params) {
  104. // If there's something to echo:
  105. if (params.length) {
  106. // Join the params into a string:
  107. const msg = params.join(' ')
  108. // Send it back to the correct place:
  109. sendMessage(target, context, msg)
  110. } else { // Nothing to echo
  111. console.log(`* Nothing to echo`)
  112. }
  113. }
  114.  
  115. // Function called when the "haiku" command is issued:
  116. function haiku (target, context) {
  117. // Generate a new haiku:
  118. haikudos((newHaiku) => {
  119. // Split it line-by-line:
  120. newHaiku.split('n').forEach((h) => {
  121. // Send each line separately:
  122. sendMessage(target, context, h)
  123. })
  124. })
  125. }
  126.  
  127. async function quote (target, context){
  128. //cant deploy this on AWS yet Untill I deploy my database api as well.
  129. let quote = await fetch('http://localhost:3006/random').then(resp =>resp.text())
  130. sendMessage(target,context,quote)
  131. }
  132.  
  133. // Helper function to send the correct type of message:
  134. function sendMessage (target, context, message) {
  135. if (context['message-type'] === 'whisper') {
  136. client.whisper(target, message)
  137. } else {
  138. client.say(target, message)
  139. }
  140. }
  141.  
  142.  
  143.  
  144. // Create a client with our options:
  145. let client = new tmi.client(opts)
  146.  
  147. // Register our event handlers (defined below):
  148. client.on('message', onMessageHandler)
  149. client.on('connected', onConnectedHandler)
  150. client.on('disconnected', onDisconnectedHandler)
  151.  
  152. // Connect to Twitch:
  153. client.connect()
  154.  
  155. // Called every time a message comes in:
  156. function onMessageHandler (target, context, msg, self) {
  157. if (self) { return } // Ignore messages from the bot
  158.  
  159. // This isn't a command since it has no prefix:
  160. if (msg.substr(0, 1) !== commandPrefix) {
  161. console.log(`[${target} (${context['message-type']})] ${context.username}: ${msg}`)
  162. return
  163. }
  164.  
  165. // Split the message into individual words:
  166. const parse = msg.slice(1).split(' ')
  167. // The command name is the first (0th) one:
  168. const commandName = parse[0]
  169. // The rest (if any) are the parameters:
  170. const params = parse.splice(1)
  171.  
  172. // If the command is known, let's execute it:
  173. if (commandName in knownCommands) {
  174. // Retrieve the function by its name:
  175. const command = knownCommands[commandName]
  176. // Then call the command with parameters:
  177. command(target, context, params)
  178. console.log(`* Executed ${commandName} command for ${context.username}`)
  179. } else {
  180. console.log(`* Unknown command ${commandName} from ${context.username}`)
  181. }
  182. }
  183.  
  184. // Called every time the bot connects to Twitch chat:
  185. function onConnectedHandler (addr, port) {
  186. console.log(`* Connected to ${addr}:${port}`)
  187. }
  188.  
  189. // Called every time the bot disconnects from Twitch:
  190. function onDisconnectedHandler (reason) {
  191. console.log(`Womp womp, disconnected: ${reason}`)
  192. process.exit(1)
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement