节点child_process和抽搐api

时间:2018-07-05 02:21:50

标签: javascript node.js process twitch

我正在尝试创建一个简单的脚本来运行一些节点文件,这些文件只是用于Quote数据库,Authentication和twitch bot的api。

报价和身份验证工作只是找到了,但是他们抽搐了bot并没有说它已连接到服务器,但是当它执行任何命令时,它会收到未处理的承诺拒绝或某些此类错误

但是,当我在自己的终端会话中分别运行twitch bot而不是从脚本运行它时,它运行良好,没有抛出错误。

为什么它不能在我创建的单独会话中工作,而不能在child_process中工作?

注意:我让child_process启动了一个新的shell,它仍然是同样的问题

过程脚本:

const exec = require('child_process').exec;
const spawn = require('child_process').spawn    


// I think twtich bot process has an issue without how much
//data is being passed to stdout or stderr and I need
//to specify how much data is allowed

twitchBot = exec('node ./twitchbot-api/index.js', 
function(error, stdout, stderr){
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if(error !== null){
        console.log('exec error: ' + error);
    }


})
twitchBot.stdout.on('data', (data) => {
 console.log(`twitchBot stdout:\n${data}`);
});

//whenver I recieve an error from quoteDataBase it also displays in the       parent process
twitchBot.stderr.on('data', (data) => {
 console.error(`twitchBot  stderr:\n${data}`);
});

//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
// let twitchBot = spawn('node ./twitchbot-api/index.js',{
//   stdio: 'inherit',
//    shell: true,
//    detached: true,
// })

// //twitchBot.unref();

// twitchBot.on('error', (error)=>{
//  console.log(`the erorr ${error}`)
// })




//starts up a child exec process for my quotes database
let quoteDataBase = exec('node ./quotes-api/index.js', 
function(error, stdout, stderr){
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if(error !== null){
        console.log('exec error: ' + error);
    }


})


//whatever the child exec process quoteDatabase pushs to the standard out (console) displays on the parent process 
// aka the terminal I run apiStart from
quoteDataBase.stdout.on('data', (data) => {
 console.log(`quoteDataBase stdout:\n${data}`);
});

//whenver I recieve an error from quoteDataBase it also displays in the  parent process
quoteDataBase.stderr.on('data', (data) => {
 console.error(`quoteDataBase  stderr:\n${data}`);
});


//starts up a child process for my user Authentication 
let AuthenDataBase = exec('node ./authen-api/index.js', 
 function(error, stdout, stderr){
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if(error !== null){
        console.log('exec error: ' + error);
    }


})

然后是twitch机器人代码:

const tmi = require('tmi.js')
const haikudos = require('haikudos')
require('dotenv').config()
require('es6-promise').polyfill();
require('isomorphic-fetch');
// Valid commands start with:
let commandPrefix = '!'
// Define configuration options:
let opts = {
 identity: {
   username: process.env.user,
   password: process.env.pass
},
channels: [
  "dshrops1"
]
}

// These are the commands the bot knows (defined below):
let knownCommands = { echo, haiku, quote }

// Function called when the "echo" command is issued:
function echo (target, context, params) {
// If there's something to echo:
if (params.length) {
// Join the params into a string:
const msg = params.join(' ')
// Send it back to the correct place:
sendMessage(target, context, msg)
} else { // Nothing to echo
 console.log(`* Nothing to echo`)
}
}

// Function called when the "haiku" command is issued:
function haiku (target, context) {
// Generate a new haiku:
haikudos((newHaiku) => {
// Split it line-by-line:
newHaiku.split('\n').forEach((h) => {
// Send each line separately:
sendMessage(target, context, h)
})
})
}

 async function quote (target, context){
  //cant deploy this on AWS yet Untill I deploy my database api as well.
  let quote = await fetch('http://localhost:3006/random').then(resp    =>resp.text())
  sendMessage(target,context,quote)
 }

// Helper function to send the correct type of message:
function sendMessage (target, context, message) {
if (context['message-type'] === 'whisper') {
client.whisper(target, message)
} else {
client.say(target, message)
}
}



// Create a client with our options:
let client = new tmi.client(opts)

// Register our event handlers (defined below):
client.on('message', onMessageHandler)
client.on('connected', onConnectedHandler)
client.on('disconnected', onDisconnectedHandler)

// Connect to Twitch:
client.connect()

// Called every time a message comes in:
function onMessageHandler (target, context, msg, self) {
if (self) { return } // Ignore messages from the bot

// This isn't a command since it has no prefix:
 if (msg.substr(0, 1) !== commandPrefix) {
   console.log(`[${target} (${context['message-type']})]        ${context.username}: ${msg}`)
  return
   }

// Split the message into individual words:
const parse = msg.slice(1).split(' ')
// The command name is the first (0th) one:
const commandName = parse[0]
// The rest (if any) are the parameters:
const params = parse.splice(1)

// If the command is known, let's execute it:
if (commandName in knownCommands) {
// Retrieve the function by its name:
const command = knownCommands[commandName]
// Then call the command with parameters:
command(target, context, params)
console.log(`* Executed ${commandName} command for ${context.username}`)
} else {
console.log(`* Unknown command ${commandName} from ${context.username}`)
}
}

// Called every time the bot connects to Twitch chat:
function onConnectedHandler (addr, port) {
console.log(`* Connected to ${addr}:${port}`)
}

// Called every time the bot disconnects from Twitch:
function onDisconnectedHandler (reason) {
console.log(`Womp womp, disconnected: ${reason}`)
process.exit(1)
}

很抱歉遇到任何格式问题

尽管twitch机器人可能存在所有问题,但当我在单独的终端上运行它时,它仍然可以正常运行,我很好奇为什么它可以从脚本中运行以及我可以做什么。

注意:我认为这可能与twitch api是IRC有关。

0 个答案:

没有答案